linux disk領域拡張

最近はLVMを使っているので需要はないかもしれませんが
通常のext3の領域拡張の方法について記載します。
たまたまリストア先のdiskが大きかったので余っている部分を
有効活用しようという事で実施しています。

現状を確認

# fdisk -l
Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/xvdc1 * 1 1044 8385898+ 83 Linux
/dev/xvdc2 1045 2088 8385930 83 Linux
/dev/xvdc3 2089 2610 4192965 82 Linux swap / Solaris
/dev/xvdc4 2611 30515 224146912+ 5 Extended
/dev/xvdc5 2611 30515 224146881 83 Linux

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.8G 6.1G 1.4G 83% /
tmpfs 755M 0 755M 0% /dev/shm
/dev/sda5 208G 128G 70G 65% /home
/dev/sda2 7.8G 693M 6.7G 10% /var

今回は残っている領域を/homeに割あてます。

まずパーティションサイズを変更します。
unmountが必要なので今回はシングルユーザーモードで行います。
# init 1

シングルユーザーになったら/homeをunmount
sh-3.2# umount /dev/sda5

マウントされていない事を確認
sh-3.2# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.8G 6.1G 1.4G 83% /
tmpfs 755M 0 755M 0% /dev/shm
/dev/sda2 7.8G 693M 6.7G 10% /var

パーティションを再作成
sh-3.2# fdisk /dev/sda

The number of cylinders for this disk is set to 38913.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): m ←ヘルプを見ればだいたいわかります。
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): d ←領域を削除(fdiskの表示を参考、忘れたらpで確認すればOK)
Partition number(1-5): 4 ←拡張領域のサイズ変更なのでまず拡張領域を削除

Command (m for help): d
Partition number(1-5): 5 ←増やすパーティション部分を削除

Command (m for help): p ←消えているか確認
Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/xvdc1 * 1 1044 8385898+ 83 Linux
/dev/xvdc2 1045 2088 8385930 83 Linux
/dev/xvdc3 2089 2610 4192965 82 Linux swap / Solaris

Command (m for help): n ←新規で作成
Command action
e extended
p primary partition (1-4)
e ←拡張領域
Partition number (1-4):
4 ←消した所に復活させる
First cylinder (2611-38913, default 2611): ←以前と場所が変わらないように注意
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-38913, default 38913): ←終了シリンダー番号、全部割り当てるので最後までにしてます。

次に拡張領域に割り当てていた領域を作成
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (2611-38913, default 2611): ←これも次のシリンダーの初めにセットされているのでそのまま
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-38913, default 38913): ←終了シリンダー番号、全部割り当てるので最後までにしてます。

Command (m for help): p ←割り当てられたか確認

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1044 8385898+ 83 Linux
/dev/sda2 1045 2088 8385930 83 Linux
/dev/sda3 2089 2610 4192965 82 Linux swap / Solaris
/dev/sda4 2611 38913 291603847+ 5 Extended
/dev/sda5 2611 38913 291603816 83 Linux

問題なさそうです。

最後に書き込みをして終了です。
Command (m for help): w
The partition table has been altered!

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

これでシングルユーザーモードでの作業は完了です。
一応マウントしました。再起動とかでfstabに書いてあるので自動マウントしますけどね
sh-3.2# mount -t ext3 /dev/sda5

起動したら状況を確認

# fdisk -l
Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1044 8385898+ 83 Linux
/dev/sda2 1045 2088 8385930 83 Linux
/dev/sda3 2089 2610 4192965 82 Linux swap / Solaris
/dev/sda4 2611 38913 291603847+ 5 Extended
/dev/sda5 2611 38913 291603816 83 Linux

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.8G 6.1G 1.4G 83% /
tmpfs 755M 0 755M 0% /dev/shm
/dev/sda5 208G 128G 70G 65% /home
/dev/sda2 7.8G 693M 6.7G 10% /var

fdiskでパーティションは増えているけど実領域は増えていない事がわかります。
状態としては外の容器は大きくしたけど中の容器はまだそのままという状態です。

次にファイルシステムをサイズ変更します。
resize2fsで使える領域のMAXまで割り当ててくれるので簡単に拡張できます。
# resize2fs /dev/sda5
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/sda5 is mounted on /home; on-line resizing required
Performing an on-line resize of /dev/sda5 to 72900954 (4k) blocks.
The filesystem on /dev/sda5 is now 72900954 blocks long.

無事領域を拡張できました。
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 7.8G 6.1G 1.4G 83% /
tmpfs 755M 0 755M 0% /dev/shm
/dev/sda5 270G 128G 129G 50% /home
/dev/sda2 7.8G 695M 6.7G 10% /var

コメントする