2015年4月9日木曜日

resize2fs と fdisk で ext4 のパーティションを縮小する


1 resize2fs と fdisk で ext4 のパーティションを縮小する




1.1 はじめに



パーティションの拡大縮小はいつもGParted を使っていた.

しかし,今回はディスプレイが繋がっていないマシンのパーティションを変更するために,CLI で操作できるresize2fs と fdisk で操作した.

結構悩んだが,わかればそんなにややこしい手順じゃないはず.






1.2 作業の流れ



パーティションを縮小すると言っても,実はパーティションのサイズだけ縮小してもダメらしい.

ファイルシステムのサイズも変えないといけないんだそうだ.

しかも,この2つのサイズが一致してないとまずいらしい.



resize2fs はサイズ指定なしで実行すると,ファイルシステムのサイズを可能な限り大きくする.

つまり,パーティションのサイズいっぱいにファイルシステムのサイズを広げてくれる.

これを利用すれば,面倒な計算なしに,ファイルシステムとパーティションサイズのサイズを合わせられる.

(逆にパーティションサイズに合わせて縮小,ということはやってくれないっぽい?)



というわけで,以下の手順を取る.



  1. resize2fs でファイルシステムを縮小(予定サイズよりもちょっと小さめに)


  2. fdisk でパーティションのサイズを縮小(予定サイズに縮小)


  3. resize2fs でファイルシステムのサイズをパーティションのサイズに合わせる(勝手に計算してくれる)







1.3 resize2fs でファイルシステム



今回は/dev/sdb1 を512GB から32GB に縮小する例を示す.



まずは,resize2fs でファイルシステムのサイズを予定サイズより少し小さめに変更.

512GB–>31GB の変更は結構時間がかかる(10分くらい?).



# e2fsck -f /dev/sdb1
e2fsck 1.42.5 (29-Jul-2012)
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/sdb1: 184019/30777344 files (1.9% non-contiguous), 7022898/123088640 blocks
# resize2fs /dev/sdb1 31G
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sdb1 to 8388608 (4k) blocks.
The filesystem on /dev/sdb1 is now 8388608 blocks long.
#


次にfdisk でパーティションのサイズを変更する.

操作的にはサイズを変更というより,元のパーティションを削除して,同じところに小さいサイズのパーティションを作りなおすと,なる.



# fdisk /dev/sdb
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

Command (m for help): p

Disk /dev/sdb: 512.1 GB, 512110190592 bytes
255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000c3f6e

Device Boot Start End Blocks Id System
/dev/sdb1 * 2048 984711167 492354560 83 Linux
/dev/sdb2 984713214 1000214527 7750657 5 Extended
Partition 2 does not start on physical sector boundary.
/dev/sdb5 984713216 1000214527 7750656 82 Linux swap / Solaris

Command (m for help): d <-- パーティション削除
Partition number (1-5): 1

Command (m for help): n <-- 新しいパーティション作成
Partition type:
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-1000215215, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-984713213, default 984713213): +32G

Command (m for help): p

Disk /dev/sdb: 512.1 GB, 512110190592 bytes
255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000c3f6e

Device Boot Start End Blocks Id System
/dev/sdb1 2048 69208063 34603008 83 Linux
/dev/sdb2 984713214 1000214527 7750657 5 Extended
Partition 2 does not start on physical sector boundary.
/dev/sdb5 984713216 1000214527 7750656 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#


最後にファイルシステムのサイズをパーティションのサイズに合わせる.



# resize2fs /dev/sdb1
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sdb1 to 8650752 (4k) blocks.
The filesystem on /dev/sdb1 is now 8650752 blocks long.
#


これで完了(のはず)






1.4 おわりに



パーティションの操作にあまり慣れていなかったので,結構調べるのに時間かかった.






--
My Emacs Files At GitHub

0 件のコメント:

コメントを投稿