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 | #cloud-config |
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 | #!/bin/bash |
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/ |