One of my virtual machines runs out of disk space in the root (/) partition, and I want to remove the /home partition and using this free space to resize the root partition.

Nowadays, for any serious systems, I’d use LVM with XFS on top. However, for this test VM, I have a virtual disk with 4 partitions without LVM. Managing partition on LVM is much easier than working the disk partition directly. I can’t remember the last time I had to do this, so here is something to remind my future-self if I need this information again. Hey, if you’re reading this now, this might be useful to you too.

First, I connect this disk to another VM, and it shows up as /dev/sdb. (Warning: Do not copy and paste the command from this blog post. You must figure out what is the actual device you want to work on.) Let’s display the partition table:

[root@workstation ~]# parted /dev/sdb print
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  538MB   537MB   fat32
 2      538MB   2538MB  2000MB  linux-swap(v1)
 3      2538MB  21.5GB  18.9GB  xfs
 4      ??????  ??????  ??????  xfs

Note: The “????” is in the above output because I forgot the exact numbers before I removed it. So just imagine it’s some real numbers there. The partition number 4 is used for /home and we’re going to use that space for partition number 3.

Let’s delete the partition number 4 and use the remaining space on this disk to resize the partition number 3.

[root@workstation ~]# parted /dev/sdb
(parted) rm 4
resizepart 3 100%
(parted) p
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  538MB   537MB   fat32
 2      538MB   2538MB  2000MB  linux-swap(v1)
 3      2538MB  21.5GB  18.9GB  xfs

(parted) q
Information: You may need to update /etc/fstab.

The next step is to resize the filesystem on the partition number 3. It’s an XFS, so we should use xfs_growfs.

[root@workstation ~]# xfs_growfs /dev/sdb3
xfs_growfs: /dev/sdb3 is not a mounted XFS filesystem

Huh… it needs to be mounted first. A quick look in the man page for xfs_growfs confirms that:

...
DESCRIPTION
       xfs_growfs  expands  an existing XFS filesystem (see xfs(5)).  The mount-point argument is the pathname of the directory where the filesystem is mounted. The block-
       device argument is the device name of a mounted XFS filesystem.  The filesystem must be mounted to be grown (see mount(8)).  The existing contents of the filesystem
       are undisturbed, and the added space becomes available for additional file storage.
...

So let’s do it.

[root@workstation ~]# mount /dev/sdb3 /mnt
[root@workstation ~]# xfs_growfs /dev/sdb3
meta-data=/dev/sdb3              isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1 spinodes=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2621440 to 4623355

[root@workstation ~]# df -lh /dev/sdb3
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb3        18G  9.2G  8.6G  52% /mnt

Voilla. C’est facile! (if you do it every week or so.)

References: