in Linux, 兴趣

Shrink existing /home LVM with xfs format

Summary

If you require to shrink the file system with xfs format, unfortunately there is no method to do so, in my case, the /root and /home system was installed in the LVM, and with xfs file system, cannot do much to /root system, dumped out /home file system, format and shrank the file system and restored the /home system.

Problem:No free sectors available

After working with RHEL 7 for quite some time, found I may need to create a new partition, however found out my LVM filled the whole file system, as below fdisk -l command shows,

[root@localhost ~]# fdisk -l

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0007f14e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   335544319   167259136   8e  Linux LVM

Disk /dev/mapper/rhel-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-home: 115.4 GB, 115368525824 bytes, 225329152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost ~]#

Tried to create new partition with fail result, as below shows,

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
No free sectors available

Command (m for help):

Found my file system root and home filled the whole disk, and with xfs format in LVM,

[root@localhost ~]# mount -l | grep mapper
/dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/mapper/rhel-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost ~]#
[root@localhost /]# umount /home
umount: /home: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@localhost /]#

For xfs format, you cannot use e2fsck and lvreduce to reduce the size, that is for ext2 format, as below command shows,

[root@localhost Desktop]# mount | grep mapper
/dev/mapper/rhel-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/mapper/rhel-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost Desktop]# umount /home
[root@localhost Desktop]# cd
[root@localhost ~]# e2fsck -f /dev/mapper/rhel-home 
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks...
e2fsck: Bad magic number in super-block while trying to open /dev/mapper/rhel-home

The superblock could not be read or does not describe a correct ext2
filesystem.  If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193 <device>

df -hT command shows the root and home file system is xfs.

[root@localhost ~]# df -hT
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs        50G  5.7G   45G  12% /
devtmpfs              devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                 tmpfs     1.9G  140K  1.9G   1% /dev/shm
tmpfs                 tmpfs     1.9G  9.0M  1.9G   1% /run
tmpfs                 tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             xfs       497M  159M  339M  32% /boot
.host:/               vmhgfs    120G  110G  9.4G  93% /mnt/hgfs
tmpfs                 tmpfs     378M   16K  378M   1% /run/user/0
/dev/mapper/rhel-home xfs       108G  338M  108G   1% /home

install xfsdump

[root@localhost ~]# yum install xfsdump -y

dump the home system to /tem/home.dump

[root@localhost ~]# xfsdump -f /tmp/home.dump /home
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.4 (dump format 3.0) - type ^C for status and control

 ============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
 -> home
session label entered: "home"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of localhost.localdomain:/home
xfsdump: dump date: Fri Aug 19 09:25:06 2016
xfsdump: session id: ded77b0c-052a-4d97-b836-5e55d9fc8c8e
xfsdump: session label: "home"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 318958656 bytes
xfsdump: /var/lib/xfsdump/inventory created

 ============================= media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> home
media label entered: "home"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 316926432 bytes
xfsdump: dump size (non-dir files) : 314043752 bytes
xfsdump: dump complete: 103 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /tmp/home.dump OK (success)
xfsdump: Dump Status: SUCCESS

umount /home, delete, create and format with shrank size

[root@localhost ~]# cd /
[root@localhost /]# umount /home 
[root@localhost /]# lvremove /dev/rhel/home
Do you really want to remove active logical volume home? [y/n]: y
  Logical volume "home" successfully removed
[root@localhost /]# lvcreate --name home -L 10G rhel 
WARNING: xfs signature detected on /dev/rhel/home at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/rhel/home.
  Logical volume "home" created.
[root@localhost /]# mkfs.xfs /dev/rhel/home
meta-data=/dev/rhel/home         isize=256    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

mount, restore /home

[root@localhost /]# mount /dev/rhel/home /home/
[root@localhost /]# xfsrestore -f /tmp/home.dump /home/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description: 
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /home
xfsrestore: volume: /dev/mapper/rhel-home
xfsrestore: session time: Fri Aug 19 09:25:06 2016
xfsrestore: level: 0
xfsrestore: session label: "home"
xfsrestore: media label: "home"
xfsrestore: file system id: 0e987d74-044d-4557-a315-16b1e85f0385
xfsrestore: session id: ded77b0c-052a-4d97-b836-5e55d9fc8c8e
xfsrestore: media id: d1449ee8-f7d2-44ca-8cfc-d4d5c9e86810
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 971 directories and 4348 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 1 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /tmp/home.dump OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost /]# cd /home/
[root@localhost home]# ls -lah
total 8.0K
drwxr-xr-x.  4 root        root          36 Aug 19 09:32 .
dr-xr-xr-x. 20 root        root        4.0K Aug 19 04:02 ..
drwx------.  3 admin       admin         74 Aug 17 13:21 admin
drwx------. 18 xionghuilin xionghuilin 4.0K Aug 18 20:10 xionghuilin
[root@localhost home]# 

successful result, reduced to 10G from 108G

[root@localhost home]# df -hT
Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs        50G  6.0G   44G  12% /
devtmpfs              devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                 tmpfs     1.9G  140K  1.9G   1% /dev/shm
tmpfs                 tmpfs     1.9G  9.0M  1.9G   1% /run
tmpfs                 tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1             xfs       497M  159M  339M  32% /boot
.host:/               vmhgfs    120G  111G  8.9G  93% /mnt/hgfs
tmpfs                 tmpfs     378M   16K  378M   1% /run/user/0
/dev/sr0              iso9660   3.8G  3.8G     0 100% /var/ftp/dvd
/dev/mapper/rhel-home xfs        10G  338M  9.7G   4% /home
[root@localhost home]# 

problem still exist, cannot create new partition

The problem is, I still cannot create new partition, for example /dev/sda3, as all the space had been used by /dev/sda2, the LVM partition, and I cannot do much about it, as the /root file system was inside this partition. I have tried to remove the partition and re-create the partition, without touch to the /root file system, however when reboot, the /root file system cannot be mounted.

[root@localhost ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  rhel lvm2 a--  159.51g 97.51g
[root@localhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   1   3   0 wz--n- 159.51g 97.51g
[root@localhost ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home rhel -wi-ao---- 10.00g
  root rhel -wi-ao---- 50.00g
  swap rhel -wi-ao----  2.00g
[root@localhost ~]#
[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                zMMtLu-h7Kq-eA5N-RUGw-YHk8-uifN-vVm4Hd
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-08-11 16:15:48 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                jOUoWU-uBpc-QLOs-Ffb7-9dGU-ELZU-FSncyC
  LV Write Access        read/write
  LV Creation host, time localhost, 2016-08-11 16:15:49 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/rhel/home
  LV Name                home
  VG Name                rhel
  LV UUID                dy5XUG-LGjc-tUDP-7fp7-sGR3-LTd1-sp9G0w
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-08-19 09:30:25 +0800
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               159.51 GiB
  PE Size               4.00 MiB
  Total PE              40834
  Alloc PE / Size       15872 / 62.00 GiB
  Free  PE / Size       24962 / 97.51 GiB
  VG UUID               FY15EN-79eI-MDry-uQgW-LZ7u-mdRT-SJMLr9

below action will crash the /root system

I followed to this Link to shrink the lvm size, have tested, /root system crashed, not sure how to solve this problem…

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0007f14e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   335544319   167259136   8e  Linux LVM

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (1026048-335544319, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-335544319, default 335544319): +80G
Partition 2 of type Linux and of size 80 GiB is set

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition\'s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition \'Linux\' to \'Linux LVM\'

Command (m for help): p

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0007f14e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   168798207    83886080   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]#

Write a Comment

Comment