Get a cloud image from:
https://cloud-images.ubuntu.com/releases/
E.g. https://cloud-images.ubuntu.com/releases/16.10/release/ubuntu-16.10-server-cloudimg-amd64.img
Create a cloud-config called cloud.txt, which defines who can login etc. to the virtual cloud server, and create a disk image from it. For this you need your login name on your current system, along with the public part of your current ssh key:
$ whoami
ghawkins
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC...
$ cat > cloud.txt << EOF
#cloud-config
users:
- name: ghawkins
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC...
sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo
shell: /bin/bash
$ cloud-localds cloud.img cloud.txt
Copy the line contained in id_rsa.pub into the ssh-authorized-keys section and replace the username specified by name with your username.
Important: I thought #cloud-config was a comment and left it out - but without it no error is reported but you cannot login later.
Backup your image:
$ cp ubuntu-16.10-server-cloudimg-amd64.img ubuntu-16.10-server-cloudimg-amd64.img.orig
Note: this is a compressed qcow2 image - while it's about 320MB the running guest will see it as 2GB (as we'll confirm later).
Now start the cloud guest:
$ qemu-system-x86_64 \
-enable-kvm \
-smp 2 \
-m 1024 \
-nographic \
-hda ubuntu-16.10-server-cloudimg-amd64.img \
-hdb cloud.img \
-redir tcp:2222::22
The command line arguments:
-enable-kvm- full virtualization (rather than emulation).-smp 2- two (virtual) processors (as we'll confirm later).-m 1024- 1024MB of system memory.-nographic- output goes to terminal (rather than opening a graphics capable window).-hda ubuntu-16.10-server-cloudimg-amd64.img- use our Ubuntu cloud image as the primary drive.-hdb cloud.img- use the image we created fromcloud.txtas the secondary drive.-redir tcp:2222::22- map port 2222 on the host to port 22 (the standard ssh port) on the guest.
Once booted you eventually get to the console getty login prompt. No one can login here - so you need to switch to another terminal tab.
Now let's logon to the guest using the redirected port and check out a few things and then shut down the guest:
$ ssh -p 2222 ghawkins@localhost
Welcome to Ubuntu 16.10 (GNU/Linux 4.8.0-26-generic x86_64)
...
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 491M 0 491M 0% /dev
tmpfs 100M 3.2M 97M 4% /run
/dev/sda1 2.0G 979M 1016M 50% /
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/sda15 105M 4.8M 100M 5% /boot/efi
tmpfs 100M 0 100M 0% /run/user/1000
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
...
processor : 1
vendor_id : GenuineIntel
$ sudo shutdown now
Connection to localhost closed by remote host.
So above using df -h we can see that the disk appears to be 2GB and with cat /proc/cpuinfo we can see that we appear to have two processors. Finally using shutdown we can get back to the command prompt in the terminal where the guest was started.
TODO: see how changing the number of virtual CPUs affects the performance of the guest.
If you redo everything from scratch again with a copy of the original disk image then the guest will generate a new key to identify itself which will cause ssh to refuse to allow you to reconnect due to the change in key. To remove the old key from known_hosts do:
$ ssh-keygen -f ~/.ssh/known_hosts -R '[localhost]:2222'
Working out how to get this far was down to:
- http://www.cnx-software.com/2016/05/10/how-to-run-ubuntu-16-04-aarch64-64-bit-arm-cloud-images-on-your-intelamd-linux-computer/ - yes, even though it's about running an emulated ARM guest it was helpful in working out how to get everything going.
- https://www.digitalocean.com/community/tutorials/how-to-use-cloud-config-for-your-initial-server-setup - goes into the cloud config in more detail (and convinced me to finally add
#cloud-configat the start of the file).
The Ubuntu cloud images page wasn't as helpful as it should be:
But it does cover uncompressing the qcow2 disk image and increasing its size (2GB isn't much) and fancier stuff like creating a delta image to keep your initial disk image in a pristine condition.