How to Test Bootable USB Drive with QEMU

23 Dec 2024 - 3 min read
How to Test Bootable USB Drive with QEMU

To boot a QEMU virtual machine from a USB drive, you need to specify the USB device as a drive in your QEMU command.

Table of Contents

1. Identify Your USB Device

Run the following command to list your connected drives and identify your USB device:

Terminal
sudo fdisk -l
Disk /dev/loop0: 4 KiB, 4096 bytes, 8 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sda: 953,55 GiB, 1023871549440 bytes, 1999749120 sectors
Disk model: STORAGE DEVICE
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F2AC206C-87D7-4D48-A713-0001DEF27C36
Device Start End Sectors Size Type
/dev/sda1 2048 1999683543 1999681496 953,5G Microsoft basic data
/dev/sda2 1999683544 1999749079 65536 32M Microsoft basic data

2. Run QEMU with the USB Drive

Use the following command to boot your VM from the USB device:

  • Legacy USB Boot:
Terminal
sudo qemu-system-x86_64 -hda /dev/sda -m 2048
  • UEFI USB Boot:
Terminal
sudo apt install ovmf qemu-efi qemu
sudo qemu-system-x86_64 -hda /dev/sda --bios /usr/share/qemu/OVMF.fd -m 2048

a. (Optional) Use BIOS Boot Menu

  • If the above command doesn’t directly boot from USB, enable the QEMU boot menu:
Terminal
sudo qemu-system-x86_64 -hda /dev/sda -boot menu=on -m 2G -enable-kvm

When the boot menu appears, manually select the USB drive.

b. (Optional) Boot Using a Real USB Drive

To test a real USB device using host bus and address, follow these steps:

  • List USB devices:
Terminal
lsusb
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 002: ID 13d3:5497 IMC Networks USB2.0 HD UVC WebCam
Bus 003 Device 005: ID 258a:00c8 SINO WEALTH RK Bluetooth Keyboard
Bus 003 Device 006: ID 046d:c542 Logitech, Inc. Wireless Receiver
Bus 003 Device 008: ID 05e3:0749 Genesys Logic, Inc. SD Card Reader and Writer
Bus 003 Device 004: ID 1a40:0801 Terminus Technology Inc. USB 2.0 Hub
Bus 003 Device 003: ID 8087:0033 Intel Corp.
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  • Use the following command to boot your VM from the USB device:
Terminal
# Legacy
sudo qemu-system-x86_64 -usb -device usb-host,hostbus=3,hostaddr=8 -m 2048
# UEFI
sudo qemu-system-x86_64 --bios /usr/share/qemu/OVMF.fd -usb -device usb-host,hostbus=3,hostaddr=8 -m 2048

3. Debugging Boot Issues

  • If booting fails, use the -serial stdio or -nographic options to see the VM’s console output for debugging.
  • Ensure your USB device contains a bootable system with a valid bootloader.
ThinhHV

About ThinhHV

DevOps • Fullstack Developer • Amateur Photographer