How to Test Bootable USB Drive with QEMU
23 Dec 2024 - 3 min read![How to Test Bootable USB Drive with QEMU](/_image?href=https%3A%2F%2Fs3.thinhhv.com%2Fthinhhv-blog%2Fuploads%2Fhow-to-test-bootable-usb-drive-with-qemu.jpeg&w=768&h=432&f=webp)
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:
sudo fdisk -l
Disk /dev/loop0: 4 KiB, 4096 bytes, 8 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sda: 953,55 GiB, 1023871549440 bytes, 1999749120 sectorsDisk model: STORAGE DEVICEUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: gptDisk 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:
sudo qemu-system-x86_64 -hda /dev/sda -m 2048
- UEFI USB Boot:
sudo apt install ovmf qemu-efi qemusudo 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:
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:
lsusb
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hubBus 003 Device 002: ID 13d3:5497 IMC Networks USB2.0 HD UVC WebCamBus 003 Device 005: ID 258a:00c8 SINO WEALTH RK Bluetooth KeyboardBus 003 Device 006: ID 046d:c542 Logitech, Inc. Wireless ReceiverBus 003 Device 008: ID 05e3:0749 Genesys Logic, Inc. SD Card Reader and WriterBus 003 Device 004: ID 1a40:0801 Terminus Technology Inc. USB 2.0 HubBus 003 Device 003: ID 8087:0033 Intel Corp.Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hubBus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hubBus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
- Use the following command to boot your VM from the USB device:
# Legacysudo qemu-system-x86_64 -usb -device usb-host,hostbus=3,hostaddr=8 -m 2048# UEFIsudo 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.