Shrink existing /home LVM with xfs format

来源:互联网 发布:快速排序算法举例说明 编辑:程序博客网 时间:2024/06/17 12:51

  • Summary
  • ProblemNo free sectors available
  • install xfsdump
  • dump the home system to temhomedump
  • umount home delete create and format with shrank size
  • mount restore home
  • successful result reduced to 10G from 108G
  • problem still exist cannot create new partition
    • below action will crash the root system
  • Refernce

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 -lDisk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0007f14e   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     1026047      512000   83  Linux/dev/sda2         1026048   335544319   167259136   8e  Linux LVMDisk /dev/mapper/rhel-root: 53.7 GB, 53687091200 bytes, 104857600 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel-swap: 2147 MB, 2147483648 bytes, 4194304 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/mapper/rhel-home: 115.4 GB, 115368525824 bytes, 225329152 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/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/sdaWelcome 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): nPartition type:   p   primary (2 primary, 0 extended, 2 free)   e   extendedSelect (default p): pPartition number (3,4, default 3):No free sectors availableCommand (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 /homeumount: /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-blocke2fsck: Superblock invalid, trying backup blocks...e2fsck: Bad magic number in super-block while trying to open /dev/mapper/rhel-homeThe superblock could not be read or does not describe a correct ext2filesystem.  If the device is valid and it really contains an ext2filesystem (and not swap or ufs or something else), then the superblockis 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 -hTFilesystem            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% /devtmpfs                 tmpfs     1.9G  140K  1.9G   1% /dev/shmtmpfs                 tmpfs     1.9G  9.0M  1.9G   1% /runtmpfs                 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/hgfstmpfs                 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 /homexfsdump: using file dump (drive_simple) strategyxfsdump: 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) -> homesession label entered: "home" --------------------------------- end dialog ---------------------------------xfsdump: level 0 dump of localhost.localdomain:/homexfsdump: dump date: Fri Aug 19 09:25:06 2016xfsdump: session id: ded77b0c-052a-4d97-b836-5e55d9fc8c8exfsdump: session label: "home"xfsdump: ino map phase 1: constructing initial dump listxfsdump: ino map phase 2: skipping (no pruning necessary)xfsdump: ino map phase 3: skipping (only one dump stream)xfsdump: ino map construction completexfsdump: estimated dump size: 318958656 bytesxfsdump: /var/lib/xfsdump/inventory created ============================= media label dialog =============================please enter label for media in drive 0 (timeout in 300 sec) -> homemedia label entered: "home" --------------------------------- end dialog ---------------------------------xfsdump: creating dump session media file 0 (media 0, file 0)xfsdump: dumping ino mapxfsdump: dumping directoriesxfsdump: dumping non-directory filesxfsdump: ending media filexfsdump: media file size 316926432 bytesxfsdump: dump size (non-dir files) : 314043752 bytesxfsdump: dump complete: 103 seconds elapsedxfsdump: 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/homeDo 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/homemeta-data=/dev/rhel/home         isize=256    agcount=4, agsize=655360 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=0        finobt=0data     =                       bsize=4096   blocks=2621440, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=0log      =internal log           bsize=4096   blocks=2560, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =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) strategyxfsrestore: version 3.1.4 (dump format 3.0) - type ^C for status and controlxfsrestore: searching media for dumpxfsrestore: examining media file 0xfsrestore: dump description: xfsrestore: hostname: localhost.localdomainxfsrestore: mount point: /homexfsrestore: volume: /dev/mapper/rhel-homexfsrestore: session time: Fri Aug 19 09:25:06 2016xfsrestore: level: 0xfsrestore: session label: "home"xfsrestore: media label: "home"xfsrestore: file system id: 0e987d74-044d-4557-a315-16b1e85f0385xfsrestore: session id: ded77b0c-052a-4d97-b836-5e55d9fc8c8exfsrestore: media id: d1449ee8-f7d2-44ca-8cfc-d4d5c9e86810xfsrestore: using online session inventoryxfsrestore: searching media for directory dumpxfsrestore: reading directoriesxfsrestore: 971 directories and 4348 entries processedxfsrestore: directory post-processingxfsrestore: restoring non-directory filesxfsrestore: restore complete: 1 seconds elapsedxfsrestore: Restore Summary:xfsrestore:   stream 0 /tmp/home.dump OK (success)xfsrestore: Restore Status: SUCCESS[root@localhost /]# cd /home/[root@localhost home]# ls -lahtotal 8.0Kdrwxr-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 admindrwx------. 18 xionghuilin xionghuilin 4.0K Aug 18 20:10 xionghuilin[root@localhost home]# 

successful result, reduced to 10G from 108G

[root@localhost home]# df -hTFilesystem            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% /devtmpfs                 tmpfs     1.9G  140K  1.9G   1% /dev/shmtmpfs                 tmpfs     1.9G  9.0M  1.9G   1% /runtmpfs                 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/hgfstmpfs                 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/sdaWelcome 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): pDisk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0007f14e   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     1026047      512000   83  Linux/dev/sda2         1026048   335544319   167259136   8e  Linux LVMCommand (m for help): dPartition number (1,2, default 2): 2Partition 2 is deletedCommand (m for help): nPartition type:   p   primary (1 primary, 0 extended, 3 free)   e   extendedSelect (default p): pPartition number (2-4, default 2):First sector (1026048-335544319, default 1026048):Using default value 1026048Last sector, +sectors or +size{K,M,G} (1026048-335544319, default 335544319): +80GPartition 2 of type Linux and of size 80 GiB is setCommand (m for help): mCommand 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): tPartition number (1,2, default 2): 2Hex code (type L to list all codes): 8eChanged type of partition 'Linux' to 'Linux LVM'Command (m for help): pDisk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x0007f14e   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     1026047      512000   83  Linux/dev/sda2         1026048   168798207    83886080   8e  Linux LVMCommand (m for help): wThe 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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.[root@localhost ~]#

Refernce

1, Youtube: Shrink an XFS partition
2, LVM Partition Resizing


0 0
原创粉丝点击