A solution using cloud images
My problem is the creation of a custom Ubuntu VirtualBox VM starting from a minimal one. The result should consist of a VM running in a local VirtualBox installation (not in the cloud).
This task was formerly carried out using the minimal install mini.iso. But this disk image is now legacy, although still unofficially available.
I wanted a well-documented, long-term solution, possibly based on official releases. Browsing the Internet I found various suggestions, but not a complete solution. Below is a step-by-step guide that is based on such suggestions.
The solution may explain why the "mini.iso" based one is going to be dismissed. In fact the new one is faster to execute and returns a VM that is probably better configured for the use in a VirtualBox environment. In addition, the process might be completely automated (see this or this), while the legacy one requires a manual installation taking a long time.
The solution starts from a cloud image, as suggested in one of the posts on the subject (How to do a minimal install of 20-04).
I divided the process, which is designed for a Linux box, into steps:
- install the cloud image
- configure the VM
- configure the cloud image
- define user credentials
- prepare for customization
I also give some untested suggestions for its use in a Windows environment.
Install the cloud image
Cloud images are available from several sources. This guide applies to those on the official Ubuntu site, which offers various flavors. I have selected the 20.04 in OVA format, which is easy to use in VirtualBox (link). Editing the URL you find other formats and the repo.
Download the image, and create the VM using File -> Import virtual application in the VirtualBox GUI, without launching it. It may be convenient to change the name in the description (double click on the description line) with a simpler one, like ''minimal-focal''.
Configure the VM
Networking
In the network configuration of the Virtual Machine, configure the network adapter as "NAT", and (in the "Advanced" box) redirect host port 2222 (or whichever you like) to 22. In this way, your VM will be connected to the Internet and accessible through ssh.
Storage
In the Storage configuration of the VM:
- Remove the useless floppy driver
- Create a new DVD drive and leave it empty
Configure the cloud image
Create a .iso image containing the files that cloud-init will use when the VM boots. In a convenient place, create the following two YAML files:
Here you have the option to configure two details in the VM: the hostname (here the ''vb-desktop'' value for the ''local-hostname'' field in the meta-data file) and the username (here the ''user'' value for the ''name'' field in the user-data file). Check the manual for more details.
Next, create the .iso file (e.g. ciconf.iso) with the command
$ cloud-localds ciconf.iso user-data meta-data
The ''cloud-localds'' command is in the "cloud-image-utils" package, that (in Debian/Ubuntu) you install with
$ sudo apt install cloud-image-utils
The same functions should be available also for Windows users.
In the Storage section of the VirtualBox GUI install the .iso file you just created in the IDE drive you created in the previous step.
Launch the VM and wait for the terminal login, then "shutdown ACPI". You cannot log in since you do not have credentials.
Define user credentials
Move to the directory of the VM virtual disk (might be ~/VirtualBox VMs/minimal-focal), and mount the disk image. The qemu package is needed, which is installed with
$ sudo apt install qemu
To install the disk image proceed as follows:
$ sudo modprobe nbd max_part=8
$ sudo qemu-nbd -c /dev/nbd0 ubuntu-focal-20.04-cloudimg.vdi
$ sudo mount /dev/nbd0p1 /mnt
The ''ubuntu-focal-20.04-cloudimg.vdi'' refers to the image file, replace with your own. In the end, the VM filesystem is available in the /mnt directory.
To create the user credential ''chroot'' to the VM filesystem, change the user credentials (the ''passwd'' command will prompt you for a new password), and exit the ''chroot'' environment:
$ sudo chroot /mnt
$ passwd user
$ sync
$ exit
Replace ''user'' with the username configured in the previous step. There are limited chances to mess-up your system using the chroot command, so be careful with it. If you do not know about it, please check the man page.
Only when you have safely left the ''chroot'' environment (double check), copy your public key (I assume you have one), inside the VM filesystem:
$ mkdir /mnt/home/user/.ssh
$ cat ~/.ssh/id_rsa.pub >> /mnt/home/user/.ssh/authorized_keys
The above step is optional but extremely convenient. In case you do not have a public key there are thousands of tutorials for this (''ssh-keygen'').
When done, unmount the VM disk image:
$ sync
$ sudo umount /dev/nbd0p1
$ sudo qemu-nbd -d /dev/nbd0
$ sudo rmmod nbd
If you forget to unmount, the VM disk is damaged and you need to restart the whole thing. Double-check before proceeding.
Prepare for customization
Start the machine from the VirtualBox GUI interface and log in using the ''user'' username and the defined password.
The VM is accessible also from the command-line in your PC with
$ ssh user@localhost -p 2222
(''user'' replaced with your username, 2222 is the port number configured in the first step).
Then use the way you like (I suggest ''ssh'') to finish the work.
Log in the VM and, using a line editor, fix a missing detail in the ''/etc/hosts file'' in the VM: add at the end of the ''localhost'' line the name of the new machine, in the example ''minimal-focal''.
Next give the following commands:
$ sudo dpkg-reconfigure keyboard-configuration
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install linux-modules-extra-$(uname -r) tasksel
The first command adapts the VM keyboard to the one you use, while the rest updates the configuration, installs a bundle package containing modules that are missing in a cloud image and the tasksel utility that is useful for customization.
Final check and conclusions
Using the ''tasksel'' command you can install various kinds of bundles, e.g. a LAMP server. But you may decide to install your favorite collection of packages, just like with the mini.iso.
The process described above is conceptually more difficult than that carried out with the mini.iso, but it is faster, taking around 10' to have the machine ready for customization. In contrast, you need to download a VDI which is larger than the mini.iso. Finally, the cloud technology is here to stay, so I hope that this modality will be available for several Ubuntu releases after the mini.iso is finally abandoned.
The final check may consist of installing a desktop distribution, like Xubuntu, using tasksel. The test is significant to check if a display manager can operate correctly. To run the check, clone the VM and call ''tasksel'' from the VM terminal. Next select (with ''space'') the "Minimal Xubuntu desktop". Wait until the installation completes and reboot.
Windows users should be able to use the same strategy, but the steps that require mounting the VM filesystem and the chroot command are carried out using specific fields in the user-data file.