How to resize (extend) a partition-based file system in Linux

Note: Resizing a filesystem on a disk parition can be dangerous and can result in loss of data. Make sure you have the backup taken prior to following the steps outlined below.

Caveats

1. The filesystem you want to resize must reside on the last partition of the disk. There is no data loss in this case as we recreate the partition without destroying the actual data on it.
2. In case it is not the last partition, you have to destroy the data and recreate a new partition with the desired size.

Extending partion-based filesystem

1. First, check the existing size of the filesystem.

# # df -hP /data01
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme1n1p1 1014M   33M  982M   4% /data01
# mount | grep -w data01
/dev/nvme1n1p1 on /data01 type xfs (rw,relatime,seclabel,attr2,inode64,noquota)

As verified from the ‘mount’ command output above, the filesystem on the partition is ‘xfs’.

2. Note the partition details and the disk details using the ‘parted’ command. Ensure that you provide the disk name and not the partition name in the below command:

# parted /dev/nvme1n1 u s p
Model: NVMe Device (nvme)
Disk /dev/nvme1n1: 4194304s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End       Size      Type     File system  Flags
 1      2048s  2099199s  2097152s  primary  xfs

From the output above, you can see the partition size (2099199s) is less than the disk size (4194304s). So there is room to increase the partition.

3. Umount the filesystem first.

# umount /data01

4. Delete the partition having the filesystem ‘/data01’.

# parted /dev/nvme1n1p1 rm 1
Information: You may need to update /etc/fstab.

Verify if the partition is delete. I had only one partition on the disk, so I should see no partition now.

# parted /dev/nvme1n1 u s p
Model: NVMe Device (nvme)
Disk /dev/nvme1n1: 4194304s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End  Size  Type  File system  Flags

5. Now, lets recreate the partition with a bigger size. Please note the starting sector from the step 2 i.e. 2048s. We will increase the partition from 1GB to 1.5GB.

# parted -s /dev/nvme1n1 mkpart primary 2048s 1.5G

Verify the new partition size again.

# parted /dev/nvme1n1 u s p
Model: NVMe Device (nvme)
Disk /dev/nvme1n1: 4194304s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End       Size      Type     File system  Flags
 1      2048s  2930687s  2928640s  primary

6. We need to run fsck on the newly created partition. Also note, we are not creating any filesystem here, as it is already present on the partition. As we are using the XFS filesystem, use ‘xfs_repair’ command instead of ‘e2fsck’.

# xfs_repair /dev/nvme1n1p1
Phase 1 - find and verify superblock...
bad primary superblock - bad magic number !!!

attempting to find secondary superblock...
.................................................................................................................................................................................................................................................found candidate secondary superblock...
verified secondary superblock...
writing modified primary superblock
sb realtime bitmap inode 18446744073709551615 (NULLFSINO) inconsistent with calculated value 65
resetting superblock realtime bitmap ino pointer to 65
sb realtime summary inode 18446744073709551615 (NULLFSINO) inconsistent with calculated value 66
resetting superblock realtime summary ino pointer to 66
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
sb_icount 0, counted 64
sb_ifree 0, counted 61
sb_fdblocks 259568, counted 259560
        - found root inode chunk
Phase 3 - for each AG...
        - scan and clear agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
Phase 5 - rebuild AG headers and trees...
        - reset superblock...
Phase 6 - check inode connectivity...
        - resetting contents of realtime bitmap and summary inodes
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...
Note - stripe unit (0) and width (0) were copied from a backup superblock.
Please reset with mount -o sunit=,swidth= if necessary
done

7. Extend the filesystem, using ‘xfs_growfs’ command. Also before running this command, we need to mount the filesystem, otherwise you would get below error:

# xfs_growfs /dev/nvme1n1p1
xfs_growfs: /dev/nvme1n1p1 is not a mounted XFS filesystem
# mount /dev/nvme1n1p1 /data01
# xfs_growfs /dev/nvme1n1p1
meta-data=/dev/nvme1n1p1         isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=262144, 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 262144 to 366080

8. Verify the new size of the mount point.

# df -hP /data01
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme1n1p1  1.4G   33M  1.4G   3% /data01
Related Post