If you want to do this without GUI tools, you can use parted
.
On my system, I was expanding the root filesystem (which was on dev/sda3
) from 128GB to 160GB. Adjust the examples below depending on your setup. All the commands must be run as root (sudo -i
to login as root).
Beforehand, I was at 95% usage:
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv 124G 113G 6.3G 95% /
With Gen2 virtual machines, everything can be done online, while the guest OS is running, and without rebooting. Each step is almost instantaneous.
First, expand the virtual disk using the Hyper-V GUI or CLI tools (easiest way is to open the VM's settings, select the hard drive under the SCSI controller, then choose "Edit" then "Expand").
Next, you need to tell Ubuntu to rescan the block device, in this case sda
:
# echo 1 > /sys/block/sda/device/rescan
Next, run parted
and choose p
for print
. It should recognize that the partition table isn't using the full space and ask whether you want to fix it, so choose Fix
at this point.
# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an
extra 69206016 blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 172GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 538MB 537MB fat32 boot, esp
2 538MB 1612MB 1074MB ext4
3 1612MB 136GB 135GB
Now you need to actually expand the partition. While still in parted
, enter the resizepart
command to accomplish this. In my case, since I'm expanding sda3
I tell it to resize partition 3 to 100% of available space. Again, adjust the command for your setup.
(parted) resizepart 3 100%
(parted) p
Model: Msft Virtual Disk (scsi)
Disk /dev/sda: 172GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 538MB 537MB fat32 boot, esp
2 538MB 1612MB 1074MB ext4
3 1612MB 172GB 170GB
(parted) quit
Type quit
to exit parted
.
If you're using LVM, you now need to tell it to expand the physical volume. pvs
shows the info on your physical volumes; use pvresize
to expand it. With no parameters given, it will use all available space.
# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <125.50g 0
# pvresize /dev/sda3
Physical volume "/dev/sda3" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
# pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <158.50g 33.00g
Now, you need to expand the logical volume, as well as the filesystem itself. lvextend
with the -r
switch will do both at once, assuming you're using a filesystem it knows about such as ext3/4.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- <125.50g
# lvextend -l +100%FREE -r /dev/mapper/ubuntu--vg-ubuntu--lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <125.50 GiB (32127 extents) to <158.50 GiB (40575 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 16, new_desc_blocks = 20
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 41548800 (4k) blocks long.
That's it! Now verify that the filesystem sees the extra space:
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv 156G 113G 38G 76% /
If you're not using LVM, you can replace the pvresize
/lvextend
steps with resize2fs
instead. A simple resize2fs /dev/sda3
will tell the filesystem to use all available space on the newly-increased partition.
This article on Teknophiles has some more details on this process along with screenshots of the Hyper-V tools. (Update Dec 2021: apparently teknophiles.com is now defunct; updated link goes to an archive.org snapshot of the site.)