28

I am trying to get qemu-kvm to boot from my live usb stick. Is this possible?

25

qemu-kvm -hdb <device>, where <device> is the USB stick (e.g. /dev/sdb), should do it (tested with Ubuntu 12.04 on an USB stick and it works).

You will need write permission to the device (i.e. be root or change its permissions).

| improve this answer | |
  • 2
    Good answer. BTW if you need an easy interface to manage your VMs, give virt-manager a try. – wazoox May 2 '12 at 21:19
  • 2
    Just as a hint: I tried VirtualBox instead of kvm to test my live usb stick, and it's A LOT faster. You have to use this command as root to create a vmdk file to be used in VirtualBox: VBoxManage internalcommands createrawvmdk -filename .VirtualBox/HardDisks/usbdisk.vmdk -rawdisk /dev/sdb – Marc MAURICE Mar 17 '13 at 8:37
  • 4
    Just a small note to self here - use the disk/device node in -hdb (e.g. /dev/sdb); NOT the partition (as in /dev/sdb4) – sdaau May 28 '13 at 14:10
  • In order to avoid a warning message WARNING: Image format was not specified for '/dev/sdb' and probing guessed raw. Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. Specify the 'raw' format explicitly to remove the restrictions. you can specify options in this format: -drive file=/dev/sdb,format=raw,index=0,media=disk See this question – Ashark Mar 8 '19 at 2:35
26

qemu-kvm has disappeared. use the hint in start qemu by giving it a path to a linux root instead. this means

qemu-system-x86_64 -usb -usbdevice disk:/dev/sdb

where /dev/sdb is your raw device (not partition) of the usb stick that you want to boot.

(however, as of 12/2014, it's been flaky for me. booting ubuntu-gnome 14.10 results eventually in a kernel panic on a stick verified to boot just fine on the metal.)

/iaw

| improve this answer | |
  • Thanks, needed this to auto boot a build of bin-x86_64-efi/ipxe.usb for a EFI boot, and it worked without issues so far. – NiKiZe Jul 27 '17 at 18:55
  • 2
    Can you please update your answer? I got the following: qemu-system-x86_64: -usbdevice disk:/dev/sdb: '-usbdevice' is deprecated, please use '-device usb-...' instead – Ashark Mar 7 '19 at 23:50
  • 1
    Instead of -usbdevice storage, you now have to use sonmething like: -device usb-storage,drive=xxx -drive file=/dev/sdd,if=none,id=xxx – Remember Monica Dec 16 '19 at 11:20
8

I don't know why exactly, but Renan's answer didn't quite work for me. KVM said to me it couldn't find a bootable drive (despite the usb partition being marked as such).

However I've found another solution. Get the USB device VendorID:ProductID with lsusb. Example:

$ lsusb
...
Bus 002 Device 007: ID 0781:5406 SanDisk Corp. Cruzer Micro U3

Pass that to KVM, and ask for the boot menu:

sudo  kvm -m 512 -smp 1 -drive file=/path/to/hardisk/file.img -usb \ 
-usbdevice host:0781:5406 -boot menu=on

Press F12 for the menu, choose the usb device, it works. There's probably a way to command the usb boot without needing to call the boot menu, but I didn't find any in man kvm.

| improve this answer | |
  • As for me, passing usb stick as a hdd drive works correctly for me, but passing usb stick as a usb device with -boot menu=on 1) in bios mode allow me to choose it, shows syslinux menu (I flashed archlinux iso to usb stick with dd) and it starts loading, but fails with Waiting 30 seconds for device /dev/disk/by-label/ARCH_201903 ... sd 2:0:0:0 [sda] Assuming drive cache: write through print_req_error: I/O error, dev sda, sector 0 Buffer I/O error on dev sda, logical block 0, async page read ERROR: '/dev/disk/by-label/ARCH_201903' device did not show up after 30 seconds 2) in uefi it fails – Ashark Mar 8 '19 at 3:38
1

If you want to use an image of a liveusb, instead of a physical USB stick, you can do that with

$ kvm -usbdevice disk:/path/to/liveusb.img -boot menu=on ...

Without "-boot menu=on", kvm will always prefer the hard drive that you set with "-drive" or "-hd[a|b|c|d]".

| improve this answer | |
1

I did this by first creating a guest using e.g. virt-manager, then editing the virsh config file to boot from USB:

  1. Edit config file for the vm:

    # virsh edit TAILS
    
  2. Delete the <boot> element from the <os> element. (Leave the rest of the <os> element intact.) You can't use <boot> here, because the boot order will be specified in step 4.

  3. Get the vendor and product ID for the USB device that contains the live system:

    # lsusb -v | grep -E '\<(Bus|iProduct|bDeviceClass|bDeviceProtocol)' 2>/dev/null
    

    This produces output like:

Bus 002 Device 007: ID 090c:1000 Silicon Motion, Inc. - Taiwan (formerly Feiya Technology Corp.) Flash Drive
  bDeviceClass            0 
  bDeviceProtocol         0 
  iProduct                2 

The vendor ID is the first four hex digits after 'ID' on the first line, 090c in this example. Product ID is the last four hex digits after the colon, here 1000.

  1. In the virsh editor, add a <hostdev> element for your USB device, inside the <devices> element (which should already exist). Detailed instructions/examples for this element are at https://libvirt.org/formatdomain.html#elementsHostDev. lsusb gives the values in hexadecimal, so use the 0x prefix unless you want to convert the values for some reason.
<devices>
  <hostdev mode='subsystem' type='usb'>
    <source startupPolicy='mandatory'>
      <vendor id='0x090c'/>
      <product id='0x1000'/>
    </source>
    <boot order='1'/>
  </hostdev>
</devices>
  1. Save and exit virsh editor, and boot the VM in the usual way. (I'm using virt-manager, but a command-line boot uses the same config file.)
| improve this answer | |
0

To emulate USB storage, attached to an USB port, that can be inserted and removed and will be seen as an USB device instead of a SATA hard disk (like -hdb does), use:

$ qemu-system-x86_64                                      \
    -device piix3-usb-uhci                                \
    -drive id=pendrive,file=<disk.img>,format=raw,if=none \
    -device usb-storage,drive=pendrive                    \
    -boot menu=on

Replace <disk.img> with some USB disk image. Then to connect and disconnect the pen drive while running the VM, on the running qemu instance console:

(qemu) device_del pendrive
(qemu) drive_add 0 id=pendrive2,if=none,file=<disk2.img>
(qemu) device_add usb-storage,drive=pendrive2

Reference: https://www.linux-kvm.org/page/USB

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.