Bellow instructions are for creating a kvm guest OS(Debian) on Debian. Though the examples are based on Debian it should be same for all other distro.
-
Check if your CPU support virtualization:
grep -E --color=auto 'vmx|svm|0xc0f' /proc/cpuinfo -
Check if the host kernel support KVM:
grep CONFIG_KVM /boot/config-$(uname -r) -
Install necessary packages for QEMU/KVM:
sudo apt install qemu-system -
Create a qcow2 image for the guest:
qemu-img create -f qcow2 virtualdebian.img 50G -
Boot ISO to install the guest OS: (You need to run this as root for networking to work)
sudo qemu-system-x86_64 -enable-kvm -m RAM_IN_MB -smp NUM_CPU -drive file=virtualdebian.img -cdrom debian.iso -boot d
-
Install guest OS.
-
Now run the guest OS with qemu whenever you want:
sudo qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp 2 -drive file=virtualdebian.img
-
If you want to share a folder from host to guest:
- Run the guest with this command:
sudo qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -smp 2 -drive file=virtualdebian.img,if=virtio -fsdev local,security_model=passthrough,id=fsdev0,path=/path/to/host/folder -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare- In guest mount the host folder:
mount -t 9p -o trans=virtio,version=9p2000.L hostshare /path/to/mount/point
-
A better alternative for sharing file would be virtiofs: Needs testing.
Debian netinst doesn't show any output if qemu is run with -nographic option while installing from ISO. You have to use -kernel /path/to/kernel with -append "console=ttyS0". Other Solutions:
- https://sites.google.com/a/palatin.fr/pierre/entries/debian-netinst-qemu
- https://superuser.com/a/1311906/460620
- https://levelup.gitconnected.com/probably-the-simplest-way-to-install-debian-ubuntu-in-qemu-2db6afde27ef
- https://serverfault.com/questions/976140/how-do-i-boot-the-debian-10-image-with-qemu-kvm
Once the installation is done you can start the VM normally.
- Audio: -soundhw ac97
- Increase Resolution:
-vga virtio - Better Graphics Support: https://www.linux-kvm.org/page/SPICE
- https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/README?id=refs/tags/v4.3.3
- https://www.linux.com/topic/desktop/how-compile-linux-kernel-0/
- https://wiki.archlinux.org/title/Kernel/Traditional_compilation
- https://unix.stackexchange.com/questions/253245/what-does-make-localmodconfig-do
- https://tldp.org/HOWTO/pdf/Modules.pdf