Use KVM to boot a Harddrive

July 25, 2020, 9:08 am

Have an old Harddrive recovered from a broken down maschine? Or do You want to install a operating system on a USBStick? You may consider using kvm for that.

I start by finding the device file, of the requireed drives.


Make sure to use the top level device (ex. /dev/sda), not a partition (ex. /dev/sda1)

Dont use Partitions, you have mounted. It might break the filesystem. If you dont use readyonly or copy on write options.

If you havent done it allready, you need to isntall kvm

sudo apt install kvmtool

Now to the hard part, using kvm. I usually hack myself a kvm command together, but for a more convenient way, you should consider using virtmanager.

kvm -hda /dev/sda -m 8G -cpu host -smp 4 -k de -vga qxl

This is one of the most simplest commands. The manpage is located here:

man qemu-system-x86_64
-hda /dev/sda

This sets, the primary drive, you need to change this to the drive you found earlier.

Alternatively you could use

-drive file=/dev/sda,index=0,media=disk,format=raw,read-only

I think one of the most interesting drive arguments is read-only to boot potentially corrupted filesystems.

-m 8G

This simply sets the allowed memory size to 8GB.

-cpu host

This sets the cpu information. kvm -cpu help to get a list of all supported types.

-cpu host tries to add all host-cpu features to the virtual cpu

-smp 4

This sets the cpu core count. I usually just put a number there, but you could go full creative with this one. -smp cpus=n,cores=n,threads=n,sockets=n,maxcpus=n

-k de

This just sets the Keyboard layout to German.

-vga qxl

This sets the vga mode. check the man page for a list, of supported modes.

Booting a iso image

You have downloaded a few new linux distros and want to quickly check them out, without setting up a extra vm for every distro.

Its possible to simply boot the image.

-cdrom /home/user/Downloads/linuxmint20.iso -boot menu

You still can mount a hard drive, maybe a common home-partition? But be careful with partitions, that are already mounted. use them in readonly mode!