{"id":113,"date":"2015-04-25T19:27:42","date_gmt":"2015-04-26T02:27:42","guid":{"rendered":"http:\/\/www.asrivas.me\/blog\/?p=113"},"modified":"2017-05-21T09:04:58","modified_gmt":"2017-05-21T16:04:58","slug":"extending-a-lvm-partition-on-a-ubuntu-vm","status":"publish","type":"post","link":"https:\/\/www.asrivas.me\/blog\/extending-a-lvm-partition-on-a-ubuntu-vm\/","title":{"rendered":"Extending an LVM partition on a Ubuntu VM"},"content":{"rendered":"<p>Logical volumes allow for flexibility of resizing system partitions on the fly without any downtime. This is a pretty nice feature to have in Guest OS since one does not have to worry too much about allocating the right amount of disk upfront. In a previous life, I have spent many a waking hour reinstalling and reconfiguring partitions offline using a gparted live CD. This is not only a time consuming and tedious process but also requires the host to be taken offline. In this post, I am going to do a quick walkthrough of how to extend a LVM partition on a Ubuntu VM by adding a virtual disk to a running VM.<\/p>\n<p>The first step is to add the disk to the VM; I use the C# client to manage my ESX VMs:<\/p>\n<p><a href=\"https:\/\/www.asrivas.me\/blog\/wp-content\/uploads\/VirtualDisk.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.asrivas.me\/blog\/wp-content\/uploads\/VirtualDisk-300x216.png\" alt=\"VirtualDisk\" width=\"300\" height=\"216\" class=\"alignnone size-medium wp-image-114\" srcset=\"https:\/\/www.asrivas.me\/blog\/wp-content\/uploads\/VirtualDisk-300x216.png 300w, https:\/\/www.asrivas.me\/blog\/wp-content\/uploads\/VirtualDisk-1024x738.png 1024w, https:\/\/www.asrivas.me\/blog\/wp-content\/uploads\/VirtualDisk.png 1297w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The disk will not be visible to the Guest OS directly; you may need to trigger a rescan of the devices. This is easy to do on Linux but it should be possible on any other Guest OS as well:<\/p>\n<p><code><br \/>\necho \"0 0 0\" > \/sys\/class\/scsi_host\/host0\/scan<br \/>\nfdisk -l<br \/>\n<\/code><br \/>\nNote that when I tried this on Ubuntu Server 16.04 LTS, it required a reboot to detect the disk. Once the disk is visible under \/dev, we can open the disk using cfdisk and create a partition on it. Lets also format it using ext4:<\/p>\n<p><code><br \/>\n mkfs.ext4 \/dev\/sdb1<br \/>\n<\/code><\/p>\n<p>where sdb1 is the new partition created on \/dev\/sdb. The next step is to create the physical volume for the partition and we can do this using the pvcreate command:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# pvcreate \/dev\/sdb1<br \/>\n  Physical volume \"\/dev\/sdb1\" successfully created<br \/>\n<\/code><\/p>\n<p>To extend a logical volume, we would first need to find out the name of the volume using the &#8220;vgdisplay&#8221; command. In the case of my setup, this is the output for &#8220;vgdisplay&#8221;:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# vgdisplay<br \/>\n  --- Volume group ---<br \/>\n  VG Name               mediaserver-vg<br \/>\n  System ID<br \/>\n  Format                lvm2<br \/>\n  Metadata Areas        1<br \/>\n  Metadata Sequence No  5<br \/>\n  VG Access             read\/write<br \/>\n...<br \/>\n...<br \/>\n<\/code><\/p>\n<p>Lets extend the mediaserver-vg volume using vgextend:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# vgextend mediaserver-vg \/dev\/sdb1<br \/>\n  Volume group \"mediaserver-vg\" successfully extended<br \/>\n<\/code><\/p>\n<p>Lets do a pvscan to check if everything looks good:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# pvscan<br \/>\n  PV \/dev\/sda5   VG mediaserver-vg   lvm2 [30.05 GiB \/ 324.00 MiB free]<br \/>\n  PV \/dev\/sdb1   VG mediaserver-vg   lvm2 [30.00 GiB \/ 30.00 GiB free]<br \/>\n  Total: 2 [60.05 GiB] \/ in use: 2 [60.05 GiB] \/ in no VG: 0 [0   ]<\/p>\n<p>root@mediaserver:~# vgdisplay<br \/>\n  --- Volume group ---<br \/>\n  VG Name               mediaserver-vg<br \/>\n  System ID<br \/>\n  Format                lvm2<br \/>\n  Metadata Areas        2<br \/>\n  Metadata Sequence No  6<br \/>\n  VG Access             read\/write<br \/>\n  VG Status             resizable<br \/>\n  MAX LV                0<br \/>\n  Cur LV                2<br \/>\n  Open LV               2<br \/>\n  Max PV                0<br \/>\n  Cur PV                2<br \/>\n  Act PV                2<br \/>\n  VG Size               60.05 GiB<br \/>\n...<br \/>\n...<\/p>\n<p><\/code><\/p>\n<p>Now that we have added a physical volume to the VG, we need to extend the logical volume partition which is running out of space. Information about the LVs can be displayed using the lvdisplay command:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# lvdisplay<br \/>\n  --- Logical volume ---<br \/>\n  LV Path                \/dev\/mediaserver-vg\/root<br \/>\n  LV Name                root<br \/>\n  VG Name                mediaserver-vg<br \/>\n  LV UUID                NDkzNB-kyPq-4qZV-bpTr-rY8z-zqXr-jwpZeH<br \/>\n  LV Write Access        read\/write<br \/>\n  LV Creation host, time ,<br \/>\n  LV Status              available<br \/>\n  # open                 1<br \/>\n  LV Size                28.74 GiB<\/p>\n<p><\/code><\/p>\n<p>Note that the LV size is only 28.74 GB and has not been extended by the extra 30GB that we added. We would need to use the &#8220;lvextend&#8221; command to do this:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# lvextend \/dev\/mediaserver-vg\/root \/dev\/sdb1<br \/>\n  Extending logical volume root to 58.73 GiB<br \/>\n  Logical volume root successfully resized<\/p>\n<p>root@mediaserver:~# lvdisplay<br \/>\n  --- Logical volume ---<br \/>\n  LV Path                \/dev\/mediaserver-vg\/root<br \/>\n  LV Name                root<br \/>\n  VG Name                mediaserver-vg<br \/>\n  LV UUID                NDkzNB-kyPq-4qZV-bpTr-rY8z-zqXr-jwpZeH<br \/>\n  LV Write Access        read\/write<br \/>\n  LV Creation host, time ,<br \/>\n  LV Status              available<br \/>\n  # open                 1<br \/>\n  LV Size                58.73 GiB<\/p>\n<p><\/code> <\/p>\n<p>So, we have extended our root partition volume to 58.73GB from 28.74GB (+30GB). The last step is to resize the filesystem to occupy the newly allocated space using resize2fs:<\/p>\n<p><code><br \/>\nroot@mediaserver:~# resize2fs \/dev\/mediaserver-vg\/root<br \/>\nresize2fs 1.42.9 (4-Feb-2014)<br \/>\nFilesystem at \/dev\/mediaserver-vg\/root is mounted on \/; on-line resizing required<br \/>\nold_desc_blocks = 2, new_desc_blocks = 4<br \/>\nThe filesystem on \/dev\/mediaserver-vg\/root is now 15396864 blocks long.<\/p>\n<p>root@mediaserver:~# df -h<br \/>\nFilesystem                        Size  Used Avail Use% Mounted on<br \/>\n\/dev\/mapper\/mediaserver--vg-root   58G   12G   44G  22% \/<\/p>\n<p><\/code><\/p>\n<p>There, we have extended the root partition of a running VM without any downtime \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logical volumes allow for flexibility of resizing system partitions on the fly without any downtime. This is a pretty nice feature to have in Guest OS since one does not have to worry too much about allocating the right amount of disk upfront. In a previous life, I have spent many a waking hour reinstalling &#8230; <span class=\"more\"><a class=\"more-link\" href=\"https:\/\/www.asrivas.me\/blog\/extending-a-lvm-partition-on-a-ubuntu-vm\/\">[Read more&#8230;]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts\/113"}],"collection":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":4,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asrivas.me\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}