USB-Flashdrive-Notes

USB-Flashdrive-Notes

January 10, 2024

1. Wipe Master Boot Record #

  • Talking about MBR-style partitions…

      dd if=/dev/zero of=/dev/[disk device] bs=1 count=64 seek=446 conv=notrunc
    
  • Explanation:

      dd
    
  • This standard command copies bytes from a source and writes them to a destination.
    It’s the simplest flexible tool for this job.

      if=/dev/zero
    
  • Here, we specify that we’re reading from /dev/zero, which is a special device which emits NUL bytes–zeros.

      of=/dev/[disk device]
    
  • Here, we specify which device we’re writing to.

      bs=1
    

    dd thinks in terms of blocks. The default block size may be 512 bytes,
    1024 bytes or 4096 bytes, depending on your system.
    However, we need to address things more precisely than that,
    so we tell dd to use a block size of 1 byte.

      count=64
    
  • Here, we tell dd to write 64 blocks (or bytes, because of our bs=1 parameter),
    since the primary partition table consists of 4 16-byte partition entries,
    for a total of 64 bytes.

      seek=446
    

    The primary partition table within the MBR (so, not talking about GPT here)
    is located 446 bytes in, so we instruct dd to seek 446 bytes in before writing.
    Extended partitions are generally created by using a primary partition slot
    to point at the extended partition table, so if we erase the 4 primary partitions,
    we effectively wipe the extended partition table as well;
    the OS won’t be able to find it, so it won’t be able to read and interpret it.
    (If you want to wipe the extended partition table,
    you’ll need to know more about the operating system;
    different operating systems do extended partitions in different ways.)

  • source

2. Create a USB Storage Drive #

  • Simplest way to create a storage drive linux filesystem using sfdisk
      <space>, 
    
    Uses the whole disk to create a partion with default linux filesystem.
      ,4G 
    
    This creates a 4GB sized partition.
  • Formatting the newly created partition as ext4
      sudo mkfs.ext4 /dev/sdX1 
    
  • For bootable installation medium use fat
      sudo mkfs.fat -F32 /dev/sdX1 
    
    or by using the usb id
      sudo mkfs.fat -F 32 /dev/disk/by-id/usb 
    
  • Eject USB-Flashdrive
      sudo eject /dev/sdX 
    
  • Edit the Disk Label (source partly chatGPT) To edit the disk label on a USB flash drive, you can use the e2label command for ext2, ext3, and ext4 filesystems.
  • Unmount the USB Drive
    Before making changes, unmount the USB flash drive.
    Use the following command, replacing /dev/sdX1 with the correct partition identifier:
    sudo umount /dev/sdX1
    
  • Change the Disk Label Use the following command to change the disk label. Replace NewLabel with the desired label, and /dev/sdX1 with the correct partition identifier for your USB drive:
      sudo e2label /dev/sdX1 NewLabel
    
  • Check the Changes - show the Disk Label Verify that the label has been changed by using the following command:
      sudo e2label /dev/sdX1
    
    This should display the new label you’ve set. Now, your USB flash drive should have the updated disk label. The e2label command is specific to ext2, ext3, and ext4 filesystems. If you’re using a different filesystem, you might need to use a different tool or command. For example, for FAT32, you can use the mlabel command:
      sudo mlabel -i /dev/sdX1 ::NewLabel
    
    Replace /dev/sdX1 with the correct identifier for your USB drive.

3. Create a USB flash installation medium #

  • as described above
    • wipe USB drive
    • create partition table
    • create partition
    • format partition using FAT32
  • write iso image to the partition
      dd bs=4M if=path/to/archlinux-version-x86_64.iso of=/dev/disk/by-id/usb-My_flash_drive conv=fsync oflag=direct status=progress
    

4. Ventoy - Multi-Image Boot-Utility #

  • Ventoy is a free and open-source utility used for writing image files such as .iso, .wim, .img, .vhd(x), and .efi files onto storage media to create bootable USB flash drives. Once Ventoy is installed onto a USB drive, there is no need to reformat the disk to update it with new installation files; it is enough to copy the .iso, .wim, .img, .img(x), or .efi file(s) to the USB drive and boot from them directly. [2] [3] [4] Ventoy will present the user with a boot menu to select one of these files.

    source - Wikipedia

  • For Linux - GUI mode

  • For Linux - CLI mode

    Download the installation package, like ventoy-x.x.xx-linux.tar.gz and decompress it. Run the shell script as root sh Ventoy2Disk.sh { -i | -I | -u } /dev/XXX XXX is the USB device, for example /dev/sdb.

    Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX
    CMD:
      -i   install ventoy to sdX (fail if disk already installed with ventoy)
      -I   force install ventoy to sdX (no matter installed or not)
      -u   update ventoy in sdX
      -l   list Ventoy information in sdX
    
    OPTION: (optional)
    -r SIZE_MB  preserve some space at the bottom of the disk (only for install)
    -s          enable secure boot support (default is disabled)
    -g          use GPT partition style, default is MBR style (only for install)
    -L          Label of the main partition (default is Ventoy)
    

    source - Ventoy