ext3 on LVMのサイズ拡張

先日、WindowsマシンのHDDを交換して余ったHDDを使って、LinuxマシンのHDDの交換を行いました。
交換自体は簡単だったのですが、GPartedを使っても領域拡張がGUI上で行えず、致し方なくコマンドラインで、拡張してみました。

そんなわけで、忘備録程度にVMwareつかって、作業内容を書き留めておこうと。
サンプルは16GBイメージを20GBに拡張するものです。

まず、dfをとっておきます。

# df -H
Filesystem Size Used Avail Use% マウント位置
/dev/mapper/VolGroup00-LogVol00
16G 4.9G 9.9G 34% /
/dev/sda1 104M 20M 80M 20% /boot
tmpfs 262M 0 262M 0% /dev/shm
次に/etc/fstabの中身も押さえておきます。

# cat /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
# End of the block added by the VMware software
ちなみに、外から見ると、いまんとここんな感じになってます。
_000142.jpeg
ここからは、おまじないが続きますが、非常に危険な作業になります。何てったって、ディスクのパーティションテーブルをいじくりますから:-)

まず、fdiskで勢いに任せて、やってしまいます(^^;

# fdisk /dev/sda

The number of cylinders for this disk is set to 2610.
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): p ←パーティションを表示させます。

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2088 16667437+ 8e Linux LVM ←今回のresize対象

Command (m for help): d ←パーティションを削除(書き込まない限り消えません)
Partition number (1-4): 2 ←対象のパーティションの番号

Command (m for help): n ←新たにパーティションを作成
Command action
e extended
p primary partition (1-4)
p ←Primary partitionをチョイス
Partition number (1-4): 2 ←番号は以前と同じ
First cylinder (14-2610, default 14): ←デフォルトのまま
Using default value 14
Last cylinder or +size or +sizeM or +sizeK (14-2610, default 2610): ←デフォルトのまま
Using default value 2610

Command (m for help): t ←パーティションタイプを変更
Partition number (1-4): 2 ←番号は以前と同じ
Hex code (type L to list codes): 8e ←Linux LVMを指定
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): w ←書き込み!(これで書き換わります)
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
#
ってことで、速攻rebootします。が、ここから先は、GPartedでもインストールイメージでrescue modeで立ち上げるもよし、インストールされているイメージ以外のメディアを利用してコマンドラインが使えるようにします。
ここまでの状態で、こんなんなってます。
_000143.jpeg

この後は、乱暴ですが、勢いに任せて、こんな感じぃ。(以下は、ハンドコピーなので、ミススペルしてるかも(^^;)
まずは、PV(Physical Volume)のサイズを拡張します。

# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

お次は、LV(Logical Volume)の拡張ですが、交換したディスクいっぱいに拡張するような場合は、実際、どれだけ拡張すれば、使い切れるのか、さっぱりわかりません。
んなわけで、拡張して余白が出来たサイズより、でかいサイズを指定して、コマンドを実行して、わざとエラーにします。で、ここで指定する「ディスクグループ/ボリューム」は最初に/etc/fstabで確認したやつを指定します。

# lvextend -L+5G VolGroup00/LogVol00
Extending logical volume LogVol00 to 19.88GB
Insufficient free space: 160 extents needed, but only 128 available

親切なことに、あとどれだけあまりがあるか教えてくれるので、availableだと言われている数字を使って、サイズではなくLE単位で拡張します。

# lvextend -l+128 VolGroup00/LogVol00
Extending logical volume LogVol00 to 18.88GB
Logical volume LogVol00 successfully resized

あとは、fsckしたあとにfilesystemのサイズ変更をして、エラーが出なければおしまいです。

# e2fsck -f /dev/VolGroup00/LogVol00
e2fsck 1.41.3 (12-Oct-2008)
Adding dirhash hint to filesystem.

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/VolGroup00/LogVol00: ***** FILE SYSTEM WAS MODIFIED *****
/dev/VolGroup00/LogVol00: 170410/3899392 files (1.2% non-contiguous), 1310690/38
99392 blocks
# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.41.3 (12-Oct-2008)
Resizing the filesystem on /dev/VolGroup00/LogVol00 to 4947968 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 4947986 blocks long.
#

そんでもって、再起動後にdfした結果は...

# df -H
Filesystem Size Used Avail Use% マウント位置
/dev/mapper/VolGroup00-LogVol00
20G 4.9G 14G 27% /
/dev/sda1 104M 20M 80M 20% /boot
tmpfs 262M 0 262M 0% /dev/shm
めでたしめでたし。
あ、エラーが出た場合は、他のサイトに任せます(^^;

この記事へのコメント


この記事へのトラックバック

LVM内のパーティションを拡張する(メモ)
Excerpt: 作業、テストなどに使っているLinux の VMwareイメージ。これのディスク容量が小さくなったので、広げるための手順のメモ。 /(ルートパーティション)を拡張するのはちょっと手間だったのと、いちい..
Weblog: DOOM! DOOMER!! DOOMEST!?
Tracked: 2010-05-14 16:15