KVM getting started

The difference between qemu-system-x86_64 and qemu-system-aarch64 comes down to the CPU architecture they emulate and the type of guest OS they can run

  • The qemu-system-x86_64 command for Emulates: An x86_64 (Intel/AMD 64-bit) CPU.
1qemu-system-x86_64 \
2    -machine q35,accel=hvf \
3    -cpu host \
4    -m 4G \
5    -hda ubuntu-x86_64.img
  • The qemu-system-aarch64 command for Emulates: An ARM64 (AArch64) CPU (e.g., Apple M1/M2, Raspberry Pi, AWS Graviton)
1qemu-system-aarch64 \
2    -machine virt,accel=hvf \
3    -cpu host \
4    -m 4G \
5    -hda ubuntu-arm64.img

Use the cloud-image to test the cloud-init

  • Download the cloud-images for ubuntu:cloud image
  • Create three cloud-init file user-data, meta-data, vendor-data
     1  # Create the user-data 
     2  cat << EOF > user-data
     3  #cloud-config
     4  password: password
     5  chpasswd:
     6  expire: False
     7
     8  EOF
     9
    10  # Create the meta-data
    11  cat << EOF > meta-data
    12  instance-id: someid/somehostname
    13
    14  EOF
    15
    16  # Create a empty vendor-data
    17  touch vendor-data
    
  • Launch the IMDS server in current directory
    1    python3 -m http.server --directory .
    
  • Create a virtual machine instance
    1qemu-system-x86_64                                            
    2    -net nic                                                    
    3    -net user                                                   
    4    -machine accel=kvm:tcg                                      
    5    -m 512                                                      
    6    -nographic                                                  
    7    -hda noble-server-cloudimg-amd64.img                        
    8    -smbios type=1,serial=ds='nocloud;s=http://10.0.2.2:8000/'
    

Can use the prompt in ChatAI

how to launch ubuntu linux x86_64 in mac os apple silicon use : qemu-system-x86_64

QEMU in macos

  • Basic QEMU to crate the x86_64 guest system
 1
 2# create the virtual disk image
 3qemu-img create -f qcow2 ubuntu-x86.qcow2 20G
 4
 5# install the ubuntu os to virtual disk
 6qemu-system-x86_64 \
 7-m 4G \
 8-smp 4 \
 9-machine q35,accel=tcg\
10-cpu Broadwell \
11-drive file=ubuntu-x86.qcow2,format=qcow2 \
12-cdrom path/to/ubuntu-iso.iso \
13-boot d \
14-vga virtio \
15-display default,show-cursor=on
16
17# launch the ubuntu linux from virtual disk
18qemu-system-x86_64 \
19-m 4G \
20-smp 4 \
21-machine q35,accel=tcg \
22-cpu Broadwell \
23-drive file=ubuntu-x86.qcow2,format=qcow2 \
24-vga virtio \
25-display default,show-cursor=on
  • Shortcut Try hitting Ctrl+Opt+2 or Ctrl+Opt+3 and you should see the output of serial and parallel ports. This is where we’re going to see our operating system running. You can always go back to the monitor console with Ctrl+Opt+1
  • Use the UEFI to launch
 1qemu-system-aarch64 \
 2    -nodefaults \
 3    -machine virt,accel=hvf \
 4    -cpu host \
 5    -chardev vc,id=monitor \
 6    -mon monitor \
 7    -serial vc \
 8    -bios /opt/homebrew/share/qemu/edk2-aarch64-code.fd
 9
10#
11# -machine virt specifies a type of a machine - we have no interest in emulating a specific hardware so we just use the special type virt
12# the accel=hvf part is the important one: it enables hardware acceleration using macOS Hypervisor Framework
13# -cpu host specifies that the guest machine will see exactly the same CPU model as the host machine (required for acceleration)
14#
15qemu-system-aarch64 \
16    -machine virt,accel=hvf \
17    -cpu host

Install require packages

 1# check the BIOS is enabled virtualization
 2egrep --color 'vmx|svm' /proc/cpuinfo
 3
 4echo " Upgrade the system"
 5sudo apt-get update && sudo apt-get -y upgrade || apt-get -y install sudo && sudo apt-get update && sudo apt-get -y upgrade
 6
 7echo "Virtualization host installation"
 8sudo apt-get -y install qemu-kvm libvirt-bin virtinst virt-viewer libguestfs-tools virt-manager uuid-runtime curl libvirt-dev genisoimage qemu-kvm libyaml-dev
 9
10echo "Enable libvirt"
11sudo systemctl restart libvirtd
12sudo virt-host-validate
13
14
15
16# with kvm
17minikube start --vm-driver kvm2

Reference