Increasing partition size in Ubuntu on Hyper-V virtual machine

It happens, even very often, that the partition size is too small and additional space needs to be added. In Hyper-V this can be done with the option to add another disk and LVM extension.

The first step will be to add an additional disk to the virtual machine. This can be conveniently done from Hyper-V Manager and add an additional disk by setting its size to the size you want to enlarge the LVM. In this case, we will be increasing /var by 65 GB. So I add a disk of this capacity to the VM via SCSI Controller > selecting Hard Drive and selecting Add > New. Next, we choose the disk type, location and its size (as shown below)

Check size /var before adding new size

bnrsadm@BNRStest:~$ df -h
 Filesystem            Size  Used Avail Use% Mounted on
 tmpfs                 187M  948K  186M   1% /run
 /dev/mapper/vg0-root   15G  5.3G  8.8G  38% /
 tmpfs                 934M     0  934M   0% /dev/shm
 tmpfs                 5.0M     0  5.0M   0% /run/lock
 tmpfs                 4.0M     0  4.0M   0% /sys/fs/cgroup
 /dev/sda2             488M  157M  296M  35% /boot
 /dev/mapper/vg0-home  9.8G   37M  9.3G   1% /home
 /dev/mapper/vg0-var    35G  789M   32G   3% /var
 /dev/sda1             511M  7.9M  504M   2% /boot/efi
 tmpfs                 187M  4.0K  187M   1% /run/user/1000

After adding the disk to the machine, it should show up in the command listing fdisk -l

Disk /dev/sdb: 65 GiB, 69793218560 bytes, 136314880 sectors
 Disk model: Virtual Disk
 Units: sectors of 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes / 4096 bytes
 I/O size (minimum/optimal): 4096 bytes / 4096 bytes

The disk does not have a partition, we need to add a new one. This can be done like this:

root@BNRStest:/home/bnrsadm# fdisk /dev/sdb
 Welcome to fdisk (util-linux 2.36).
 Changes will remain in memory only, until you decide to write them.
 Be careful before using the write command.
 Device does not contain a recognized partition table.
 Created a new DOS disklabel with disk identifier 0x06d624ee.
 Command (m for help): n
 Partition type
    p   primary (0 primary, 0 extended, 4 free)
    e   extended (container for logical partitions)
 Select (default p): p
 Partition number (1-4, default 1):
 First sector (2048-136314879, default 2048):
 Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-136314879, default 136314879):
 Created a new partition 1 of type 'Linux' and of size 65 GiB.
 Command (m for help): w
 The partition table has been altered.
 Calling ioctl() to re-read partition table.
 Syncing disks.

Program should create /dev/sdb1. Next run fdisk -l confirms that.

Nextly, we modify LVM. Before operation show configuration

root@BNRStest:/home/bnrsadm# vgs
   VG  #PV #LV #SN Attr   VSize   VFree
   vg0   1   3   0 wz--n- <60.00g    0
 root@BNRStest:/home/bnrsadm# pvs
   PV         VG  Fmt  Attr PSize   PFree
   /dev/sda4  vg0 lvm2 a--  <60.00g    0
 root@BNRStest:/home/bnrsadm# lvs
   LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
   home vg0 -wi-ao----  10.00g
   root vg0 -wi-ao----  15.00g
   var  vg0 -wi-ao---- <35.00g

We add physical volume with new partition

root@BNRStest:/home/bnrsadm# pvcreate /dev/sdb1
   Physical volume "/dev/sdb1" successfully created.

Extend volume group

root@BNRStest:/home/bnrsadm# vgextend vg0 /dev/sdb1
   Volume group "vg0" successfully extended

Extend logical volume

root@BNRStest:/home/bnrsadm# lvextend /dev/mapper/vg0-var /dev/sdb1
   Size of logical volume vg0/var changed from <35.00 GiB (8959 extents) to 99.99 GiB (25598 extents).
   Logical volume vg0/var successfully resized.

Extend file system

root@BNRStest:/home/bnrsadm# resize2fs /dev/mapper/vg0-var
 resize2fs 1.45.6 (20-Mar-2020)
 Filesystem at /dev/mapper/vg0-var is mounted on /var; on-line resizing required
 old_desc_blocks = 5, new_desc_blocks = 13
 The filesystem on /dev/mapper/vg0-var is now 26212352 (4k) blocks long.

After that /var size should extend. df command confirms that

root@BNRStest:/home/bnrsadm# df -h
 Filesystem            Size  Used Avail Use% Mounted on
 tmpfs                 187M  972K  186M   1% /run
 /dev/mapper/vg0-root   15G  5.3G  8.8G  38% /
 tmpfs                 934M     0  934M   0% /dev/shm
 tmpfs                 5.0M     0  5.0M   0% /run/lock
 tmpfs                 4.0M     0  4.0M   0% /sys/fs/cgroup
 /dev/sda2             488M  157M  296M  35% /boot
 /dev/mapper/vg0-home  9.8G   37M  9.3G   1% /home
 /dev/mapper/vg0-var    99G  946M   93G   1% /var
 /dev/sda1             511M  7.9M  504M   2% /boot/efi
 tmpfs                 187M  4.0K  187M   1% /run/user/1000

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments