Install Ubuntu 20.04 as detailed here. (Guest VM on KVM infra)
Hard disk size: Based on trial and error identified that it would be better to have the base disk size as 3.5G
Set default password for ‘root; account.
sudo passwd
Disable consistent device naming – Network interface names are same always (legacy – eth0, eth1, etc.,)
Edit /etc/default/grub and update as shown below.
# If you change this file, run ‘update-grub’ afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n ‘Simple configuration’
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=”maybe-ubiquity”
GRUB_CMDLINE_LINUX=”net.ifnames=0 biosdevname=0″
Update grub – so that the same gets applied during the next boot
sudo update-grub
Since the interface name during the next boot will be ‘eth0’ for the primary interface, edit /etc/network/interfaces to reflect the same. Also, let the configuration be DHCP-based and not static IP. We will be using the VM image as the base image.
source-directory /etc/network/interfaces.d
auto eth0
iface eth0 inet dhcp
Since DHCP is set, it could take up to five minutes of time waiting for DHCP requests to be honored. Let us configure 10 seconds as DHCP timeout. Edit /etc/dhcp/dhclient.conf and update the timeout value as shown below.
# Configuration file for /sbin/dhclient.
#
# This is a sample configuration file for dhclient. See dhclient.conf's
...
...
#prepend domain-name-servers 127.0.0.1;
#require subnet-mask, domain-name-servers;
timeout 10;
Reboot and verify network configuration.
root@devsys:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:5d:e2:7e brd ff:ff:ff:ff:ff:ff
inet 10.0.0.233/16 brd 10.0.255.255 scope global dynamic eth0
valid_lft 2591872sec preferred_lft 2591872sec
inet6 fe80::5054:ff:fe5d:e27e/64 scope link
valid_lft forever preferred_lft forever
root@devsys:~#
Create a file /usr/local/bin/resizedisk with following content. Typically this has to be invoked as the first boot command to resize the disk partition.
#!/bin/bash
disk=`fdisk -l | grep “Disk \/dev\/” | cut -d “:” -f1 | cut -d ” ” -f2`
partition=`fdisk -l | tail -n1 | cut -d” ” -f1`
partN=$(echo $partition | tail -c 2)
# fdisk: delete and recreate the last partition with the largest size possible.
(
echo d # Delete partition
echo $partN # Last partition
echo n # Add a new partition
echo p # Primary partition
echo $partN # Last partition
echo # First sector (Accept default: 1)
echo # Last sector (Accept default: varies)
echo w # Write changes
) | fdisk $disk
# update filesystem to match new partition size
resize2fs $partition
Add execute permission to the script
chmod +x /usr/local/bin/resizedisk
Finally, clear the history and shut down the VM. The VM image in /var/lib/libvirt/images will be the base image.
#history -c
#shutdown -h now
On the host make a copy of the VM image to a folder, customize the image with first-boot and convert qcow to vmdk
root@s1:~# mkdir base
root@s1:~# cd base/
root@s1:~/base# cp /var/lib/libvirt/images/baseimage.qcow2 .
Add first boot command to resize the disk partition.
root@s1:~/base# virt-customize -a baseimage.qcow2 --firstboot-command '/usr/local/bin/resizedisk'
[ 0.0] Examining the guest ...
[ 3.6] Setting a random seed
[ 3.7] Installing firstboot command: /usr/local/bin/resizedisk
[ 3.8] Finishing off
Convert to VMDK
root@s1:~/base# qemu-img convert -f qcow2 -O vmdk baseimage.qcow2 baseimage.vmdk
Note: Instruction to users making use of the VMDK file. To resize the image to the required size on the ESX host
vmkfstools -X 50G <clonedbaseimage.vmdk>
where 50G is the size required.