Closed5

ルートボリュームのサイズ縮小 (ext4)

Kazuki Suda (@superbrothers)Kazuki Suda (@superbrothers)

ルートボリュームに限らず、ファイルシステムのサイズ縮小はアンマウントしてからしかできないので、シャットダウンしたあとに外部メディアからブートしてから作業する必要がある。

Kazuki Suda (@superbrothers)Kazuki Suda (@superbrothers)
pi@raspberrypi:~ $ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    0 465.8G  0 disk
├─sda1        8:1    0   256M  0 part
└─sda2        8:2    0 465.5G  0 part
mmcblk0     179:0    0  14.8G  0 disk
├─mmcblk0p1 179:1    0   256M  0 part /boot
└─mmcblk0p2 179:2    0  14.6G  0 part /
pi@raspberrypi:~ $ lsblk -f /dev/sda2
NAME FSTYPE FSVER LABEL    UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda2 ext4   1.0   writable 87f67789-4694-4d47-8fcd-d86ef4277f59
pi@raspberrypi:~ $ sudo e2fsck /dev/sda2
e2fsck 1.46.2 (28-Feb-2021)
writable: clean, 185744/29800000 files, 3894746/122030849 blocks
Kazuki Suda (@superbrothers)Kazuki Suda (@superbrothers)

今回はルートボリュームに使っている /dev/sda2 を 100G に縮小して残りで新しいパーティションを作成する。

pi@raspberrypi:~ $ sudo mount /dev/sda2 /mnt
pi@raspberrypi:~ $ df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       466G   15G  433G   4% /mnt
pi@raspberrypi:~ $ sudo umount /mnt

現状 15G 使っていることがわかったので、一旦ファイルシステムサイズを 20G まで縮小してからパーティションのサイズを縮小して、その後ファイルシステムのサイズを最大まで広げるという手順を取る。

pi@raspberrypi:~ $ sudo e2fsck -f /dev/sda2
e2fsck 1.46.2 (28-Feb-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Inode 2896 extent tree (at level 1) could be shorter.  Optimize<y>? yes
Inode 30494 extent tree (at level 1) could be shorter.  Optimize<y>? yes
Inode 30728 extent tree (at level 1) could be shorter.  Optimize<y>? yes
Pass 4: Checking reference counts
Pass 5: Checking group summary information

writable: ***** FILE SYSTEM WAS MODIFIED *****
writable: 204348/29800000 files (0.1% non-contiguous), 3799311/122030849 blocks
pi@raspberrypi:~ $ sudo resize2fs /dev/sda2 20G
resize2fs 1.46.2 (28-Feb-2021)
Resizing the filesystem on /dev/sda2 to 5242880 (4k) blocks.
The filesystem on /dev/sda2 is now 5242880 (4k) blocks long.
Kazuki Suda (@superbrothers)Kazuki Suda (@superbrothers)

ファイルシステムのサイズを縮小できたので、パーティションのサイズを 100G に縮小して残りで新しいパーティションを作成する。注意することとして、パーティションサイズの変更は fdisk では一度パーティション定義を削除したのちに新たに希望するサイズでパーティションを作成する手順となる。その際に既存のパーティションの開始セクタと同じ番号で開始セクタを指定しないとファイルシステムが壊れる。

pi@raspberrypi:~ $ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.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/sda: 465.76 GiB, 500107862016 bytes, 976773168 sectors
Disk model: 2115
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0xdeca7dfc

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sda1  *      2048    526335    524288   256M  c W95 FAT32 (LBA)
/dev/sda2       526336 976773134 976246799 465.5G 83 Linux

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

Partition 2 has been deleted.

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

Created a new partition 2 of type 'Linux' and of size 100 GiB.
Partition #2 contains a ext4 signature.

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

The signature will be removed by a write command.

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

Created a new partition 3 of type 'Linux' and of size 365.5 GiB.

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

Kazuki Suda (@superbrothers)Kazuki Suda (@superbrothers)

パーティションサイズが変更できたら、ファイルシステムサイズを最大まで拡張すればおわり。

pi@raspberrypi:~ $ sudo e2fsck -f /dev/sda2
e2fsck 1.46.2 (28-Feb-2021)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks...
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
Block bitmap differences:  -(2752512--2757983) -(2760192--2761016) -(2762752--2766878) -(2768896--2776234) -(2787840--2788297) -(2849298--2849416) -(2850816--2851557) -(2851840--2861381) -(2861408--2861436) -(2861440--2861466) -(2862816--2862840) -(2865152--2867199) -(2887680--2888710) -(2889216--2889673) -(2889728--2897919) -2916352 -(2916864--2917055) -(2917120--2918347) -(2918400--2926591) -(2949120--2949124) -(2949128--2949136) +(4096000--4096453) -(4292608--4293118) -(4915200--4915262) -(4915304--4915695) -(4915698--4915709) -(4915712--4915775) -(4915862--4915867) -(4915871--4916757) -(4923019--4923053) -(4938124--4938175) -4980736 -5013504 -5046272 -(5046280--5046293)
Fix<y>? yes
Inode bitmap differences:  -(392001--394814) -(394817--394831) -(394833--394837)
Fix<y>? yes
Padding at end of inode bitmap is not set. Fix<y>? yes

writable: ***** FILE SYSTEM WAS MODIFIED *****
writable: 204348/1280000 files (0.4% non-contiguous), 2006620/5242880 blocks
pi@raspberrypi:~ $ sudo resize2fs /dev/sda2
resize2fs 1.46.2 (28-Feb-2021)
Resizing the filesystem on /dev/sda2 to 26213743 (4k) blocks.
The filesystem on /dev/sda2 is now 26213743 (4k) blocks long.
pi@raspberrypi:~ $ sudo mount /dev/sda2 /mnt
pi@raspberrypi:~ $ df -h /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        99G  7.3G   88G   8% /mnt
このスクラップは2022/03/26にクローズされました