I have created a golden image of Ubuntu 20.04 on a 25mb virtual disk, with the partitions as follows:
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 26.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 512MB 511MB primary linux-swap(v1) boot
2 513MB 26.8GB 26.3GB extended
5 513MB 1012MB 499MB logical ext4
6 1013MB 16.0GB 15.0G logical ext4
7 16.0GB 26.8GB 10.8GB logical ext4
/dev/vda6
is /root
and /dev/vda7
is /home
.
This disk gets exported out as an image for virtual machines to be created from. I'm trying to create a script that will run on the first boot of a newly created virtual machine that will extend the home partition to use the available free space at the end of the disk, but I'm not getting the results I'm expecting.
I followed this suggestion and created the following files on the golden image disk before exporting:
/etc/rc.local
:
#!/bin/sh -e
type /etc/init.d/extend-home-part.sh && /etc/init.d/extend-home-part.sh
exit 0
/etc/init.d/extend-home-part.sh
:
#!/bin/sh
sudo growpart /dev/vda 7 && sudo resize2fs /dev/vda7
sudo mv /etc/init.d/extend-home-part.sh /etc/init.d/extended-home-part.sh
exit 0
After I created a virtual machine with a larger virtual disk, I can see that the script ran because the script has been renamed, but the /home partition hasn't changed. parted print free
gives this output:
(parted) print free
Model: Virtio Block Device (virtblk)
Disk: /dev/vda: 172GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1049kB 1016kB Free Space
1 1049kB 512MB 511MB primary linux-swap(v1) boot
512MB 513MB 1048kB Free Space
2 513MB 26.8GB 26.3GB extended
5 513MB 1012MB 499MB logical ext4
1012MB 1013MB 70kB Free Space
6 1013MB 16.0GB 15.0G logical ext4
16.0GB 16.0GB 639kB Free Space
7 16.0GB 26.8GB 10.8GB logical ext4
26.8GB 172GB 145GB Free Space
When I try to run the commands manually on the new virtual machine, I get this output:
$ sudo growpart /dev/vda 7
CHANGED: partition=7 start=31275008 old: size=21151744 end=52426752 new: size=304269279 end=335544287
$ sudo resize2fs /dev/vda7
resize2fs 1.45.5 (07-Jan-2020)
The filesystem is already 2643968 (4k) blocks long. Nothing to do!
The free space at the end still remains unused, and parted print free
output doesn't change. I don't understand why the /home
partition doesn't extend, even after manually running the commands.
What am I doing wrong here? Is there a better way to execute this task?
Note: I know it is possible to use the Disks utility in Ubuntu to resize the /home
partition, but we do not want virtual machine users having to do this every time they create a new Ubuntu VM. Automating this process is the necessity.