KVMのゲストOSディスク拡張の作業メモ。ポイントポイントでは情報が見つかるのだけど、一貫した内容はなく、頻繁にやる作業でもなく確実に忘れる自信があるので、自分用に記録。OSはUbuntu12.04。
下記は、「KVMのゲストOSにディスク容量を128GB拡張する場合」の例です。
目次
全体の流れです。要は、ゲストOSの既存の論理ボリュームを拡張できればいいわけですが、いがいと手順を踏みます。仮想マシンマネージャー上でサクッでできると嬉しいんですけどね。
- 拡張前のディスクパーティションを確認
- 仮想マシンの領域を拡張する
- 仮想マシンのパーティションを設定する
- 仮想マシン再起動
- 物理ボリュームを作成し、ボリュームグループを拡張し、論理ボリュームを拡張する
1. 拡張前のディスクパーティションを確認
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//-------------------------------------------/ // 1-1. fdiskでパーティションテーブルを確認 // /dev/sdaが68.7GBしかありません。 root@virtual-test:# fdisk -l Disk /dev/sda: 68.7 GB, 68719476736 bytes 255 heads, 63 sectors/track, 8354 cylinders, total 134217728 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00067151 Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 134215679 66856961 5 Extended /dev/sda5 501760 134215679 66856960 8e Linux LVM Disk /dev/mapper/virtual-test-root: 67.4 GB, 67385688064 bytes 255 heads, 63 sectors/track, 8192 cylinders, total 131612672 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 |
2. 仮想マシンの領域を拡張する
1 2 3 4 5 6 7 8 9 10 11 |
// 2-1. 仮想マシンを停止する []# virsh shutdown virtual-test // 2-2. 仮想マシンの領域を128GB拡大する []# qemu-img resize virtual-test +128G // 2-3. 仮想マシンを起動する []# virsh start virtual-test // 2-4. 仮想マシンにログイン []# virsh console virtual-test |
3. 仮想マシンのパーティションを設定する。
128GBの追加領域でパーティションを区切ります。以下、 仮想マシン上で実行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
//-------------------------------------------/ // 3-1. dfでファイルシステムのディスク容量を確認 // ディスク容量にはまだ反映されません。 root@virtual-test:~# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/virtual-test-root 65691344 64890100 0 100% / udev 1016732 4 1016728 1% /dev tmpfs 410320 272 410048 1% /run none 5120 0 5120 0% /run/lock none 1025796 0 1025796 0% /run/shm overflow 1024 0 1024 0% /tmp /dev/sda1 233191 25004 195746 12% /boot //-------------------------------------------/ // 3-2. fdiskコマンドでパーティションテーブルを確認 // /dev/sdaが68.7GB -> 206.2GBに増えているはずです。 root@virtual-test:~# fdisk -l Disk /dev/sda: 206.2 GB, 206158430208 bytes 255 heads, 63 sectors/track, 25064 cylinders, total 402653184 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00067151 Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 134215679 66856961 5 Extended /dev/sda5 501760 134215679 66856960 8e Linux LVM //-------------------------------------------/ // 3-3. fdiskコマンドでパーティションテーブルを設定します。 root@virtual-test:~# fdisk /dev/sda //***** 「p」を入力(一覧表示) *****// Command (m for help): p Disk /dev/sda: 206.2 GB, 206158430208 bytes 255 heads, 63 sectors/track, 25064 cylinders, total 402653184 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00067151 Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 134215679 66856961 5 Extended /dev/sda5 501760 134215679 66856960 8e Linux LVM //***** 「n」を入力(パーティション新規作成) *****// Command (m for help): n Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) //***** 「p」を選択(primary) *****// Select (default p): p //***** 「3」を選択(sda3を新規で作成) *****// Partition number (1-4, default 3): 3 //***** 開始セクターを入力(sda5の'End'に1を足した値(134215679+1)を入力します。) *****// First sector (499712-402653183, default 499712): 134215680 Last sector,sectors orsize{K,M,G} (134215680-402653183, default 402653183): Using default value 402653183 //***** 「p」を確認(sda3が増えていると思います) *****// Command (m for help): p Disk /dev/sda: 206.2 GB, 206158430208 bytes 255 heads, 63 sectors/track, 25064 cylinders, total 402653184 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00067151 Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 134215679 66856961 5 Extended /dev/sda3 134215680 402653183 134218752 83 Linux /dev/sda5 501760 134215679 66856960 8e Linux LVM //***** 「t」を入力(sda3のパーティションタイプをLVMに変更します) *****// Command (m for help): t Partition number (1-5): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) //***** 「p」を入力(sda3がLinux -> Linux LVMに変更されているか確認) *****// Command (m for help): p Disk /dev/sda: 206.2 GB, 206158430208 bytes 255 heads, 63 sectors/track, 25064 cylinders, total 402653184 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00067151 Device Boot Start End Blocks Id System /dev/sda1 * 2048 499711 248832 83 Linux /dev/sda2 501758 134215679 66856961 5 Extended /dev/sda3 134215680 402653183 134218752 8e Linux LVM /dev/sda5 501760 134215679 66856960 8e Linux LVM //***** 「w」を入力(変更を反映させる) *****// 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 or after you run partprobe(8) or kpartx(8) Syncing disks. |
4. 仮想マシン再起動
パーティションの変更を反映させるため、仮想マシンを再起動する。
1 2 3 4 5 6 7 8 |
// 4-1. 仮想マシンを停止する virsh shutdown virtual-test // 4-2. 仮想マシンを起動する virsh start virtual-test // 4-3. ログイン virsh console virtual-test |
5. 物理ボリュームを作成し、ボリュームグループを拡張し、論理ボリュームを拡張する
これで最後です。
3で作成した128GBのパーティション(dev/sda3)に対し、物理ボリュームを割り当てます。次に、その物理ボリュームをボリュームグループに追加し、その拡張分を既存の論理ボリュームに追加します。聞くより慣れ・・ですね。
ボリュームグループ名や論理ボリューム名は環境により異なるので、それぞれpvdisplayやlvdisplayなどで確認します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
//-------------------------------------------/ // 5-1. 物理ボリュームを追加する // 先程追加した/dev/sda3(128GB)を物理ボリュームに追加します root@virtual-test:~# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created //-------------------------------------------/ // 5-2. 物理ボリュームを確認 // 物理ボリュームに/dev/sda3(128GB)が追加されているか確認 root@virtual-test:~# pvdisplay --- Physical volume --- PV Name /dev/sda5 VG Name virtual-test PV Size 63.76 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 16322 Free PE 0 Allocated PE 16322 PV UUID gH9J3k-Ju1t-Ksr7-GRBa-LcGQ-tdd2-3Cs6Ot "/dev/sda3" is a new physical volume of "128.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 128.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID wCplKb-1daX-9OjK-1SuJ-7Ahl-c93m-iOfFS4 //-------------------------------------------/ // 5-3. VG(ボリュームグループ)名を取得 // VG Name = 「virtual-test」と確認できます。 root@virtual-test:~# vgdisplay --- Volume group --- VG Name virtual-test System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 63.76 GiB PE Size 4.00 MiB Total PE 16322 Alloc PE / Size 16322 / 63.76 GiB Free PE / Size 0 / 0 VG UUID 04cJUA-UuJm-ckHI-iyAm-ykfU-lr22-C9cxUp //-------------------------------------------/ // 5-4. VG(ボリュームグループ)を拡張 // ボリュームグループ「virtual-test」に/dev/sda3を追加します // Free PEが128.00GiBに増えています。 root@virtual-test:~# vgextend virtual-test /dev/sda3 Volume group "virtual-test" successfully extended root@virtual-test:~# vgdisplay --- Volume group --- VG Name virtual-test System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 191.76 GiB PE Size 4.00 MiB Total PE 49090 Alloc PE / Size 16322 / 63.76 GiB Free PE / Size 32768 / 128.00 GiB VG UUID 04cJUA-UuJm-ckHI-iyAm-ykfU-lr22-C9cxUp //-------------------------------------------/ // 5-5. 論理ボリューム名を確認 // 論理ボリューム名はLV Name = "/dev/virtual-test/root"と確認できました。 root@virtual-test:~# lvdisplay --- Logical volume --- LV Name /dev/virtual-test/root VG Name virtual-test LV UUID YjaQbj-QGUN-dVuU-GcOF-YXmb-BBXd-F1NJI1 LV Write Access read/write LV Status available # open 1 LV Size 62.76 GiB Current LE 16066 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0 --- Logical volume --- LV Name /dev/virtual-test/swap_1 VG Name virtual-test LV UUID PSTQr1-GnAf-Mj4E-e4Tn-bINx-auhm-nMDTjn LV Write Access read/write LV Status available # open 2 LV Size 1.00 GiB Current LE 256 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:1 //-------------------------------------------/ // 5-5. 論理ボリュームの拡張 // 既存の論理ボリュームに拡張した128GBを追加で割り当てます。 // 「-l +100%FREE」でボリュームグループの空きを全て割り当ててくれます。 root@virtual-test:~# lvextend -l +100%FREE /dev/virtual-test/root // 最後にファイルシステムの拡張を行う。この処理は数分かかる。 root@virtual-test:~# resize2fs /dev/virtual-test/root resize2fs 1.42 (29-Nov-2011) Filesystem at /dev/gitlab/root is mounted on /; on-line resizing required old_desc_blocks = 4, new_desc_blocks = 12 Performing an on-line resize of /dev/gitlab/root to 50006016 (4k) blocks. The filesystem on /dev/gitlab/root is now 50006016 blocks long. // dfコマンドで容量が増えていることを確認。めでたし、めでたし。 root@gitlab:~# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/gitlab-root 197795528 64890216 122911892 35% / udev 1016732 4 1016728 1% /dev tmpfs 410320 276 410044 1% /run none 5120 0 5120 0% /run/lock none 1025796 0 1025796 0% /run/shm /dev/sda1 233191 25004 195746 12% /boot overflow 1024 0 1024 0% /tmp |