Virtual machine made easy

Virtual machine made easy

ubuntu 18.04 hw2018 server vm

In this post, I’ll present how I deal with my virtual machines on a server an access them remotely.

Setup - server side

In the post about the network configuration, a virtual bridge named br0 has been configured, especially for the virtual machines. A virtual machine connected to this brige will have a connection to the network equivalent to the server connection, and will be seen by the network as another machine on the same interface(s) as the server.

  • Install the required packages:
    cli@server:~$ sudo apt-get install libvirt-bin
  • Make sure your user is in the libvirt group:
    sudo usermod -aG libvirt cli
    sudo usermod -aG libvirt local
  • ubuntu 16.04 and 18.04 kernels have nesting (running vm inside vm) enabled by default, for other distributions, nesting can be enable like this test:
    # If you have an Intel CPU, use this:
    $ cat /etc/modprobe.d/kvm_intel.conf
    options kvm-intel nested=Y
    # If you have an AMD CPU, then this:
    $ cat /etc/modprobe.d/kvm_amd.conf
    options kvm-amd nested=1

    Virtual disks

  • A pool for storing virtual machines already exists in /var/lib/libvirt/images

  • Setup a pool for boot disks
    virsh pool-define-as boot --type dir --target /var/lib/libvirt/boot
    virsh pool-autostart boot
    virsh pool-start boot
  • Get some linux distributions to play with:
    • An iso installer image for xubuntu 18.04
    • A ready to run image of openwrt (release lede-17.01)
      sudo -i
      cd /var/lib/libvirt/boot
      chown libvirt-qemu:kvm xubuntu-18.04-desktop-amd64.iso
      cd ../images
      chown libvirt-qemu:kvm lede-17.01.4-x86-64-combined-squashfs.img
  • Since we have create a bridge for the virtual machines, we can safely disable the default network created by libvirt:
    virsh net-autostart default --disable
    virsh net-destroy default
    virsh net-undefine default

Disable snapshots for virtual disks

Snaphshotting multi-gigabyte virtual disk image is not a good idea, it ended up filling my disks very fast, and since I don’t have enough space on the SSD for the virtual machines, let’s move them to the HDDs.

  1. Stop libvirt
       sudo systemctl stop libvirt-bin
  2. Create the subvolume and move data (if libvirt is already installed)
       sudo -i
       mkdir -p /tmp/work/root /tmp/work/data
       mount /dev/mapper/root-root /tmp/work/root
       mount /dev/mapper/data-data /tmp/work/data
       cd /tmp/work
       btrfs subvolume create data/@var-lib-libvirt
       mv root/@/var/lib/libvirt/* data/@var-lib-libvirt/
  3. Add the required line to /etc/fstab:
    /dev/mapper/data-data /var/lib/libvirt btrfs noatime,subvol=@var-lib-libvirt 0 2
  4. Mount and restart libvirt
    sudo mount -a
    sudo systemctl start libvirt-bin

Setup - client side

  • Install the required packages:
    cli@workstation:~$ sudo apt-get install virt-manager
  • copy your ssh public key to the server
    cli@workstation:~$ ssh-copy-id cli@server
  • Start virt-manager, File > new connection…
    • Hypervisor : qemu/kvm
    • Connect to remote host : checked
    • username : cli
    • hostname : server
    • Connect

Testing - client side - test the xubuntu image

  • Select the server connection
  • File > new virtual machine (or the new virtual machine button).
  • Step 1
    • Verify the connection selected is server one
    • Select Local media
    • Forward
  • Step 2
    • Select use ISO image and browse…, select boot/xubuntu-18.04-desktop-amd64.iso then Choose volume
    • OS type : Linux
    • Version : Ubuntu 16.04 - Forward
  • Step 3
    • 1024 MB RAM and 1 CPU should be sufficient for testing - Forward
  • Step 4
    • 15 Go should be sufficient for testing - Forward
  • Step 5
    • Choose a name
    • Network selection : specify a shared device name:
      • br0 ⚠ It seems there is a bug in 18.04, since the br0 is listed when the server is running 16.04
    • Forward
  • Now you can play with that new image


Question, remark, bug? Don't hesitate to contact me or report a bug.