exlar's IT note

ITやスマートデバイスを中心とした趣味情報の寄せ集め

LVMでパーティションサイズを調整(縮小・拡張)する

このブログ記事は移転しました。10秒後に自動でリダイレクトします。

----------------------

VMware (ESXi) で増量したディスクをLVMに組み込む方法”では、新規割り当てディスクを既存LVMに組み込む方法をまとめた。Step.2では、LVM管理下の各LVの容量を調整する。

現在の状態と目標

現在はStep.1の状態。Step.2のようにディスク容量の調整を行いたい。/home 領域が無駄に大きいので、ディスク拡張をきっかけに /mnt を大きくしたい。

f:id:exlair:20131208150134p:plain
図3 パーティションの縮小と拡張

(Step.2) LVM構築の大まかな流れ

  1. /homeの縮小
    • ファイルシステムのサイズ縮小
    • LVの縮小
  2. /mntの拡張
    • LVの拡張
    • ファイルシステムのサイズ拡張

縮小と拡張では順番が逆になることに注意する。

事前作業

/home を使いそうな作業スクリプト、デーモンを念のため止める。

(Step.2-1)/dev/vg_cent/lv_home (/home)を縮小

LVの縮小の場合は、まずファイルシステムのサイズを縮小した後にLVの容量を減らす。ここでは650GBにしているので、元々の1.2TBから -550GBしていることになる。

本当はもっと大きく、一気に減らしてしまいたいのだが、既に/homeには大量データが入っており、一気にデータ退避ができないので少しずつしか動かせない事情がある。問題なければ一気にサイズ縮小をした方が効率的である。

$ bash (個人的に利用しているzshでは/homeのスクリプトを動作させている為)

$ umount /home

$ fsck.ext4 -f /dev/vg_cent/lv_home

$ resize2fs /dev/vg_cent/lv_home 650G

$ lvreduce -L 650G /dev/vg_cent/lv_home

$ mount -t ext4 /dev/vg_cent/lv_home /home

(参考) 実行結果のコピー

$ bash                                                                                                                                                          bash-4.1$ ls -al /home/
drwxr-xr-x.  2 root root 4096  5月  5 18:03 2013 .
dr-xr-xr-x. 22 root root 4096 11月 25 00:36 2013 ..

bash-4.1$ sudo fsck.ext4 -f /dev/vg_cent/lv_home
e2fsck 1.41.12 (17-May-2010)
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
(中略)

bash-4.1$ sudo resize2fs /dev/vg_cent/lv_home 650G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_cent/lv_home to 170393600 (4k) blocks.
The filesystem on /dev/vg_cent/lv_home is now 170393600 blocks long.

bash-4.1$ sudo lvreduce -L 650G /dev/vg_cent/lv_home
xxx@cent Password: 
  WARNING: Reducing active logical volume to 650.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
  Reducing logical volume lv_home to 650.00 GiB
  Logical volume lv_home successfully resized

bash-4.1$ sudo mount -t ext4 /dev/vg_cent/lv_home /home

(Step.2-2) /dev/vg_cent/lv_mnt (/mnt) を拡大

LVの拡張の場合は、まずLVを拡張した後にファイルシステムのサイズの拡張を行う

  1. lvextend -l +100%FREE /dev/vg_cent/lv_mnt
  2. resize2fs /dev/vg_cent/lv_mnt

lvextendでは空き領域100%フルで拡張。 -L にするとサイズ指定も可能。空き容量をフル利用したいため、100%指定とした。

(参考) 実行結果のコピー

bash-4.1$ sudo lvextend -l +100%FREE /dev/vg_cent/lv_mnt
  Extending logical volume lv_mnt to 1.17 TiB
  Logical volume lv_mnt successfully resized

bash-4.1$ sudo resize2fs /dev/vg_cent/lv_mnt
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_cent/lv_mnt is mounted on /mnt; on-line resizing required
old desc_blocks = 38, new_desc_blocks = 75
Performing an on-line resize of /dev/vg_cent/lv_mnt to 313426944 (4k) blocks.
The filesystem on /dev/vg_cent/lv_mnt is now 313426944 blocks long.

結果確認

$ df -Th

$ fdisk -l

$ sudo pvdisplay
$ sudo lvdisplay