logo
vision scalability social networking revelation source

Virtual Box

abstract: Setting up various operating systems in VirtualBox, including Ubuntu, Devuan, Debian, OpenWrt, and Windows 10. Also covers virtual disk management.

1 Virtual Box

To build a cross platform application, you need to build in a cross platform environment.

If you face grief launching an installer for your virtual box device make sure the virtual network is bridged mode and get into the live cd command line.

sudo -i
apt-get update
apt-get install debian-installer-launcher
debian-installer-launcher --text

1.1 Setting up Ubuntu in VirtualBox

Having a whole lot of different versions of different machines, with a whole lot of snapshots, can suck up a remarkable amount of disk space mighty fast. Even if your virtual disk is quite small, your snapshots wind up eating a huge amount of space, so you really need some capacious disk drives. And you are not going to be able to back up all this enormous stuff, so you have to document how to recreate it.

Each snapshot that you intend to keep around long term needs to correspond to a documented path from install to that snapshot.

When creating a Virtual Box machine, make sure to set the network adapter to paravirtualization, set preferences in the file menu, the virtual hard disk, and the snapshot directory to the desired location.  Virtual hard disk location selection is done when creating it, snapshot directory is done in settings/general/advanced (which also allow you to do clipboard sharing).

apt-get -qy update && apt-get -qy upgrade
# Fetches the list of available updates and
 # Strictly upgrades the current packages

To install guest additions, thus allow full communication between host and virtual machine, update Ubuntu, then while Ubuntu is running, simulate placing the guest additions CD in the simulated optical drive. Then Ubuntu will correctly activate and run the guest additions install.

Installing guest additions frequently runs into trouble. Debian especially tends to have security in place to stop random people from sticking in CDs that get root access to the OS to run code to amend the OS in ways the developers did not anticipate.

1.2 Setting up Devuan in virtual box

Devuan is the same as Debian, except not woke, uses sysinit instead of systemd. I wanted to use it for those reasons, but:

The Devuan netinstall does not boot correctly on an EFT UEFI system, nor can it format it or partition it. Nor can it recognize an an existing format that has been setup for it in advance.

Some Devuan installers recognize that, but then throw you into the deep end to install the bootloader yourself.

Devuan can only be installed on EFT UEFI system semi manually, and everyone who tries it gives up.

The grub package for UEFI is apt install grub-efi-amd64 which no Devuan installer plays nice with. Even the ones that are UEFI aware are not aware of how to install it.

One can manage to get Devuan booting on EFI, but it is not worth it.

To do in on oracle vm, copy a debian vm (so that the boot variables are setup to boot efi/debian/grub, because adding a boot option in the bios boot manager does not work in an oracle vm)

Then in a live debian cd with the source and target virtual disks attached, the source being non uefi devuan machine

Change the uuids of the newly created target system

copy the files from the working mbr install of devuan. (using rsync -a as root so that you do not screw up the user ownership of the files)

mount -m /dev/sdb2 /mnt/source
mount -m /dev/sdc2 /mnt/destination
mkdir -p /mnt/destination/boot/efi
mount /dev/sdc1 /mnt/destination/boot/efi
rsync -a /mnt/source/* /mnt/destination
# check if /mnt/destination/boot/grub/x86_64-efi or similar thing with efi label exists
# if it does not
cp /boot/grub/x86_64-efi /mnt/destination/boot/grub
for i in /sys /proc /dev /dev/pts /dev/shm; do sudo mount -B $i /mnt/destination$i; done
sudo mount --bind /dev /mnt/destination/dev
sudo mount --bind /dev/pts /mnt/destination/dev/pts
sudo mount --bind /proc /mnt/destination/proc
sudo mount --bind /sys /mnt/destination/sys
chroot /mnt/destination/
# make fstab look like the UEFI fstab of a regular debian UEFI install
# edit the UUIDs in fstab, and provide the needed mount points
# Reinstall GRUB within the chroot environment:
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian
update-grub
# fails because of missing grub efi directory tree
# copy the missing grub directory that you mucked up the first time
# over from your live cd
# and do another grub install.

Don’t do any of that. It is too tiresome and the fact that you have to do it should tell you that devuan cannot keep up. If I can install on a UEFI system why cannot someone write an installer that can do what I did?

1.3 Setting up Debian in VirtualBox

1.3.1 Initial installation from iso

Login as root and manually type in:

apt update
apt full-upgrade
apt install sudo avahi-daemon
visudo # turn off password for sudo
# give the user sudo
usermod -a -G sudo «username»

if visudo is configured for the wrong editor, you should have set .bashrc to have the line export EDITOR=/usr/bin/nano in ~/bashrc

Then you can login through ssh, sudo to root, and fix ssh and all that using cut and paste.

And then, once you can login through public keys, install the desktop if desired using apt -y install task-mate-desktop

This installs x-windows and all that, all that is needed to bring up mate at boot. Whereas apt install mate-desktop-environment only installs the desktop, whichall that xrdp needs, since xrdp provides an x windows server and all that.

1.3.2 virtual box Debian install bug

install fails when installing Debian 12 UEFI

Edit the Grub boot entry when booting off the install ISO. 1. Highlight the “Install” option 2. Press “e” to edit the Grub entry 3. Add fb=false before the --- parameter Should read something like linux /install.amd/vmlinuz vga=788 fb=false --- quiet 4. Press F10 or Ctrl+X to boot

1.3.3 server in virtual box

If it is a server and you are using nfs, don’t need guest additions and therefore do not need module-module assistant, and may not need the build stuff.

sudo -i
apt-get -qy update
apt-get -qy full-upgrade
apt-get -qy install dnsutils curl sudo dialog rsync zstd avahi-daemon nfs-common

To access disks on the real machine, create the empty directory «/mytarget» directory and add the line

«my-nfs-server»:/«my-nfs-subdirectory»  «/mytarget»  nfs4  defaults  0  0

to /etc/fstab

to test that it works without rebooting: mount «/mytarget» as root

1.3.4 Guest Additions

If you are running it through your local machine, you want to bring up the gui and possibly the disk access through guest additions

To install guest additions on Debian:

sudo -i
apt-get -qy update && apt-get -qy install build-essential module-assistant
apt-get -qy install git dnsutils curl sudo dialog rsync zstd avahi-daemon nfs-common
apt-get -qy full-upgrade
m-a -qi prepare
apt install linux-headers-$(uname -r)
apt autoremove -qy
mount  /media/cdrom0
cd /media/cdrom0 && sh ./VBoxLinuxAdditions.run
usermod -a -G vboxsf «username»

You will need to do another m-a prepare and to reinstall it after a apt-get -qy dist-upgrade. Sometimes you need to do this after a mere upgrade to Debian or to Guest Additions. Every now and then, guest additions gets mysteriously broken on Debian, due to automatic operating system updates in the background, the system will not shut down correctly, and guest additions has to be reinstalled with a shutdown -r. Or copy and paste mysteriously stops working.

1.3.4.1 Set the default display size for guest additions

On windows using Oracle vm

/c/Program\ Files/oracle/VirtualBox/VBoxManage setextradata «vboxname» GUI/LastGuestSizeHint 2176,1360

To switch between windowed mode and fullscreen, host+F

1.3.5 Updating kernel and headers

When you install guest additions or wireguard, you are recompiling and need the appropriate kernel headers. Which may not be available if you do not have the latest kernel. Which you frequently do not.

apt-get -qy full-upgrade normally makes this problem go away.

a normal upgrade may leave your kernel out of dae.

To get the latest kernel headers manually

 apt install linux-headers-$(uname -r)

Suppose it is not found. Probably because your kernel is out of date.

Suppose you want to manually update it, perhaps because it stubbornly remains strangely out of date

To find what is the latest kernel apt-cache search linux-image, and then install the selected image with apt install «linux-image-6.1.0-25-amd64»

After that, full upgrade, because updating the kernel is likely to mysteriously start working again.

1.3.6 auto gui login

To set automatic login on lightdm-mate

nano /etc/lightdm/lightdm.conf

In the [Seat:*] section of the configuration file (there is another section of this configuration file where these changes have no apparent effect) edit

#autologin-guest=false
#autologin-user=user
#autologin-user-timeout=0

to

autologin-guest=false
autologin-user=«username»
autologin-user-timeout=0

1.3.7 grub timeout

nano /etc/default/grub

The full configuration built by update-grub is built from the file /etc/default/grub, the file /etc/fstab, and the files in /etc/grub.d/.

Among the generated files, the key file is menu.cfg, which will contain a boot entry for any additional disk containing a linux kernel that you have attached to the system. You might then be able to boot into that other linux, and recreate its configuration files within it.

1.3.8 autostart preferred programs

To set things to autostart on gui login under Mate and KDE Plasma create the directory ~/.config/autostart and copy the appropriate *.desktop files into it from /usr/share/applications or ~/.local/share/applications.

1.3.9 Don’t let the screen saver log you out.

On Debian lightdm mate go to system/ control center/ Look and Feel/ Screensaver and turn off the screensaver screen lock

Go to go to system / control center/ Hardware/ Power Management and turn off the computer and screen sleep.

1.3.10 setup ssh server

In the shared directory, I have a copy of /etc and ~.ssh ready to roll, so I just go into the shared directory copy them over, chmod .ssh and reboot.

Alternatively manually set them then

chmod 700 ~/.ssh && chmod 600 ~/.ssh/*

1.3.11 make the name available

You can manually edit the hosts file, or the .ssh/config file, which is a pain if you have a lot of machines, or fix your router to hand out names, which cheap routers do not do and every router is different.

Or, if it is networked in virtual box bridged mode,

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install avahi-daemon

Which daemon will multicast the name and IP address to every machine on the network so that you can access it as «name».local

1.3.12 Set the hostname

check the hostname and dns domain name with

hostname && domainname -s && hostnamectl status

And if need be, set them with

fn=«example.com»
domainname -b $fn
hostnamectl set-hostname $fn

Your /etc/hosts file should contain

127.0.0.1       localhost
127.0.0.1       «example.com»
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

To change the host ssh key, so that different hosts have different hostnames after I copied everything to a new instance:

cd /etc/ssh
cat sshd* |grep HostKey
#Make sure that `/etc/ssh/sshd_config` has the line
#     HostKey /etc/ssh/ssh_host_ed25519_key
rm -v ssh_host*
ssh-keygen -t ed25519 -f  /etc/ssh/ssh_host_ed25519_key

Note that visual studio remote compile requires an ecdsa-sha2-nistp256 key on the host machine that it is remote compiling for. If it is nist, it is backdoored

To change the snake oil certificate created and used by xrdp:

make-ssl-cert generate-default-snakeoil --force-overwrite

1.3.13 .bashrc

If the host has a domain name, the default in /etc/bash.bashrc will not display it in full at the prompt, which can lead to you being confused about which host on the internet you are commanding.

nano /etc/bash.bashrc

Change the lower case h in PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' to an upper case H

  PS1='${debian_chroot:+($debian_chroot)}\u@\H:\w\$ '

I also like the bash aliases:

alias ll="ls --color=auto -hal --time-style=iso"
mkcd() { mkdir -p "$1" && cd "$1"; }

Setting them in /etc/bash.bashrc sets them for all users, including root. But the default ~/.bashrc is apt to override the change of H for h in PS1

1.3.14 fstab

The line for in fstab for optical disks needs to given the options udf,iso9660 ro,users,auto,nofail so that it automounts, and any user can eject it.

Confusingly, nofail means that it is allowed to fail, which of course it will if there is nothing in the optical drive. If you have auto but not nofail the system will not boot into multi-user let along gui unless there is something in the drive. You get dropped into single user root logon (where you will see an error message regarding the offending drive and can edit the offending fstab).

user,noauto means that the user has to mount it, and only the user that mounted it can unmount it. user,auto is likely to result in root mounting it, and if root mounted it, as it probably did, you have a problem. Which problem is fixed by saying users instead of user

1.4 Setting up Ubuntu in Virtual box

The same as for Debian, except that the desktop addition lacks openssh-server, it already has avahi-daemon to make the name available, and the install program will setup auto login for you.

sudo apt install openssh-server.

Then ssh in

1.4.1 Guest Additions

sudo -i
apt-get -qy update && apt-get -qy install build-essential dkms
apt-get -qy install git dnsutils curl sudo dialog rsync zstd
apt-get -qy full-upgrade
apt autoremove -qy

Then you click on the autorun.sh in the cdrom through the gui.

usermod -a -G vboxsf «username»

1.5 Setting up OpenWrt in VirtualBox

OpenWrt is a router, and needs a network to route. So you use it to route a virtual box internal network.

Ignore the instructions on the OpenWrt website for setting up in Virtual Box. Those instructions are wrong and do not work. Kind of obvious that they are not going to work, since they do not provide for connecting to an internal network that would need its own router. They suffer from a basic lack of direction, purpose, and intent.

Download the appropriate gzipped image file, expand it to an image file, and convert to a vdi file.

You need an x86 64 bit version of OpenWrt. There are four versions of them, squashed and not squashed, efi and not efi. Not efi is more likely to work and not squashed is more likely to work, but only squashed supports automatic updates of the kernel.

In git bash terminal

gzip -d openwrt-*.img.gz
/c/"Program Files"/Oracle/VirtualBox/VBoxManage convertfromraw --format VDI openwrt-22.03.3-x86-64-generic-ext4-combined.img openwrt-generic-ext4-combined.vdi

Add the vdi to oracle media using the oracle media manager.

The resulting vdi file may have things wrong with it that would prevent it from booting, but viewing it in gparted will normalize it.

Create a virtual computer, name openwrt, type linux, version Linux 2.6, 3.x, 4.x, 5.x (64 bit) The first network adaptor in it should be internal, the second one should be NAT or bridged/

Boot up openwrt headless, and any virtual machine on the internal network should just work. From any virtual machine on the internal network, configure the router at http://192.168.1.1

1.6 Virtual disks

The first virtual disk attached to a virtual machine is /dev/sda, the second is /dev/sdb, and so on and so forth.

This does not necessarily correspond to order in which virtual drives have been attached to the virtual machine

Be warned that the debian setup, when it encounters multiple partitions that have the same UUID (because one system was cloned from the other) is apt to make seemingly random decisions as to which partitions to mount to what. So, you should boot from a live cd-rom, and attach the system to be manipulated to that.

This also protects you from accidentally manipulating the wrong system.

The resulting repaired system may nonetheless take a strangely long time to boot, because it is trying to resume a suspended linux, which may not be supported on your device.

boot-repair and update-initramfs make a wild assed guess that if it sees what looks like a swap partition, it is probably on a laptop that supports suspend/resume. If this guess is wrong, you are in trouble.

If it is not supported this leads to a strangely long boot delay while grub waits for the resume data that was stored to the swap file:

#to fix long waits to resume a nonexistent suspend
sudo -i
nano /etc/initramfs-tools/conf.d/resume
RESUME=none
update-initramfs -u
shutdown -r now

If you have a separate boot partition in an efisystem then the grub.cfg in /boot/efi/EFI/debian (not to be confused with all the other grub.cfgs) should look like

search.fs_uuid «8943ba15-8939-4bca-ae3d-92534cc937c3» boot hd0,gpt«4»
set prefix=($boot)'/grub'
configfile $prefix/grub.cfg

Where the «funny brackets», as always, indicate mutas mutandis.

Should you dig all the way down to the efi boot menu, which boots grub, which then boots the real grub, the device identifier used corresponds to the PARTUUID in

lsblk -o name,type,size,fstype,mountpoint,UUID,PARTUUID while linux uses the UUID.

This will display something like

NAME   TYPE  SIZE FSTYPE MOUNTPOINT UUID                                 PARTUUID
sda    disk   20G
├─sda1 part  512M vfat   /boot/efi  2444-76B3                            797edd50-0c57-45f4-8220-dc485aeeab17
└─sda2 part 18.5G ext4   /          1b05149d-ca76-4ddf-ab14-6af124c513c2 ca01668d-f5ff-4e69-9b5d-536fc136a967
sdb    disk    5G
└─sdb1 part    5G swap   [SWAP]     f3f1f1fb-d972-4902-814c-a407f23e6d42 df5f1bd6-01
sdc    disk   17G
└─sdc1 part   17G swap              b25ff239-9e79-4db0-991c-2c5a3e49c172 adc1538f-a06e-4954-b52c-7453dc547898
sr0    rom  1024M

If you attach two virtual disks representing two different linux systems,with the same UUIDs to the same sata controller while powered down, big surprise is likely on powering up. Attaching one of them to virtio will evade this problem.

If you amend file system UUID’s referenced in fstab and boot, have to amend /etc/fstab and /boot/efi/EFI/debian/grub.cfg, then rerun update-grub.

But a better solution is to change all the UUIDs, since every piece of software expects them to be unique, and edit /etc/fstab accordingly. Which will probably stop grub from booting your system, because in grub.cfg it is searching for the /boot or / by UUID.

However, sometimes one can add one additional virtual disk to a sata controller after the system has powered up, which will produce no surprises, for the disk will be attached but not mounted.

So cheerfully attaching one linux disk to another linux system so that you can manipulate one system with the other may well have surprising, unexpected, and highly undesirable results.

What decisions it has in fact made are revealed by lsblk

If one wants to add a several attached disks without surprises, then while the virtual machines is powered down, attach the virtio-scsis controller, and a bunch of virtual hard disks to it. The machine will then boot up with only the sata disk mounted, as one would expect, but the disks attached to the virtio controller will get attached as the ids /dev/sda, /dev/sdb, /dev/sdc/, etc, while the sata disk gets mounted, but surprisingly gets the last id, rather than the first.

After one does what is needful, power down and detach the hard disks, for if a hard disk is attached to multiple systems, unpleasant suprises are likely to ensue.

So when you attach a foreign linux disk by sata to another linux system, attach after it has booted, and detach before you shutdown, to ensure predictable and expected behavior.

This however only seems to work with efi sata drives, so one can only attach one additional disk after it has booted.

Dynamic virtual disks in virtual box can be resized, and copied to a different (larger size)

Confusingly, the documentation and the UI does not distinguish between dynamic and fixed sized virtual disks - so the UI to change a fixed sized disks size, or to copy it to a disk of different size is there, but has absolutely no effect.

Having changed the virtual disk size in the host system, you then want to change the partition sizes using gparted, which requires the virtual disk to be attached, but not mounted, to another guest virtual machine in which you will run gparted.

Over time, dynamic virtual disks occupy more and more physical storage, because more and more sectors become non zero, even though unused.

You attach the virtual disk that you want to shrink to another guest OS as /dev/sdb, which is attached but not mounted, and, in the other guest OS zerofree /dev/sdb1 which will zero the free space on partition 1. (And similarly for any other linux file system partitions)

You run zerofree, like gparted, in another in a guest OS, that is mounted on /dev/sda while the disk whose partitions you are zeroing is attached, but not mounted, as /dev/sdb1.

You can then shrink it in the host OS with

VBoxManage modifyhd -compact thediskfile.vdi

or make a copy that will be smaller than the original.

To resize a fixed sized disk you have to make a dynamic copy, then run gparted (on the other guest OS, you don’t want to muck with a mounted file system using gparted, it is dangerous and broken) to shrink the partitions if you intend to shrink the virtual disk, resize the dynamic copy in the host OS, then, if you expanded the virtual disk run gparted to expand the partitions.

To modify the size of a guest operating system virtual disk, you need that OS not running, and two other operating systems, the host system and a second guest operating system. You attach, but not mount, the disk to a second guest operating system so that you can run zerofree and gparted in that guest OS.

And now that you have a dynamic disk that is a different size, you can create a fixed size copy of it using virtual media manager in the host system. This, however, is an impractically slow and inefficient process for any large disk. For a one terabyte disk, takes a couple of days, a day or so to initialize the new virtual disk, during which the progress meter shows zero progress, and another day or so to do actually do the copy, during which the progress meter very slowly increases.

Cloning a fixed sized disk is quite fast, and a quite reasonable way of backing stuff up.

To list block devices lsblk -o name,type,size,fsuse%,fstype,fsver,mountpoint,UUID.

To mount an attached disk, create an empty directory, normally under mnt, and mount /dev/sdb3 /mnt/newvm

For example:

root@example.com:~#lsblk -o name,type,size,fsuse%,fstype,fsver,mountpoint,UUID
NAME   TYPE SIZE UUID                                 FSTYPE MOUNTPOINT
sda    disk 20G
├─sda1 part 33M  E470-C4BA                            vfat   /boot/efi
├─sda2 part  3G  764b1b37-c66f-4552-b2b6-0d48196198d7 swap   [SWAP]
└─sda3 part 17G  efd3621c-63a4-4728-b7dd-747527f107c0 ext4   /
sdb    disk 20G
├─sdb1 part 33M  E470-C4BA                            vfat
├─sdb2 part  3G  764b1b37-c66f-4552-b2b6-0d48196198d7 swap
└─sdb3 part1 7G  efd3621c-63a4-4728-b7dd-747527f107c0 ext4
sr0    rom  1024M
root@example.com:~# mkdir -p /mnt/sdb2
root@example.com:~# mount /dev/sdb2 /mnt/sdb2
root@example.com:~# ls -hal /mnt/sdb2
drwxr-xr-x  20 root root 4.0K Dec 12 06:55 .
drwxr-xr-x   5 root root 4.0K Dec 20 16:02 ..
drwxr-xr-x   4 root root 4.0K Dec 12 06:27 dev
drwxr-xr-x 119 root root 4.0K Dec 20 12:58 etc
drwxr-xr-x   3 root root 4.0K Dec 12 06:32 home
drwxr-xr-x   3 root root 4.0K Dec 12 06:27 media
drwxr-xr-x   2 root root 4.0K Dec 12 06:27 mnt
drwxr-xr-x  11 root root 4.0K Dec 12 06:27 var

when backing up from one virtual hard drive to another very similar one, mount the source disk with mount -r

We are not worried about permissions and symlinks, so use rsync -rcv --inplace --append-verify

If worried about permissions and symlinks rsync -acv --inplace --append-verify

There is some horrid bug with rsync -acv --inplace --append-verify that makes it excruciatingly slow if you are copying a lot of data.

cp -vuxr «source-dir»/«.bit*» «dest-dir» should have similar effect, but perhaps considerably faster, but it checks only the times, which may be disastrous if you have been using your backup live any time after you used the master live. After backing up, run your backup live once briefly, before using the backed up master, then never again till the next backup.

1.7 Windows 10 in virtual box

Install, as always, with no internet connected, so that you get the UI to create a local user with no password.

Remove bloat and disable spyware with the tool provided by https://github.com/christopherhowe02/Debloat10

Creative Commons License reaction.la gpg key 154588427F2709CD9D7146B01C99BB982002C39F
This work is licensed under the Creative Commons Attribution 4.0 International License.