[Linux partition] How to shrink home to move sectors and then increase root size

Thing you want to do

In this article, the Linux root partition has become too small, so we will reduce the extra home partition and expand the root partition.

Specifically, the configuration that was like this at first

Device             Start       End   Sectors  Size Type
/dev/nvme0n1p1      2048    206847    204800  100M EFI System
/dev/nvme0n1p2    206848    239615     32768   16M Microsoft reserved
/dev/nvme0n1p3    239616 168011775 167772160   80G Microsoft basic data
/dev/nvme0n1p4 168011776 170108927   2097152    1G Windows recovery environment
/dev/nvme0n1p5 170108928 274966527 104857600   50G Microsoft basic data
/dev/nvme0n1p6 274966528 370976767  96010240 45.8G Linux root
/dev/nvme0n1p7 370976768 907847679 536870912  256G Linux home

Reduce / dev / nvme0n1p7 by 56GB and expand / dev / nvme0n1p6 by 50GB to make the following configuration.

Device             Start       End   Sectors  Size Type
/dev/nvme0n1p1      2048    206847    204800  100M EFI System
/dev/nvme0n1p2    206848    239615     32768   16M Microsoft reserved
/dev/nvme0n1p3    239616 168011775 167772160   80G Microsoft basic data
/dev/nvme0n1p4 168011776 170108927   2097152    1G Windows recovery environment
/dev/nvme0n1p5 170108928 274966527 104857600   50G Microsoft basic data
/dev/nvme0n1p6 274966528 475834367 200867840 95.8G Linux root
/dev/nvme0n1p7 475834368 895264767 419430400  200G Linux home

What i did

The operation can be divided into the following three main

  1. Shrink home partition / dev / nvme0n1p7
  2. Move the starting sector of home partition / dev / nvme0n1p7 to make unallocated sector after root partition / dev / nvme0n1p6
  3. Extend the root partition / dev / nvme0n1p6

For the reason of operation 2, in the case of this partition configuration, there is a home partition in the sector below the root partition. This is because it is not possible to create one partition across sectors, so after shrinking the home partition, you need to shift the start sector of the home partition back by the amount you want to extend the root partition.

From now on, I will explain step by step. First of all, boot Linux from USB and display the console, as it can be any distribution. Since the environment this time is archlinux, the installation media of arch (archlinux-2019.12.01-x86_64.iso We will proceed with 2019.12.01-x86_64.iso)).

If you need to boot the OS in the middle of the process, you may destroy the file system, so be sure to proceed to the end of each step before booting. In fact, we have confirmed that the OS boots without problems at the end of each step.

STEP1: Shrink the home partition / dev / nvme0n1p7

First, check the file system with the e2fsck command.

You will be asked if you want to optimize the extend tree on the way, but it seems that you do not have to do this as long as the extend tree does not exceed the upper limit. I proceeded with all no. Reference: https://unix.stackexchange.com/questions/392537/inode-extent-tree-at-level-1-could-be-shorter-ignored

e2fsck -f <The path of the file system you want to check>
root@archiso ~ # e2fsck -f /dev/nvme0n1p7
e2fsck 1.45.4 (23-Sep-2019)
Pass 1: Checking inodes, blocks, and sizes
Inode 8404395 extent tree (at level 1) could be shorter. Optimize<y>? no
Inode 8404481 extent tree (at level 2) could be narrower. Optimize<y>? no
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/nvme0n1p7:2498456/16777216 files (0.9% non-contiguous), 32640354/67108864 blocks
e2fsck -f /dev/nvme0n1p7 7.08s user 0.82s system 6% cpu 2:07.42 total

Next, execute the reduction process with resize2fs.

resize2fs <The path of the file system you want to shrink> <Partition size after reduction(K,M,Can be specified with G etc.)> 
root@archiso ~ # resize2fs /dev/nvme0n1p7 200G
resize2fs 1.45.4 (23-Sep-2019)
Resizing the filesystem on /dev/nvme0n1p7 to 52428800 (4k) blocks.
The filesystem on /dev/nvme0n1p7 is now 52428800 (4k) blocks long.

resize2fs /dev/nvme0n1p7 200G 6.41s user 14.03s system 36% cpu 55.878 total

So far, the actual file system shrinkage has been completed, but this information is not reflected in LVM (the layer where the partition size of each partition is recorded). Therefore, use fdisk to update LVM.

root@archiso ~ # fdisk <Volume with shrunk partition>

Delete the information of the partition reduced by the d command once,
Enter the partition information changed to the size after reduction with the n command and recreate the information.
Save to LVM with w command
root@archiso ~ # fdisk /dev/nvme0n1
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1-7, default 7): 7

Partition 7 has been deleted.

Command (m for help): n
Partition number (7-128, default 7): 
First sector (370976768-1000215182, default 370976768): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (370976768-1000215182, default 1000215182): +200G

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

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

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

At this point, the reduction of / dev / nvme0n1p7 is complete.

STEP2: Move the start sector of home partition / dev / nvme0n1p7 and make unallocated sector after root partition / dev / nvme0n1p6

Use sfdisk to move the partition. This command also updates LVM, so this step is all you need to do.

Please note that this process takes time. In this case, it's an SSD and I thought it would take tens of minutes, but it actually took about 6 hours.

echo '+<The size you want to move(=Extended size of root partition)>' | sfdisk --move-data <Volume containing the partition you want to move> -N <The number of the version to move>
root@archiso ~ # echo '+50G' | sfdisk --move-data /dev/nvme0n1 -N 7
Checking that no-one is using this disk right now ... OK

Disk /dev/nvme0n1: 476.96 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: KXG50ZNV512G TOSHIBA                    
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: 80F7A56F-2E69-49B5-8A44-09E9998615AE

Old situation:

Device             Start       End   Sectors  Size Type
/dev/nvme0n1p1      2048    206847    204800  100M EFI System
/dev/nvme0n1p2    206848    239615     32768   16M Microsoft reserved
/dev/nvme0n1p3    239616 168011775 167772160   80G Microsoft basic data
/dev/nvme0n1p4 168011776 170108927   2097152    1G Windows recovery environment
/dev/nvme0n1p5 170108928 274966527 104857600   50G Microsoft basic data
/dev/nvme0n1p6 274966528 370976767  96010240 45.8G Linux filesystem
/dev/nvme0n1p7 370976768 790407167 419430400  200G Linux home

/dev/nvme0n1p7:
New situation:
Disklabel type: gpt
Disk identifier: 80F7A56F-2E69-49B5-8A44-09E9998615AE

Device             Start       End   Sectors  Size Type
/dev/nvme0n1p1      2048    206847    204800  100M EFI System
/dev/nvme0n1p2    206848    239615     32768   16M Microsoft reserved
/dev/nvme0n1p3    239616 168011775 167772160   80G Microsoft basic data
/dev/nvme0n1p4 168011776 170108927   2097152    1G Windows recovery environment
/dev/nvme0n1p5 170108928 274966527 104857600   50G Microsoft basic data
/dev/nvme0n1p6 274966528 370976767  96010240 45.8G Linux filesystem
/dev/nvme0n1p7 475834368 895264767 419430400  200G Linux home

Data move:
 typescript file : /root/sfdisk-nvme0n1p7.move
 old start: 370976768, new start: 475834368 (move 419430400 sectors)

STEP3: Extend root partition / dev / nvme0n1p6

resize2fs has a function that changes the partition size according to the fdisk information. Therefore, first update the information on LVM with fdisk and perform the actual expansion with resize2fs.

Use the following command for fdisk as in STEP1.

root@archiso ~ # fdisk <Volume containing the partition you want to grow>

Delete the partition information you want to expand with the d command, and then use the d command.
Enter the partition information changed to the enlarged size with the n command and recreate the information.
Save to LVM with w command
root@archiso ~ # fdisk /dev/nvme0n1
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1-7, default 7): 6

Partition 6 has been deleted.

Command (m for help): n
Partition number (6,8-128, default 6): 6
First sector (274966528-1000215182, default 274966528): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (274966528-475834367, default 475834367): 

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

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

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

Next, after executing the same e2fsck as STEP1, execute resize2fs without specifying the partition size. Unlike the time of reduction, the expansion was completed in an instant.

root@archiso ~ # e2fsck -f /dev/nvme0n1p6
(Omission:The execution result will be similar to that of STEP1)

root@archiso ~ # resize2fs /dev/nvme0n1p6
resize2fs 1.45.4 (23-Sep-2019)
Resizing the filesystem on /dev/nvme0n1p6 to 25108480 (4k) blocks.
The filesystem on /dev/nvme0n1p6 is now 25108480 (4k) blocks long.

This completes the process.

Summary

Resize2fs is convenient for scaling partitions, and --move-data of sfdisk is convenient for moving sectors!

When moving to a sector, I thought I had to use dd to get rid of it, but using sfdisk solved it in one shot! !!

Recommended Posts

[Linux partition] How to shrink home to move sectors and then increase root size
[Linux] How to subdivide files and folders
How to switch between Linux and Mac shells
Steps to change partition including Linux system area and boot area
[Google Colab] How to interrupt learning and then resume it