Using a file (drive IMG) instead of a physical USB flash drive for developer's ssh access

:bomb: :fire: :bomb: :fire: :bomb: :fire: :bomb: :fire: :bomb: :fire:
WARNING: You can destroy you primary hard drive and loose all data if you are not careful. Please do not attempt unless you completely understand the instructions and what fdisk is.
:bomb: :fire: :bomb: :fire: :bomb: :fire: :bomb: :fire: :bomb: :fire:

Summary: I want to use a file to mimic a USB flash drive instead of using a physical usb drive. I needed a usb drive to allow Debugging the Home Assistant Operating System | Home Assistant Developer Docs to let me setup VLAN access. This is how I created the virtual usb drive / file instead of using a physical flash drive. Note: the file has a partition within it just like a hard drive. You can not directly mount and edit the file contents.

  1. Back up all your data and use a different virtual machine to run this commands. Don’t use your main / host machine.

  2. Create the virtual flash drive. I like using the location /var/lib/libvirt/images for my saved usb drive.

  3. cd /var/lib/libvirt/images/

  4. fallocate -l 10M ha-usb.img

  5. fdisk ha-usb.img You need to follow the instructions here How to create FAT32 USB drive on Linux – Redips spideR Net to create the partition.

  6. kpartx -asv ha-usb.img ; ls -l /dev/mapper/loop* You will need to know what the correct value is to change <<loop0p1>> below

  7. mkfs -t vfat /dev/mapper/<<loop0p1>>

  8. dosfslabel /dev/mapper/<<loop0p1>> CONFIG

  9. kpartx -dsv ha-usb.img

  10. Now can see what is inside the virtual flash drive

    1. mkdir /tmp/t

    2. kpartx -asv ha-usb.img ; ls -l /dev/mapper/loop* You will need to know what the correct value is to change <<loop0p1>> below

    3. mount -t auto -o loop /dev/mapper/<<loop0p1>> /tmp/t/

    4. ls -l /tmp/t

    5. edit the files per https://github.com/home-assistant/operating-system/blob/eba7099adcf20ebafd84015c90c615191a7bfc71/Documentation/configuration.md

      You can use an USB drive with HassOS to configure network options, 
       SSH access to the host and to install updates. 
      Format a USB stick with FAT32/EXT4/NTFS and name it CONFIG (in all capitals). 
      Alternative you can create a CONFIG folder inside the boot partition. 
      Use the following directory structure within the USB drive:
      
    6. umount /tmp/t

    7. kpartx -dsv ha-usb.img

  11. Follow the instructions here to install KVM / QE Install Home Assistant OS with KVM on Ubuntu headless (CLI only).

  12. start Home Assistant virtually and verify it is running

  13. Using virt-manager stop and edit the home assistant settings. You need to add a the created virtual usb drive

    1. click on the show virtual hardware details icon. In the tool bar it looks like a light bulb.
    2. click Add Hardware, bottom left, button
    3. click Storage
    4. click Select or create custom storage
    5. click Manage
    6. click on the new file ha-usb.img and press Choose Volume
    7. leave Device type as Disk device
    8. Bus Type: must be USB
    9. Optionally click Advanced Options
      1. check Read Only
      2. check Shareable
  14. To check the ssh access is working
    ssh root@homeassistant -I <<your_file_location/id_rsa>> -p 22222