{"id":6486,"date":"2017-02-11T17:18:10","date_gmt":"2017-02-11T09:18:10","guid":{"rendered":"http:\/\/rmohan.com\/?p=6486"},"modified":"2017-02-11T17:18:10","modified_gmt":"2017-02-11T09:18:10","slug":"kvm-how-to-resize-grow-an-ext4-lvm-vm","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6486","title":{"rendered":"KVM: How to resize (grow) an ext4 LVM VM"},"content":{"rendered":"<div class=\"entry-content\">\n<p>We use LVM both on the guest and the CentOS KVM host. Let\u2019s upgrade from 5 GB to 15 GB on the guest.<\/p>\n<p>1. Shutdown the VM.<\/p>\n<p>2. On the KVM host, take a backup of the VM LV in case something goes wrong.<\/p>\n<blockquote><p>[root@host ~]# <strong>dd if=\/dev\/vg_host01\/lv_guest01-vm of=.\/lv_guest01-vm-5gb<\/strong><\/p><\/blockquote>\n<p>3. On the KVM host, resize the VM LV.<\/p>\n<p>Add 1 GB:<\/p>\n<blockquote><p>[root@host ~]# <strong>lvresize -L+1G \/dev\/vg_host01\/lv_guest01-vm<\/strong><\/p><\/blockquote>\n<p>Or add all available space from the VG:<\/p>\n<blockquote><p>[root@host ~]# <strong>lvresize -l+100%FREE \/dev\/vg_host01\/lv_guest01-vm<\/strong><\/p><\/blockquote>\n<p>4. Boot up the VM again.<\/p>\n<p>5. Delete and recreate the LVM partition.<\/p>\n<blockquote><p>root@guest:~$ <strong>parted \/dev\/vda<\/strong><br \/>\n(parted) <strong>unit s<\/strong><br \/>\n(parted) <strong>print<\/strong><br \/>\nModel: Virtio Block Device (virtblk)<br \/>\nDisk \/dev\/vda: 30720000s<br \/>\nSector size (logical\/physical): 512B\/512B<br \/>\nPartition Table: msdos<\/p>\n<p>Number Start End Size Type File system Flags<br \/>\n1 2048s 1026047s 1024000s primary ext4 boot<br \/>\n2 1026048s 10239999s 9213952s primary lvm<\/p>\n<p>(parted) <strong>rm 2<\/strong><br \/>\nWarning: WARNING: the kernel failed to re-read the partition table on \/dev\/vda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.<\/p><\/blockquote>\n<p>Exit parted. You can also use fdisk to delete the partition, but when recreating the partition, fdisk doesn\u2019t know about the right first and last cylinders. Maybe exiting and running fdisk again solves that (forgot to check that, we\u2019ll try that next time) \u2013 <strong>UPDATE: on another try with fdisk it worked, no need for parted. You can do everything with fdisk in one run, just don\u2019t exit it and use the start cylinder from the deleted partition. It will know the end cylinder automatically<\/strong>. Ok, so we deleted the partition using parted and now we start fdisk again to recreate the partition. Also, for some reason, the first partition (1) is now just half in size, 512 MB instead of 1024 MB. Weird. parted messed something up there?<\/p>\n<blockquote><p>root@guest:~$ <strong>fdisk \/dev\/vda<\/strong><\/p>\n<p>WARNING: DOS-compatible mode is deprecated. It\u2019s strongly recommended to<br \/>\nswitch off the mode (command \u2018c\u2019) and change display units to<br \/>\nsectors (command \u2018u\u2019).<\/p>\n<p>Command (m for help): <strong>p<\/strong><\/p>\n<p>Disk \/dev\/vda: 15.7 GB, 15728640000 bytes<br \/>\n222 heads, 30 sectors\/track, 4612 cylinders<br \/>\nUnits = cylinders of 6660 * 512 = 3409920 bytes<br \/>\nSector size (logical\/physical): 512 bytes \/ 512 bytes<br \/>\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes<br \/>\nDisk identifier: 0x000831eb<\/p>\n<p>Device Boot Start End Blocks Id System<br \/>\n\/dev\/vda1 * 1 155 512000 83 Linux<\/p>\n<p>Command (m for help): <strong>n<\/strong><br \/>\nCommand action<br \/>\ne extended<br \/>\np primary partition (1-4) <strong>p<\/strong><br \/>\nPartition number (1-4): <strong>2<\/strong><br \/>\nFirst cylinder (155-4612, default 155):<br \/>\nUsing default value 155<br \/>\nLast cylinder, +cylinders or +size{K,M,G} (155-4612, default 4612):<br \/>\nUsing default value 4612<\/p>\n<p>Command (m for help): <strong>t<\/strong><br \/>\nPartition number (1-4): <strong>2<\/strong><br \/>\nHex code (type L to list codes): <strong>8e<\/strong><br \/>\nChanged system type of partition 2 to 8e (Linux LVM)<\/p>\n<p>Command (m for help): <strong>p<\/strong><\/p>\n<p>Disk \/dev\/vda: 15.7 GB, 15728640000 bytes<br \/>\n222 heads, 30 sectors\/track, 4612 cylinders<br \/>\nUnits = cylinders of 6660 * 512 = 3409920 bytes<br \/>\nSector size (logical\/physical): 512 bytes \/ 512 bytes<br \/>\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes<br \/>\nDisk identifier: 0x000831eb<\/p>\n<p>Device Boot Start End Blocks Id System<br \/>\n\/dev\/vda1 * 1 155 512000 83 Linux<br \/>\n\/dev\/vda2 155 4612 14844936 8e Linux LVM<\/p>\n<p>Command (m for help): <strong>w<\/strong><br \/>\nThe partition table has been altered!<\/p>\n<p>Calling ioctl() to re-read partition table.<\/p>\n<p>WARNING: Re-reading the partition table failed with error 16: Device or resource busy.<br \/>\nThe kernel still uses the old table. The new table will be used at<br \/>\nthe next reboot or after you run partprobe(8) or kpartx(8)<br \/>\nSyncing disks.<\/p><\/blockquote>\n<p>6. Reboot the VM.<\/p>\n<p>7. Resize the PV.<\/p>\n<p>Let\u2019s look at the PV first:<\/p>\n<blockquote><p>root@guest:~$ <strong>pvdisplay<\/strong><br \/>\n\u2014 Physical volume \u2014<br \/>\nPV Name \/dev\/vda2<br \/>\nVG Name vg_guest01<br \/>\nPV Size 4.39 GiB \/ not usable 3.00 MiB<br \/>\nAllocatable yes (but full)<br \/>\nPE Size 4.00 MiB<br \/>\nTotal PE 1124<br \/>\nFree PE 0<br \/>\nAllocated PE 1124<br \/>\nPV UUID IzkvJd-G9JW-Ju7r-1bDK-FLzC-I9y1-Ll3N4R<\/p><\/blockquote>\n<p>Still 5 GB. Let\u2019s resize it:<\/p>\n<blockquote><p>root@guest:~$ <strong>pvresize \/dev\/vda2<\/strong><br \/>\nFailed to read physical volume \u201c\/dev\/vda2\u201d<br \/>\n0 physical volume(s) resized \/ 0 physical volume(s) not resized<\/p><\/blockquote>\n<p>Don\u2019t worry about the \u201cnot resized\u201d. It worked:<\/p>\n<blockquote><p>root@guest:~$ <strong>pvdisplay<\/strong><br \/>\n\u2014 Physical volume \u2014<br \/>\nPV Name \/dev\/vda2<br \/>\nVG Name vg_guest01<br \/>\nPV Size 14.16 GiB \/ not usable 1016.00 KiB<br \/>\nAllocatable yes<br \/>\nPE Size 4.00 MiB<br \/>\nTotal PE 3624<br \/>\nFree PE 2500<br \/>\nAllocated PE 1124<br \/>\nPV UUID IzkvJd-G9JW-Ju7r-1bDK-FLzC-I9y1-Ll3N4R<\/p><\/blockquote>\n<p>8. Resize the LV.<\/p>\n<blockquote><p>root@guest:~$ <strong>lvresize -l+100%FREE \/dev\/vg_guest01\/lv_root<\/strong><\/p><\/blockquote>\n<p>Check it:<\/p>\n<blockquote><p>root@guest:~$ <strong>lvdisplay \/dev\/vg_guest01\/lv_root<\/strong><br \/>\n\u2014 Logical volume \u2014<br \/>\nLV Path \/dev\/vg_guest01\/lv_root<br \/>\nLV Name lv_root<br \/>\nVG Name vg_guest01<br \/>\nLV UUID 3Qfe2E-5j5H-H8el-iwxt-oN81-DvYq-AzENqH<br \/>\nLV Write Access read\/write<br \/>\nLV Creation host, time guest01, 2013-03-02 00:41:16 +0100<br \/>\nLV Status available<br \/>\n# open 1<br \/>\nLV Size 14.03 GiB<br \/>\nCurrent LE 3592<br \/>\nSegments 2<br \/>\nAllocation inherit<br \/>\nRead ahead sectors auto<br \/>\n\u2013 currently set to 256<br \/>\nBlock device 253:0<\/p><\/blockquote>\n<p>9. Resize the ext4 FS.<\/p>\n<blockquote><p>root@guest:~$ <strong>resize2fs \/dev\/vg_guest01\/lv_root<\/strong><br \/>\nresize2fs 1.41.12 (17-May-2010)<br \/>\nFilesystem at \/dev\/vg_guest01\/lv_root is mounted on \/; on-line resizing required<br \/>\nold desc_blocks = 1, new_desc_blocks = 1<br \/>\nPerforming an on-line resize of \/dev\/vg_guest01\/lv_root to 3678208 (4k) blocks.<br \/>\nThe filesystem on \/dev\/vg_guest01\/lv_root is now 3678208 blocks long.<\/p><\/blockquote>\n<p>10. Reboot the VM.<\/p>\n<p>11. Done.<\/p>\n<blockquote><p>root@guest:~$ <strong>df -h<\/strong><br \/>\nFilesystem Size Used Avail Use% Mounted on<br \/>\n\/dev\/mapper\/vg_guest01-lv_root<br \/>\n14G 1.2G 12G 9% \/<br \/>\ntmpfs 939M 0 939M 0% \/dev\/shm<br \/>\n\/dev\/vda1 485M 53M 408M 12% \/boot<\/p><\/blockquote>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\n<p>We use LVM both on the guest and the CentOS KVM host. Let\u2019s upgrade from 5 GB to 15 GB on the guest.<\/p>\n<p>1. Shutdown the VM.<\/p>\n<p>2. On the KVM host, take a backup of the VM LV in case something goes wrong.<\/p>\n<p>[root@host ~]# dd if=\/dev\/vg_host01\/lv_guest01-vm of=.\/lv_guest01-vm-5gb<\/p>\n<p>3. On the KVM host, [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6486"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6486"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6486\/revisions"}],"predecessor-version":[{"id":6487,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6486\/revisions\/6487"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}