Thứ Hai, 31 tháng 10, 2022

How to resize ext4 root partition live without umount on Linux

 

How to resize ext4 root partition live without umount on Linux

This article will focus on how to resize EXT4 root partition without unmount. This is an easy way for some system where you are unable to unmount root partition and the system can be recovered easily if something goes wrong like for example AWS instance.

Resizing any live partition without unmout comes with a tremendous risk of loosing data thus it is not recommended. If you a have a sensitive data stored on your system, it is always recommended to take the system down make a backup and resize the partition while it is not mounted.

In the following example we are going to resize a partition of a fresh single partition AWS Linux instance. The current partition size is 7.8GB:

# df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      7.8G  642M  6.8G   9% /

However, the disk size reports 20GB:

# fdisk -l

Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd7f2e0e8

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux

The above partition is mounted as root and thus the partition cannot be unmount:

# umount /
umount: /: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

All what needs to be done is take a note of a starting sector of a current partition, which in our case is 4096. Remove the partition and recreate a new, larger partition starting from sector 4096. For this we are simply going to use fdisk command. Let’s start by printing again our current partition table:

# fdisk /dev/xvda

Welcome to fdisk (util-linux 2.25.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/xvda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd7f2e0e8

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux

Once again please take a note of the starting sector 4096. Still in fdisk‘s interactive mode remove partition:

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.


Next, create a new partition right on the top of the previous and ensure that you use same starting sector:

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 4096
Last sector, +sectors or +size{K,M,G,T,P} (4096-41943039, default 41943039): 

Created a new partition 1 of type 'Linux' and of size 20 GiB.

Make the partition 1 bootable and print new partition table:

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

Command (m for help): p
Disk /dev/xvda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xd7f2e0e8

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 41943039 41938944  20G 83 Linux

Confirm all new details and write new partition table:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).

At this point the system needs to be rebooted in order to remount our root partition with a new size. Force fsck on next reboot to ensure that the partition is checked before it is mounted. To do so just create an empty file called forcefsck in the root of your / partition:

# touch /forcefsck

Reboot your system. Once the system is up again check the partition size:

df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       20G  644M   19G   4% /

and last fsck check:

# tune2fs -l /dev/xvda1

Không có nhận xét nào:

Đăng nhận xét