How Ventoy Distinguishes USB Devices from Local Disks During Boot and Installation
Ventoy distinguishes USB devices from local disks by reading the Linux sysfs hierarchy at /sys/block and checking if the device path contains the substring /usb, while UEFI implementations verify the EFI device path bus type against BusTypeUsb or the VTOY_DEVICE_USB constant.
Ventoy implements strict safety mechanisms to prevent accidental installation onto internal hard drives. According to the ventoy/Ventoy source code, the open-source boot tool detects removable USB media by analyzing kernel sysfs symlinks and EFI device paths, ensuring that installation scripts and GUI utilities target only the intended removable devices.
Enumerating Block Devices via the Linux Sysfs
Ventoy begins device detection by scanning the kernel's sysfs filesystem to identify all available block devices.
Scanning /sys/block
In VtoyTool/vtoyexpand.c, the get_all_disk() function opens /sys/block to enumerate every block device node (such as sda, nvme0, or mmcblk0). The utility iterates through directory entries using opendir() and readdir(), collecting device names for subsequent classification.
Filtering Non-Block Entries
Before processing, Ventoy filters out pseudo-devices and non-physical media. The vtoy_is_possible_blkdev() function excludes entries like ram, loop, dm (device mapper), and sr (SCSI CD-ROM), ensuring only actual storage devices proceed to the USB detection stage.
Identifying USB Devices by Bus Type
Once enumerated, each device undergoes bus-type verification to determine if it connects via USB.
The is_usb_disk() Function
The core USB detection logic resides in is_usb_disk() within VtoyTool/vtoyexpand.c. For each candidate device, the function constructs the sysfs path /sys/block/<device> and reads its symbolic link target using readlink(). If the resolved path contains the substring "/usb", the function returns 1, flagging the device as USB in the disk_info struct's isUSB field.
// VtoyTool/vtoyexpand.c – USB detection logic
static int is_usb_disk(const char *diskname)
{
char dstpath[1024] = { 0 };
char syspath[1024] = { 0 };
snprintf(syspath, sizeof(syspath), "/sys/block/%s", diskname);
int rc = readlink(syspath, dstpath, sizeof(dstpath) - 1);
if (rc > 0 && strstr(dstpath, "/usb"))
return 1; // USB device detected
return 0; // Local disk or other bus type
}
EFI Device Path Detection
On UEFI systems, Ventoy uses the EFI BlockIO protocol to enumerate devices. The code in Plugson/src/Core/ventoy_disk.c checks the device path or bus type information available through EFI services, comparing against VTOY_DEVICE_USB as defined in Plugson/src/Core/ventoy_disk.h to identify removable USB media during the pre-boot environment.
Enforcing USB-Only Filters in Ventoy Tools
Ventoy's front-end utilities provide a "USB only" mode that leverages these detection mechanisms to prevent operations on internal disks.
CLI Enforcement in ventoy_cli.c
The command-line interface in Ventoy2Disk/ventoy_cli.c enforces USB verification unless explicitly disabled. When the USBCheck flag is enabled (default behavior), the code verifies that pDrvInfo->BusType equals BusTypeUsb before proceeding with installation.
// Ventoy2Disk/ventoy_cli.c – USB validation
if (CliCfg.op == 0 && CliCfg.USBCheck) {
if (pDrvInfo->BusType != BusTypeUsb) {
Log("[ERROR] PhyDrive %d is NOT USB type", pDrvInfo->PhyDrive);
// Abort installation to prevent local disk modification
}
}
Users can disable this check with the /NoUSBCheck switch, though this bypasses the safety mechanism.
GUI and Web UI Filtering
Both the GTK and Qt GUI implementations, as well as the Plugson web interface, utilize the VTOY_DEVICE_USB constant to filter device lists. In LinuxGUI/Ventoy2Disk/GTK/ventoy_gtk.c, the enumeration loop skips any device where cur->type != VTOY_DEVICE_USB when "USB Only" mode is active.
// LinuxGUI/Ventoy2Disk/GTK/ventoy_gtk.c – USB filter loop
if (alldev == 0 && cur->type != VTOY_DEVICE_USB)
continue; // Skip non-USB devices when USB-only mode is enabled
The same logic appears in LinuxGUI/Ventoy2Disk/QT/ventoy2diskwindow.cpp for the Qt interface and in Plugson/src/Core/ventoy_disk.c for the web-based management utility.
Summary
- Ventoy scans
/sys/blockviaget_all_disk()inVtoyTool/vtoyexpand.cto enumerate all storage devices while filtering out pseudo-devices likeramandloopthroughvtoy_is_possible_blkdev(). - The
is_usb_disk()function determines USB status by checking if the sysfs symlink target contains"/usb", setting theisUSBflag in the device information structure. - UEFI implementations detect USB devices through EFI device paths and the
BlockIOprotocol, comparing againstVTOY_DEVICE_USBorBusTypeUsbconstants defined in the core headers. - Installation tools enforce USB-only policies by verifying
pDrvInfo->BusTypein the CLI andcur->typein GUI applications, aborting operations on non-USB drives unless/NoUSBCheckis specified.
Frequently Asked Questions
How does Ventoy prevent installation on internal hard drives?
Ventoy prevents accidental internal disk modification by verifying the bus type of each physical drive before installation. The tools check for BusTypeUsb in the CLI and VTOY_DEVICE_USB in graphical interfaces, aborting the process if the selected drive is not identified as USB removable media according to the source code in ventoy_cli.c and ventoy_gtk.c.
Can Ventoy detect USB devices on systems without sysfs?
On UEFI systems or environments without accessible Linux sysfs, Ventoy uses EFI protocols to enumerate block devices. The implementation in Plugson/src/Core/ventoy_disk.c inspects EFI device paths to return VTOY_DEVICE_USB when the hardware path indicates a USB connection, providing cross-platform detection capabilities independent of the Linux kernel.
What happens if I use the /NoUSBCheck command-line option?
The /NoUSBCheck switch in Ventoy2Disk.exe disables the USB verification routine, allowing installation onto any block device including local NVMe or SATA drives. This bypasses the safety check in ventoy_cli.c that normally ensures pDrvInfo->BusType == BusTypeUsb, increasing the risk of overwriting internal disks.
Why does Ventoy filter out ram and loop devices during enumeration?
Ventoy excludes ram, loop, dm, and sr devices through vtoy_is_possible_blkdev() to avoid processing virtual or temporary block devices that lack physical storage media. This filtering ensures that USB detection logic and installation operations apply only to actual hardware storage devices capable of receiving the Ventoy bootloader.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →