I've installed Ubuntu 16.04 Desktop on QEMU, but now when I start it with:
qemu-system-i386 -m 1024M -enable-kvm -drive file=./ubuntu-desktop.img,index=0,media=disk,format=raw
Whats going on? How can I fix this?
I've installed Ubuntu 16.04 Desktop on QEMU, but now when I start it with:
qemu-system-i386 -m 1024M -enable-kvm -drive file=./ubuntu-desktop.img,index=0,media=disk,format=raw
Whats going on? How can I fix this?
Working Ubuntu 18.04 setup
Tested in an Ubuntu 18.10 host.
ubuntu-18.04.1-desktop-amd64.sh
#!/usr/bin/env bash
set -eux
Parameters.
id=ubuntu-18.04.1-desktop-amd64
disk_img="${id}.img.qcow2"
disk_img_snapshot="${id}.post-install.qcow2"
iso="${id}.iso"
Get image.
if [ ! -f "$iso" ]; then
wget "http://releases.ubuntu.com/18.04/${iso}"
fi
Go through installer manually.
if [ ! -f "$disk_img" ]; then
qemu-img create -f qcow2 "$disk_img" 1T
qemu-system-x86_64
-cdrom "$iso"
-drive "file=${disk_img},format=qcow2"
-enable-kvm
-m 2G
-smp 2
;
fi
Create an image based on the original post-installation image
so as to keep a pristine post-install image.
if [ ! -f "$disk_img_snapshot" ]; then
qemu-img
create
-F qcow2
-b "$disk_img"
-f qcow2
"$disk_img_snapshot"
;
fi
Run the copy of the installed image.
qemu-system-x86_64
-drive "file=${disk_img_snapshot},format=qcow2"
-enable-kvm
-m 2G
-smp 2
-soundhw hda
-vga virtio
"$@"
;
This script will do two QEMU runs:
The first time QEMU comes up:
The installer looks like this:
After the install is complete, the script automatically takes a post-install disk snapshot to ubuntu-18.04-desktop-amd64.img.qcow2, and creates a diffed copy to ubuntu-18.04-desktop-amd64.snapshot.qcow2.
It then runs with ubuntu-18.04-desktop-amd64.snapshot.qcow2, and saves any post-regular-boot changes there.
So next time you re-run ./ubuntu-18.04.1-desktop-amd64.sh just starts back from where you last left off.
And if you want to start from the pristine post-manual installation stage to test stuff in a clean environment, you can just:
rm ubuntu-18.04-desktop-amd64.snapshot.qcow2
./ubuntu-18.04.1-desktop-amd64.sh
The snapshot will be re-generated starting from the clean install.
The snapshot only stores the diffs between the original image, and so it does not take a lot of disk space.
This setup has, by default, a funky system that automatically resizes the guest resolution to best fit the QEMU window size, you can see this if you do one of:
Ctrl + Alt + F or or start QEMU with -full-screenNotes:
-vga virtio option is to be able to get higher resolutions: https://superuser.com/questions/132322/how-to-increase-the-visualized-screen-resolution-on-qemu-kvm/1331924#1331924
-soundhw hda enables sound. Why QEMU does not enable it by default beats me.
Once inside the VM, reduce the GRUB menu wait time and show some boot messages for next boot with:
printf 'GRUB_TIMEOUT=1\nGRUB_CMDLINE_LINUX_DEFAULT=""\n' | sudo tee -a /etc/default/grub
sudo update-grub
TODO clipboard sharing
Tried -spice port=5930,disable-ticketing + remote-viewer spice://127.0.0.1:5930, and spice-vdagent is pre-installed on guest, but no success.
The root cause of the mess is that QEMU devs seem more focused on non-interactive usage, rather than implementing things like this reliably and therefore killing VirtualBox once and for all: https://bugs.launchpad.net/qemu/+bug/614958
TODO: host 3D acceleration
Still with SPICE and QXL, glxgears gives 1k FPS, and the exact same with regular SDL. But on host __GL_SYNC_TO_VBLANK=0 vblank_mode=0 glxgears gives 20k FPS, so I'm guessing graphics were not accelerated?
Related:
Disable networking (e.g. for malware analysis)
Pass: -nic none on the CLI.
Mentioned at: https://wiki.qemu.org/Documentation/Networking#How_to_disable_network_completely
Share a host directory with the guest
Add to QEMU CLI:
-virtfs local,path=/path/to/share,mount_tag=host0,security_model=mapped,id=host0
and mount on guest with:
sudo mkdir /mnt/9p
mount -t 9p -o trans=virtio,version=9p2000.L host0 /mnt/9p
More details at: https://superuser.com/questions/628169/how-to-share-a-directory-with-the-host-without-networking-in-qemu/1301973#1301973 That also shows the corresponding fstab entry.
https://serverfault.com/questions/516097/how-to-efficiently-share-hosts-folder-with-kvm-guests claims is is not ultra fast though.
More dedicated question: How to share a folder between KVM host and guest using virt-manager?
This section was tested on Ubuntu 20.04 host.
Prebuilt bootable images
If you want an image that boots without the need for any interaction on the installer, see: Is there any prebuilt QEMU Ubuntu image(32bit) online?
Ubuntu server image
The exact same procedure as mentioned for desktop also worked for the server image.
I just wasn't able to do the initial install with -nogrphic to get rid of the GUI entirely: How to boot and install the Ubuntu server image on QEMU -nographic without the GUI?
But after the initial boot, after editing GRUB configs I achieve that: How to get to the GRUB menu at boot-time using serial console?
However, for the most part, you will likely just want to use the cloud image instead of the server image with emulation, as it does not have the interactive installer, and is usable from the CLI by default.
Tested on an Ubuntu 18.04 host, QEMU 1:2.11+dfsg-1ubuntu7.3, nvidia-384 version 390.48-0ubuntu3, Lenovo ThinkPad P51, NVIDIA Corporation GM107GLM [Quadro M1200 Mobile] GPU.
Installation steps described (by Ciro )worked for me. The cmd line used in script worked fine, But I had to use other options which were causing boot issues after that (booting stops showing storage blocks info)
-vga cirrus helped finally (max 1280 x 1024 resolution). I have nvidia GPU on host with dual monitor setup. Not sure what was causing issue. Also -vga std and -vga virtio didn't work.
Host OS = 16.4 LTS & Guest OS = 18.4.2 LTS
sudo ./x86_64-softmmu/qemu-system-x86_64 -m 8196 -M q35,accel=kvm,kernel-irqchip=split -cpu host -smp 8 -drive file=~/qemu/ubuntu-18.04.2-desktop-amd64.img.qcow2,format=qcow2 -vnc :5 -net nic -net user,hostfwd=tcp::2222-:22 -device intel-iommu,intremap=on **-vga cirrus**
Quickemu
https://github.com/quickemu-project/quickemu
This project looks promising. It aims to provide very easy launching of Linux, Mac and Windows VMs on QEMU from Linux and Mac hosts by downloading the right things and running them with QEMU correctly configured.
Install on Ubuntu 24.10 host:
sudo apt-add-repository ppa:flexiondotorg/quickemu
sudo apt update
sudo apt install quickemu
List available images:
quickget --list
One of them was:
ubuntu 22.04
so we download the Ubuntu 22.04 ISO with:
quickget ubuntu 22.04
Run it:
quickemu --vm ubuntu-22.04.conf
and this leaves us in the Ubuntu installer:
From there on I just next next next and wait for the installation to finish and after that it rebooted into the GUI:
And if I shut down the VM the next time I run:
quickemu --vm ubuntu-22.04.conf
it opens the GUI again with files saved as from previous session. At this point you might also want to snapshot the clean post-install with:
quickemu --vm ubuntu-22.04.conf --snapshot create postinstall
This stores the snapshot under the main qcow2 itself ubuntu-22.04/disk.qcow2 and you can later restore snapshots with:
quickemu --vm ubuntu-22.04.conf --snapshot apply postinstall
or list them with:
quickemu --vm ubuntu-22.04.conf --snapshot info
Working:
Working with further config:
clipboard sharing needs --display spice while running. This may degrade UI performance and has the following bugs limitations:
so it's not very practical... Annoying.
SSH setup: using QEMU headlessly is a good workaround given clipboard sharing issues if you don't need the GUI. It also overcomes mouse capture annoyance. You can install sddh on the guest with:
sudo apt install openssh-server
and then connect from host with something like:
sshpass -p mypassword ssh user@localhost -p 22220
The SSH command to use is also shown at launch time in a line such as:
- ssh: On host: ssh user@localhost -p 22220
From then on you can add --display none to your future VM launches to not startup the GUI at all:
quickemu --vm ubuntu-22.04.conf --display none
Another popular option over SSH is to use the VS Code remote server as it integrates with the filesystem a bit better than simply directory sharing, e.g. you can ctrl + click on files printed on the remote terminal logs such as compilation errors and have them open on host.
directory sharing: ~/Public on host is automatically exposed as a 9p mount on guest. You can mount it manually with something like:
sudo mkdir /mnt/9p
sudo mount -t 9p Public-$HOST_USERNAME /mnt/9p
in my case it is:
sudo mount -t 9p Public-ciro /mnt/9p
Not amazing that you have to leak your host username into the guest to get it to work. You could then automate that with a guest fstab entry such as:
printf 'Public-ciro /mnt/9p 9p trans=virtio,version=9p2000.L 0 0\n' | tr ' ' '\t' | sudo tee -a /etc/fstab
You can change the shared host directory with --public-dir as in:
quickemu --vm ubuntu-22.04.conf --public-dir ~/path/to/new/share
Related Reddit thread: https://www.reddit.com/r/commandline/comments/v85fmx/how_to_share_folder_between_guesthost_quickemu/
increase disk size: the default disk size is just 16 GB which is unreasonably small, especially considering that qcow2 is sparse and you can basically put arbitrarily large values there. The easiest way to work around this is to increase it before the first boot which is what creates the image by adding the following to ubuntu-22.04.conf:
disk_size="1T"
Alternatively, you can also do it afterwards with qemu-img resize and then modifying the partitions from inside the guest, related:
The download step downloads the official ISO from https://releases.ubuntu.com/22.04/ubuntu-22.04.5-desktop-amd64.iso and creates the following files in the current working directory:
Tested on quickemu 4.9.7 on a Lenovo ThinkPad P14s amd laptop.