1

I need to produce customized VirtualBox VMs (shipped as .ova) for teaching: one is an Ubuntu server with terminal interface and LAMP, the other has a minimal desktop Ubuntu with only selected applications available. I need them to be lightweight because students use both of them for networking, and the quality of their BYOD computers varies.

In the past (18.04LTS), I used to download a minimal .iso and proceed incrementally. Now this is no more possible, since only the complete .iso are available (e.g. link) for 20.04. I do not want to proceed decrementally since the result is non-optimal, uncertain, and time-consuming.

I browsed the Web, but (quite surprisingly) without finding a solution for this. I suspect I missed something relevant, so I ask here before spending time to discover the boiling water.

Thank you for your help!

  • Take a look at Multipass. It's cross-platform, reasonably lightweight, and you can customize the install using cloud-init YAML. Instead of an OVA, you distribute the instructions and the cloud-init file. – user535733 Oct 17 '20 at 10:46
  • @user535733 Thank you, but it is more complex than I can afford. I need a moderately complex process to produce a VM that is really easy to use. – MastroGeppetto Oct 17 '20 at 14:03
  • Alternative: Hang onto a copy of the final netinstall .iso, use it, dist-upgrade to whatever release you want, then build your system. It's only one additional step. – user535733 Oct 17 '20 at 14:08
  • Does this answer your question? How to do a minimal install of 20.04? – karel Oct 17 '20 at 22:05
  • @user535733 mini.iso will not be available in the future, and it is presently legacy: see the second paragraph in my question.Thank you! – MastroGeppetto Oct 18 '20 at 15:59
  • @karel Thank you, but this link is indeed the name of the problem. For netbooting I should organize an infrastructure in my virtualbox network, dist-upgrade a VirtualBox VM does not return a reliable result (tested), cloud images are apparently not appropriate for my use case. – MastroGeppetto Oct 18 '20 at 15:59
  • 1
    You seem to have many (unstated) preferences after all. Consider updating your question to prevent unproductive suggestions. – user535733 Oct 18 '20 at 16:14

1 Answers1

3

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:

  • meta-data:

     instance-id: desktop
     local-hostname: vb-desktop
    
  • user-data:

     #cloud-config
     users:
       - name: user
         sudo: ['ALL=(ALL) NOPASSWD:ALL']
         shell: /bin/bash
    

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.