🚀

yocto linuxのパーティションを拡大する

2022/07/22に公開
  • AWS上のYoctoLinuxの容量を増やしたい
  • とりあえずボリュームを増やしてみたがファイルシステムに反映されてないじゃないか!
  • しかもYoctoLinuxの知識がなさ過ぎてaptもyumも使えないぞ

という経緯から生まれた、Linuxの基礎ツールだけでファイルシステムを拡張する手順メモ

check current disk space

root@ip-0-0-0-0:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       2.5G  1.2G  1.2G  50% /
devtmpfs        448M     0  448M   0% /dev
tmpfs           467M     0  467M   0% /dev/shm
tmpfs           187M  9.2M  178M   5% /run
...

only 1.2GB is available.

check device name

root@ip-0-0-0-0:~# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1     259:0    0   32G  0 disk 
|-nvme0n1p1 259:1    0 46.8M  0 part /boot
|-nvme0n1p2 259:2    0  2.6G  0 part /
`-nvme0n1p3 259:3    0   44M  0 part [SWAP]

disable swap

linux - How to safely turn off swap permanently and reclaim the space? (on Debian Jessie) - Unix & Linux Stack Exchange

root@ip-0-0-0-0:~#  swapoff --all
root@ip-0-0-0-0:~# vi /etc/fstab

Comment out / remove the swap partition's UUID

#UUID=8715bff0-c6c2-48b7-93b0-5e009631b0e5      swap    swap    defaults        0       0

next,

  • delete swap partition
  • delete main partition
  • (re)create partiton with large space for main linux filesystem
  • (re)create partiton with remaining capacity for swap
  • change partition type for swap

procedures:

  1. fdisk /dev/nvme0n1
  2. p: please memo start number of main Linux partition
  3. d, 3(partittion number of swap)
  4. d, 2(partittion number of Linux main)
  5. n, 2, 98304, 50000000, y
  • 98304 should be same value as the original starting position, which you memoed.
  • 50000000` should be large enough, but leave about 1700000sector(~= 8GB) for the swap partition.
  1. n, 3, Enter, Enter
  2. t, 19
  3. w

Linuxのディスク拡張をpartxコマンドで停止なしで実現 | ほそぼそ話

root@ip-0-0-0-0:~# fdisk /dev/nvme0n1

Welcome to fdisk (util-linux 2.36.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (5651693 != 67108863) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.
Command (m for help): p

Disk /dev/nvme0n1: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: Amazon Elastic Block Store              
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: ACB00E85-3528-44F5-9C2B-1882676B91E1

Device           Start     End Sectors  Size Type
/dev/nvme0n1p1    2048   97879   95832 46.8M Microsoft basic data
/dev/nvme0n1p2   98304 5561547 5463244  2.6G Linux filesystem
/dev/nvme0n1p3 5561548 5651659   90112   44M Linux swap

Partition 3 does not start on physical sector boundary.
Command (m for help): d
Partition number (1-3, default 3): 3

Partition 3 has been deleted.
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.
Command (m for help): n
Partition number (2-128, default 2): 2
First sector (97880-67108830, default 98304): 98304
Last sector, +/-sectors or +/-size{K,M,G,T,P} (98304-67108830, default 67108830): 50000000

Created a new partition 2 of type 'Linux filesystem' and of size 23.8 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 number (3-128, default 3): 3
First sector (50001920-67108830, default 98304): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (50001920-67108830, default 67108830): 
Command (m for help): t
Partition number (1-3, default 3): 3
Partition type or alias (type L to list all): 19

Changed type of partition 'Linux filesystem' to 'Linux swap'.
Command (m for help): p
Disk /dev/nvme0n1: 32 GiB, 34359738368 bytes, 67108864 sectors
Disk model: Amazon Elastic Block Store              
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: ACB00E85-3528-44F5-9C2B-1882676B91E1

Device            Start      End  Sectors  Size Type
/dev/nvme0n1p1     2048    97879    95832 46.8M Microsoft basic data
/dev/nvme0n1p2    98304 50000000 49901697 23.8G Linux filesystem
/dev/nvme0n1p3 50001920 67108830 17106911  8.2G Linux swap
Command (m for help): w
The partition table has been altered.
Syncing disks.

reflect fdisk changes by partx

root@ip-0-0-0-0:~# partx -v -u /dev/nvme0n1
partition: none, disk: /dev/nvme0n1, lower: 0, upper: 0
/dev/nvme0n1: partition table type 'gpt' detected
/dev/nvme0n1: partition #1 resized
/dev/nvme0n1: partition #2 resized

reflect partition changes to file system by resize2fs

argument requires partion number.

  • OK: /dev/nvme0n1p2
  • NG: /dev/nvme0n1
root@ip-0-0-0-0:~# resize2fs /dev/nvme0n1p2
resize2fs 1.46.1 (9-Feb-2021)
Filesystem at /dev/nvme0n1p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 4
The filesystem on /dev/nvme0n1p2 is now 8376315 (4k) blocks long.
root@ip-0-0-0-0:~# resize2fs /dev/nvme0n1p3
resize2fs 1.46.1 (9-Feb-2021)
resize2fs: Bad magic number in super-block while trying to open /dev/nvme0n1p3
Couldn't find valid filesystem superblock.

enable swap

参考: linux スワップ(swap)領域の作成

enable /dev/nvme0n1p3 as swap.

root@ip-0-0-0-0:~# mkswap /dev/nvme0n1p3
Setting up swapspace version 1, size = 8.2 GiB (8758730752 bytes)
no label, UUID=46a54f3d-c4cd-4e45-88e4-f4bf0e664062
root@ip-0-0-0-0:~# swapon /dev/nvme0n1p3
root@ip-0-0-0-0:~# swapon -s
Filename                                Type            Size    Used    Priority
/dev/nvme0n1p3                          partition       8553448 6400    -2

register /dev/nvme0n1p3 as swap for automatically mounted even when the OS is rebooted. (You can specify a UUID, but note that the UUID has changed from the one you commented out.)

root@ip-0-0-0-0:~# vi /etc/fstab
/dev/nvme0n1p3   swap    swap    defaults        0       0

check

root@ip-0-0-0-0::~# free -m
              total        used        free      shared  buff/cache   available
Mem:            932         733          40          19         157         114
Swap:          8352          65        8287

root@ip-0-0-0-0::~# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1     259:0    0   32G  0 disk 
|-nvme0n1p1 259:1    0 46.8M  0 part /boot
|-nvme0n1p2 259:2    0 23.8G  0 part /
`-nvme0n1p3 259:4    0  8.2G  0 part [SWAP]

root@ip-0-0-0-0:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        31G  1.2G   29G   4% /
devtmpfs        448M     0  448M   0% /dev
tmpfs           467M     0  467M   0% /dev/shm
tmpfs           187M  9.2M  178M   5% /run
...

Discussion