Thursday, July 18, 2024

QEMU with command line

QEMU with command line


Introduction

QEMU is Quick emulator. This tutorial works on Qemu 9.0.1.

Installation

Debian: $ apt install qemu
Homebrew: $ brew install qemu
Linuxbrew: $ brew install qemu

Using qemu

Create image:

$ qemu-img create -f qcow2 alpine_disk.qcow2 12G
$ ls alpine_disk.qcow2
⇒ only created a 192k image file, even though it was stated to be 12G. The file will grow if it needs more space.

Launch QEMU

$ qemu-system-x86_64 \
-enable-kvm -m 2G -smp 1 \
-hda alpine_disk.qcow2 \
-boot d -cdrom alpine_linux.iso \
-netdev user,id=net0,net=192.168.0.0/24 \
-device virtio-net-pci,netdev=net0 \
-vga virtio \
-device AC97

## if no AC97 audio, just remove it
## -enable-kvm, use kernel kvm virtualization
## -cpu host, if you have this error warning: host doesn't support requested feature: CPUID.80000001H:ECX.svm [bit 2]

Hardware virtualization

Modern CPUs have hardware virtualization. Qemu performs much faster with hardware virtualization. For macOS, no KVM, use -machine type=q35,accel=hvf, for macOS virtualization framework.

## Linux -enable-kvm
## macOS -machine type=q35,accel=hvf

Install Alpine Linux

Choose Alpine Linux as an example, because it is small and simple.

Installation:

1. keymap (us,us)
2. network setting use default or [dhcp]
3. timezone [Singapore]
4. mirror [1]
5. add in new user (other than root) with username and password
6. Which disk to use [none]? sda ## depends on your system
7. How would you like to install it (sys, data, lvm…)? sys
8. Erase disk? y

Alpine Linux on Qemu

After the OS installation:

$ qemu-system-x86_64 \
-m 1G -smp 1 \
-hda alpine_disk.qcow2 \
-netdev user,id=net0,net=192.168.0.0/24 \
-device virtio-net-pci,netdev=net0 \
-nodisplay

Qemu display
## -nographic, not creating another window
## -vga virtio -display default, shows progress and login prompt

Reset terminal

If you need to reset terminal for word wrap.
$ reset
$ tput smam

Ssh from host to guest OS

Using ,hostfwd=tcp::2222-:22

## map host os port 2222 to guest os port 22
$ qemu-system-x86_64 \

-netdev user,id=net0,net=192.168.0.0/24,hostfwd=tcp::2222-:22


$ ssh user@localhost -p 2222

No comments: