-1

I am writing a program that needs to check if there are removable drives. It uses /sys/class/block to identify these devices by checking if the special file removable is present and contains a number that is not zero. For example, on my system, /sys/class/block/sdc/removable contains a zero since it is a SATA hard drive, while the USB stick in /sys/class/block/sdd/removable contains 1.

I am testing my program on a virtual machine using qemu. I got so far as to have entries in /sys/class/block by disabling CONFIG_SYSFS_DEPRECATED in the kernel configuration (kernel 6.1.14) and adding a USB drive like so:

kvm -bios /usr/share/qemu/OVMF.fd \
    -net none \
    -drive file=root.img,format=raw \
    -m 1G -cpu host -smp 2 \
    -drive if=none,id=stick,format=raw,file=usb.img \
    -device nec-usb-xhci,id=xhci \
    -device usb-storage,bus=xhci.0,drive=stick

taken from the QEmu documentation here. The USB stick works and can be mounted/unmounted, but there is no removable entry in the sys filesystem. What am I missing?

hochl
  • 12,524
  • 10
  • 53
  • 87
  • There was another Qemu question recently where a Qemu expert (seemingly), replied that Qemu is a very limited system and there are many features not available that you would find in a normal OS. I'm pretty sure it was in the last month, so try looking at questions with Qemu tags in date order descending. (I probably don't have that exactly right, but it is the general idea). Good luck. – shellter May 04 '23 at 01:15
  • SO is a programming Q&A platform and this question is not about programming. Questions about operating systems, their utilities, networking and hardware, are off topic here. [What topics can I ask about here?](https://stackoverflow.com/help/on-topic). Please delete this and ask, instead, on [Unix & Linux Stack Exchange](https://unix.stackexchange.com/) or https://superuser.com/ – Rob May 05 '23 at 10:16
  • Actually this is a programming question because it is a programming project that is interacting with a virtual machine, which are often used to simulate and debug software projects that interact with the OS, thus can be considered a programming tool in this context. Also, there are dozens of similar questions on SO, and qemu is even a tag. Additionally, a question can be migrated to a different site without deleting it. – hochl May 05 '23 at 17:17

1 Answers1

3

The usb-storage device defaults to behaving like a USB hard disk, which typically will not set the RMB (removable media bit) in the information it reports to the guest. However you can configure it to set RMB, making it behave like a typical USB thumb drive instead.

You can do this by adding removable=on to your usb-storage device suboptions, so in your case:

-device usb-storage,bus=xhci.0,drive=stick,removable=on
Peter Maydell
  • 9,707
  • 1
  • 19
  • 25
  • +1 perfect I seem to have that one overlooked it is neither in the man page nor in the link I have in my question :( – hochl May 05 '23 at 17:14
  • I'm debugging some GRUB/UEFI code and it saved my day :) Now it would be nice to be able to simulate "media_present=0" but it seems qemu canot do it :( – mishmashru May 26 '23 at 15:16