Shrinking the root filesystem on a Linux machine

This was needed by me in order to run ZFS on a DigitalOcean droplet, but the process should be applicable to more or less any Linux system.

1: Boot into rescue mode

Click the “Recover” link in the left vertical menu on the droplet in Digital Ocean, select “Boot from Recover ISO” now log into the system and type:

root@server:~# poweroff

Wait 10 seconds, and click the power button in the top-left corner of the DigitalOcean interface, let the machine power down and then power it up again, reboot takes approximately 20 seconds, then ssh back in: (Host keys have change because it is now running the rescue system, so add the StrictHostKeychecking=no option to the command)

user@workstation:~$ ssh -o "StrictHostKeyChecking=no" root@server 

Resize an unmounted partition

Get an overview over our current partition table:

root@server:~# fdisk -l /dev/vda
Disk /dev/vda: 160 GiB, 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
Disklabel type: gpt
Disk identifier: 890B1932-7DF1-C144-BC7B-9077D65BBCFC

Device      Start       End   Sectors   Size Type
/dev/vda1  262144 335544286 335282143 159.9G Linux filesystem
/dev/vda14   2048      8191      6144     3M BIOS boot
/dev/vda15   8192    262143    253952   124M EFI System

Partition table entries are not in disk order.

We want to divide vda1 into vda1 of 59GB and vda2 of 100GB for using with ZFS, so first we will need to shrink the filesystem om vda1:

root@server:~# e2fsck -f /dev/vda1
e2fsck 1.44.1 (24-Mar-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vda1: 29873/10477568 files (0.1% non-contiguous), 941511/41910267 blocks
root@server:~# resize2fs /dev/vda1 59G
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/vda1 to 15466496 (4k) blocks.
The filesystem on /dev/vda1 is now 15466496 (4k) blocks long.

This will have changed the filesystem inside of the partition, which means that the last 100GB of the partition is now unused, we now use fdisk to delete vda and create vda1 and 2:

root@server:~# fdisk /dev/vda

Welcome to fdisk (util-linux 2.31.1).
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/vda: 160 GiB, 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
Disklabel type: gpt
Disk identifier: 890B1932-7DF1-C144-BC7B-9077D65BBCFC

Device      Start       End   Sectors   Size Type
/dev/vda1  262144 335544286 335282143 159.9G Linux filesystem
/dev/vda14   2048      8191      6144     3M BIOS boot
/dev/vda15   8192    262143    253952   124M EFI System

Partition table entries are not in disk order.

Command (m for help): d
Partition number (1,14,15, default 15): 1

Partition 1 has been deleted.

Command (m for help): n
Partition number (1-13,16-128, default 1): 1
First sector (262144-335544286, default 262144): <enter>
Last sector, +sectors or +size{K,M,G,T,P} (262144-335544286, default 335544286): +59G

Created a new partition 1 of type 'Linux filesystem' and of size 59 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): n
Partition number (2-13,16-128, default 2): 2
First sector (123994112-335544286, default 123994112): <enter>
Last sector, +sectors or +size{K,M,G,T,P} (123994112-335544286, default 335544286): <enter>

Created a new partition 2 of type 'Linux filesystem' and of size 100.9 GiB.

Command (m for help): p
Disk /dev/vda: 160 GiB, 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
Disklabel type: gpt
Disk identifier: 890B1932-7DF1-C144-BC7B-9077D65BBCFC

Device         Start       End   Sectors   Size Type
/dev/vda1     262144 123994111 123731968    59G Linux filesystem
/dev/vda2  123994112 335544286 211550175 100.9G Linux filesystem
/dev/vda14      2048      8191      6144     3M BIOS boot
/dev/vda15      8192    262143    253952   124M EFI System

Partition table entries are not in disk order.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Boot back into the server own operating system, and start using your new partition.

Dette indlæg blev udgivet i Linux. Bogmærk permalinket.