The USB detection and management system now fully supports macOS using the native diskutil command-line tool.
- Added
is_macosflag detection (Darwin system) - Implemented
_detect_macos()method usingdiskutil list -plist external - Added
_detect_macos_fallback()for systems without plistlib - Updated
mount_device()to usediskutil mounton macOS - Updated
unmount_device()to usediskutil unmounton macOS - Updated
check_permissions()- macOS doesn't always require root for diskutil
- Added macOS detection throughout the file
- Updated
check_root()to be more lenient on macOS - Implemented macOS USB detection in
list_usb_devices()usingdiskutil list - Updated
flash_image()to:- Use
diskutil unmountDiskbefore flashing - Use
/dev/rdiskinstead of/dev/diskfor faster writes - Use
bs=4minstead ofbs=4M(BSD vs GNU dd syntax) - Use
diskutil ejectafter flashing
- Use
# The system uses diskutil to detect external USB drives:
diskutil list -plist external
# Fallback method:
diskutil list# If not already mounted:
diskutil mount /dev/disk2s1# Normal unmount:
diskutil unmount /Volumes/MyUSB
# Force unmount if needed:
diskutil unmount force /Volumes/MyUSB# Unmount disk before writing:
diskutil unmountDisk /dev/disk2
# Use raw disk device for faster writes:
dd if=image.img of=/dev/rdisk2 bs=4m
# Eject when done:
diskutil eject /dev/disk2# Run the main application
python3 main.py
# The system will:
# 1. Detect USB drives using diskutil
# 2. List available devices
# 3. Mount/unmount as needed# Flash an image to USB
sudo python3 flash_usb.py
# The system will:
# 1. List external USB drives
# 2. Let you select one
# 3. Unmount all partitions
# 4. Write the image using dd
# 5. Eject the drive when complete- Native macOS Tool - No additional packages required
- Proper Disk Management - Handles mounting/unmounting cleanly
- Fast Raw Disk Access - Using
/dev/rdiskfor better performance - Safe Ejection - Ensures all buffers are flushed before removal
- Works Without Root - For detection and basic mounting operations
| Operation | Linux | macOS |
|---|---|---|
| List USB | lsblk |
diskutil list |
| Mount | mount |
diskutil mount |
| Unmount | umount |
diskutil unmount |
| Format | mkfs.ext4 |
diskutil eraseDisk |
| Write Image | dd bs=4M |
dd bs=4m (note lowercase) |
| Device Path | /dev/sdb |
/dev/disk2 |
| Fast Write | /dev/sdb |
/dev/rdisk2 (raw device) |
To test on macOS:
-
Plug in a USB drive (
⚠️ WARNING: Testing may erase data!) -
Test USB Detection:
python3 main.py # Should detect and list your USB drive -
Test Mounting:
python3 -c "from src.usb import USBManager; usb = USBManager(); devices = usb.detect_usb_devices(); print(devices)" -
Test Flashing (use a spare USB!):
sudo python3 flash_usb.py
- diskutil is a standard macOS command - you're not on macOS or have a very unusual system
- Some operations (like flashing) require sudo
- USB detection and basic mounting usually work without root
- Make sure the USB isn't being used by another application
- Try force unmount:
diskutil unmount force /Volumes/YourUSB
- Make sure you're using
/dev/rdisknot/dev/disk - The code automatically converts disk to rdisk for better performance
🔍 Detecting USB devices...
✓ Found 1 USB device(s)
Available USB Devices:
1. /dev/disk2 - 32.0 GB - SanDisk USB
✓ Selected device: /dev/disk2
✓ Using mounted volume: /Volumes/COLDSTAR
🔄 First instance boot detected on this machine...
✓ Boot instance marker updated
✓ First boot process completed - no restoration needed
- macOS uses
/Volumes/for mount points instead of/mnt/or/media/ - Device names are like
/dev/disk2not/dev/sdb - Raw devices are
/dev/rdisk2for faster access - macOS typically auto-mounts USB drives, so explicit mounting is often unnecessary
- The system handles all these differences automatically!