0%

Install Ubuntu on QEMU

By Z.H. Fu
https://fuzihaofzh.github.io/blog/

In this article, we will show how to install qemu and install an ubuntu system on it.

qemu install

you can directly install with brew or directly compile to code. The code is well written and I haven’t encounter some weird problems.

Download Image

Though you can install the system just the same as installing an ordinary, here, we strongly suggest you directly use the precompiled official image which saves a lot of time. Just download them at https://cloud-images.ubuntu.com/ .

make a user image

Since the downloaded image has a random password for the root user “ubuntu”, we should add our user name and set access to the image. Here, we use “cloud-localds” command to achieve this. Firstly, we write our login information into a user-data file as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#cloud-config
password: 123456
chpasswd: { expire: False }
ssh_pwauth: True
users:
- default
- name: maple
ssh-authorized-keys:
- ssh-rsa xxxxxxxxxxxxxx
sudo: ALL=(ALL) NOPASSWD:ALL
groups: sudo
shell: /bin/bash
write_files:
- path: /home/maple/.bashrc
content: |
some config in bash config file
runcmd:
- whoami

Afterward, we make a user-data image with the command:

1
cloud-localds user-data.img user-data

Then we start the qemu simulator with the downloaded image and our user image mounted together.

Start QEMU

Run the following code:

1
qemu-system-x86_64 -drive file=ubuntu-19.04-disco-server-cloudimg-amd64.img -drive file=user-data.img,format=raw -m 4000 -smp 4 -nographic -enable-kvm -net nic -net user,smb=xxx,hostfwd=tcp::9022-:22

Here, we use smb to mount the host machine’s folder. We also forward the ssh port of our guest machine the host’s 9022 port.

We mount the smb to the guest’s local file system as:

1
sudo mount -t cifs -o username=maple //10.0.2.4/qemu /path

Here, the ip address 10.0.2.4 is an address for the host servers.

To allow the symlink in the smb, we use the following code provided by the official document:

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
eval $(ps h -C smbd -o pid,args | grep /tmp/qemu-smb | gawk '{print "pid="$1";conf="$6}')
echo "[global]
allow insecure wide links = yes
[qemu]
follow symlinks = yes
wide links = yes
acl allow execute always = yes" >> $conf
# in case the change is not detected automatically:
smbcontrol --configfile=$conf $pid reload-config

copy file with ssh

We can install the files in the guest system and sync it to the host system. This helps a lot if we don’t have the sudo permission of the host machine. We firstly prepare some files as:

1
yum --installroot=/target --releasever=7 install yum

Then, we sync the files to the host machine as

1
sudo rsync -avzh fedora/* zhfu@10.0.2.2:/gds/zhfu/centos/

Reference

https://wiki.centos.org/HowTos/ManualInstall