Virtual machine on linux: qemu
Create a harddrive file
qemu-img create -f qcow2 ./qemu-vm-hdd.qcow2 32G
-
qemu-img
is a tool to create or edit harddrive images for the virtual machine. This is where the filesystem of the virtual machine will typically be. -
-f qcow2
sets the format of the harddrive image file (alternativelyraw
may be used). -
./qemu-vm-hdd.qcow2 32G
initializes a new file at the given location with a size of 32 GiB.
Run the virtual machine using the harddrive file
qemu-system-x86_64 -cdrom "./os-installation-disk.iso" -vga vmware -cpu host -enable-kvm -m 4G -smp 4 -drive file=./qemu-vm-hdd.qcow2,format=qcow2
-
qemu-system-x86_64
indicates that the chosen architecture isx86_64
(other common architectures are:arm
,aarch64
, andi386
). Thesystem
part of this command indicates that it should run a complete system, and not just any stand-alone application. -
-cdrom "./os-installation-disk.iso"
is a bootable iso-file. Typically, an OS provides an iso-file for download from their website or mirrors. -
-vga vmware
is used, because the default value for this option (std
) resulted in wrong colors (blue is red, and vice versa) being displayed. -
-cpu host -enable-kvm
allows the virtual machine to directly use the CPU of the host (=parent) using a kernel virtualization module (Kernel-based Virtual Machine (KVM)). -
-m 4G -smp 4
enables qemu to use respectively 4GB of RAM and 4 CPU cores. -
-drive file=./qemu-vm-hdd.qcow2,format=qcow2
is the file that represents the virtual machine's harddrive, this is used as a permanent internal storage so to speak. You can also use the format:raw
. -
-bios /usr/share/edk2-ovmf/x64/OVMF_CODE.fdd
makes it possible to boot from GPT EFI partition (usingedk2-ovmf
package).
The first time you start the virtual machine using the given command, the virtual machine will boot using the provided installation disk (./os-installation-disk.iso
).
The harddrive file will be created by the virtual machine, if not exists already.
During this time you will install a bootable OS on the virtual machine's harddrive file (./qemu-vm-hdd.qcow2
).
Unless there are other filesystems mounted into the virtual machine, there will only be one harddrive available within the virtual machine, which refers to the given harddrive file.
The second time you start the virtual machine, you may run the command without the -cdrom "./os-installation-disk.iso"
option, since it should now boot from the newly installed OS.
By default, the virtual machine will already use the internet/network access of the host.