Connecting an external USB SSD with a UFS partition to a FreeBSD virtual machine running on QEMU on Raspberry Pi OS, RPi 500 keyboard model

Connecting an external USB SSD with a UFS partition to a FreeBSD virtual machine running on QEMU, hosted on a Raspberry Pi OS, involves several steps. The goal is to make the USB SSD accessible to the FreeBSD guest as a file storage system. Below is a step-by-step guide to achieve this, assuming the host is a Raspberry Pi running Raspberry Pi OS (Linux-based) and the guest is a FreeBSD virtual machine in QEMU.


Prerequisites

  • Hardware:

    • Raspberry Pi (e.g., Pi 4 or 5) running Raspberry Pi OS.
    • External USB SSD with a UFS partition (formatted on a FreeBSD system, as UFS is not natively supported by Linux).
    • Sufficient power for the USB SSD (use a powered USB hub if needed, as Raspberry Pi USB ports may not provide enough power for some drives).
  • Software:

    • QEMU installed on the Raspberry Pi OS (qemu-system-aarch64 for ARM64 architecture).
    • FreeBSD disk image or installed FreeBSD VM for QEMU.
    • Basic familiarity with Linux and FreeBSD command-line tools.
  • Notes:

    • Linux (Raspberry Pi OS) does not natively support UFS, so direct mounting of the UFS partition on the host is not feasible. Instead, we’ll pass the USB SSD as a raw block device to the FreeBSD VM, which supports UFS natively.

Step-by-Step Guide

1. Prepare the USB SSD on the Host (Raspberry Pi OS)

  • Connect the USB SSD: Plug the USB SSD into a USB port on the Raspberry Pi. If the drive requires more power than the Pi can supply, use a powered USB hub.

  • Identify the USB SSD: On the Raspberry Pi OS, run the following command to list block devices and identify the SSD:

    lsblk
    

    Look for a device like /dev/sda or /dev/sdb with the expected size of your SSD. Note the device name (e.g., /dev/sda). If the SSD has a UFS partition, it may not show a recognizable filesystem type in lsblk because Linux does not support UFS natively.

    Alternatively, use:

    sudo fdisk -l
    

    to see all disks and partitions. Be cautious to identify the correct device to avoid data loss.

  • Verify the Device: To ensure you have the correct device, unplug the SSD, run lsblk again, and confirm the device disappears. This helps avoid mistakes with other drives (e.g., the SD card at /dev/mmcblk0).

  • Do Not Mount the UFS Partition: Since the SSD has a UFS partition, Raspberry Pi OS cannot mount it. Instead, you’ll pass the raw device to the FreeBSD VM.

2. Install QEMU on Raspberry Pi OS

If QEMU is not already installed, install it on the Raspberry Pi OS:

   sudo apt update
   sudo apt install qemu-system-arm

This installs the QEMU emulator for ARM systems, including qemu-system-aarch64, which is needed for FreeBSD on ARM64.

3. Prepare the FreeBSD VM

Ensure you have a FreeBSD VM image or have already set up a FreeBSD virtual machine in QEMU. You can download a FreeBSD ARM64 image from the official FreeBSD website (e.g., FreeBSD-14.1-RELEASE-arm64-aarch64-RPI.img.xz) and extract it:

   xz -d FreeBSD-14.1-RELEASE-arm64-aarch64-RPI.img.xz

Alternatively, follow a guide to install FreeBSD in QEMU from a CDROM image or ISO.

4. Configure QEMU to Pass the USB SSD to the FreeBSD VM

To make the USB SSD accessible to the FreeBSD VM, pass it as a block device to QEMU. Modify your QEMU command to include the SSD as a raw device.

Example QEMU command to start the FreeBSD VM with the USB SSD:

   sudo qemu-system-aarch64 \
     -machine virt \
     -cpu cortex-a72 \
     -smp 4 \
     -m 2G \
     -drive file=/path/to/FreeBSD-14.1-RELEASE-arm64-aarch64-RPI.img,format=raw,if=none,id=hd0 \
     -device virtio-blk,drive=hd0 \
     -drive file=/dev/sda,format=raw,if=none,id=usb-ssd \
     -device virtio-blk,drive=usb-ssd \
     -netdev user,id=mynet,hostfwd=tcp::2222-:22 \
     -device virtio-net-pci,netdev=mynet \
     -serial stdio \
     -monitor telnet:127.0.0.1:5555,server,nowait
  • Key Parameters:

    • -drive file=/dev/sda,format=raw,if=none,id=usb-ssd: Passes the USB SSD (e.g., /dev/sda) as a raw block device to the VM. Replace /dev/sda with the correct device name from lsblk.
    • -device virtio-blk,drive=usb-ssd: Attaches the SSD as a virtio block device, which FreeBSD supports.
    • -machine virt: Uses the generic virt machine type, suitable for ARM64 VMs. Note that QEMU’s Raspberry Pi-specific machine types (e.g., raspi4b) may not fully emulate all hardware, so virt is often more reliable.
  • Permissions: QEMU needs permission to access /dev/sda. Running QEMU with sudo (as shown) ensures this. Alternatively, add your user to the disk group:

    sudo usermod -aG disk $USER
    

    Then log out and back in. Be cautious, as this gives broad access to disk devices.

  • Warning: Ensure no other processes (e.g., automount) are accessing /dev/sda. Unmount any partitions if they were automatically mounted:

    sudo umount /dev/sda*
    

5. Boot the FreeBSD VM and Detect the USB SSD

  • Start the QEMU VM with the command above.
  • Log in to the FreeBSD VM (via the QEMU console or SSH if configured).
  • Check for the USB SSD in FreeBSD using:

    ls /dev
    

    The SSD should appear as a device like /dev/da0 (for SCSI-like devices, which virtio-blk emulates). If it has a UFS partition, you might see /dev/da0s1 (for slice 1, FreeBSD’s term for a partition).

  • Confirm the disk is detected:

    geom disk list
    

    This lists all disks. Look for a device matching the SSD’s size (e.g., da0).

6. Mount the UFS Partition in FreeBSD

  • Assuming the UFS partition is /dev/da0s1, create a mount point:
    sudo mkdir /mnt/usb-ssd
    
  • Mount the UFS partition:
    sudo mount -t ufs /dev/da0s1 /mnt/usb-ssd
    
  • Verify the mount:

    df -h
    

    You should see the SSD’s UFS partition mounted at /mnt/usb-ssd.

  • If the partition is not detected or mounting fails, check the partition table:

    gpart show da0
    

    This displays the partition scheme (e.g., GPT) and partitions. Ensure the UFS partition is correctly formatted. If it’s not UFS or is corrupted, you may need to reformat it in FreeBSD (see step 8).

7. Use the SSD as a File Storage System

  • The mounted UFS partition (/mnt/usb-ssd) can now be used as a file storage system in FreeBSD. For example:
    sudo touch /mnt/usb-ssd/testfile.txt
    sudo ls /mnt/usb-ssd
    
  • You can store files, create directories, or use it for any storage needs within the FreeBSD VM.
  • To make the mount persistent across reboots, edit /etc/fstab in the FreeBSD VM:
    sudo vi /etc/fstab
    
    Add a line like:
    /dev/da0s1  /mnt/usb-ssd  ufs  rw  0  2
    
    Save and exit. This ensures the UFS partition mounts automatically on boot.

8. (Optional) Format the SSD with UFS in FreeBSD

If the SSD does not already have a UFS partition or you want to reformat it:

  • In the FreeBSD VM, wipe the existing partition table (be cautious, this erases all data):
    sudo gpart destroy -F da0
    
  • Create a new GPT partition table:
    sudo gpart create -s gpt da0
    
  • Add a UFS partition:
    sudo gpart add -t freebsd-ufs -l ssd1 da0
    
  • Format the partition with UFS:
    sudo newfs -U /dev/da0p1
    
  • Mount the new partition:
    sudo mount -t ufs /dev/da0p1 /mnt/usb-ssd
    
  • Update /etc/fstab as described above for persistent mounting.

9. Test and Verify

  • Write test files to the SSD and verify they persist across VM reboots.
  • If using the SSD for specific applications (e.g., a file server), configure them to use /mnt/usb-ssd as the storage location.
  • Monitor performance. USB 3.0 SSDs on Raspberry Pi 4/5 offer better performance than USB 2.0 or SD cards, but QEMU’s virtio-blk emulation may introduce slight overhead.

10. Troubleshooting

  • SSD Not Detected in FreeBSD:
    • Ensure the correct device path (/dev/sda) is passed to QEMU.
    • Check QEMU’s monitor (connect via telnet 127.0.0.1 5555) and use info block to verify the SSD is attached.
    • Verify FreeBSD’s kernel supports virtio-blk (kldload virtio_blk if needed).
  • Permission Issues:
    • Run QEMU with sudo or adjust /dev/sda permissions.
  • Kernel Panic or Boot Issues:
    • Ensure the FreeBSD image is compatible with QEMU’s virt machine type. If using a Raspberry Pi-specific image, you may need to adjust the -machine type to raspi4b, but note that QEMU’s Raspberry Pi emulation has limitations.
  • UFS Mount Fails:
    • Verify the partition is UFS using gpart show.
    • If corrupted, reformat as described in step 8.

Additional Notes

  • Performance Considerations:
    • USB 3.0 ports on Raspberry Pi 4/5 provide better throughput than USB 2.0, so use a USB 3.0 SSD if possible.
    • QEMU’s virtio-blk driver is efficient but may not match native performance. For optimal performance, consider using a native FreeBSD installation on the Raspberry Pi instead of a VM, if the use case allows.
  • Security:
    • Passing raw devices (/dev/sda) to QEMU gives the VM full access, which could be a security risk. Ensure the FreeBSD VM is trusted and isolated from sensitive host data.
    • If the SSD contains sensitive data, consider using FreeBSD’s gbde or geli for encryption.
  • Alternative Approach:
    • If QEMU emulation is too complex or slow, consider running FreeBSD natively on the Raspberry Pi and mounting the UFS partition directly. This avoids virtualization overhead but requires reflashing the Pi’s SD card with FreeBSD.

Example Use Case

Suppose you want to use the SSD as a file server in the FreeBSD VM:

  1. Install a file server like samba or nfs in FreeBSD:
    sudo pkg install samba413
    
  2. Configure the file server to share /mnt/usb-ssd.
  3. Access the shared storage from other machines on the network, using the VM’s IP (configured via QEMU’s networking options).

This setup allows the FreeBSD VM to use the USB SSD’s UFS partition as a file storage system, leveraging FreeBSD’s native UFS support while running on a Raspberry Pi host via QEMU.

The end ...

Comments

Popular posts from this blog

Honey, I broke my ZFS USB SATA SSD 1 TB disk, Oops. What can I do to fix this ZFS to boot again?

FreeBSD 14.0 Compiling kernel for Raspberry Pi 4B

HDMI Audio sound patches into GhostBSD source code /usr/ghost14/ghostbsd-src SOLVED Jan20 2024