Skip to content

Instantly share code, notes, and snippets.

@Dids
Last active March 17, 2025 23:10
Show Gist options
  • Select an option

  • Save Dids/c6fa12602ae812e873db539f71c52a3a to your computer and use it in GitHub Desktop.

Select an option

Save Dids/c6fa12602ae812e873db539f71c52a3a to your computer and use it in GitHub Desktop.

Revisions

  1. Dids revised this gist Jan 29, 2025. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -18,20 +18,20 @@ dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync

    ##### Mount the image
    ```sh
    # Mount the image on the first available loopback device
    # Mount the image on the first available loop device
    losetup -f image.img

    # List currently mounted loopback devices
    # List currently mounted loop devices
    # (this is just to confirm it was mounted)
    losetup
    ```

    ##### Create the EFI partition on the image
    ```sh
    # Get the loopback device for the mounted image
    # Get the loop device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`

    # Partition the loopback device
    # Partition the loop device
    # (enter the commands/characters and press enter)
    gdisk $LOOP_DEV_PATH
    o
    @@ -42,17 +42,17 @@ n
    w
    y

    # Trigger partition discovery for the newly partitioned loopback device
    # Trigger partition discovery for the newly partitioned loop device
    partprobe $LOOP_DEV_PATH

    # List the partitions of the loopback device
    # List the partitions of the loop device
    # (this is only for confirming that the partitions are visible)
    ls $LOOP_DEV_PATH*
    ```

    ##### Format the EFI partition
    ```sh
    # Get the loopback device for the mounted image
    # Get the loop device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1

    # Format the EFI partition as FAT32
    @@ -61,7 +61,7 @@ mkfs.fat -F32 $LOOP_DEV_PATH

    ##### Mount the EFI partition
    ```sh
    # Get the loopback device for the mounted image
    # Get the loop device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1

    # Make sure the target mounting directory exists
    @@ -74,7 +74,7 @@ mount $LOOP_DEV_PATH /mnt/image
    ##### Working with files on the EFI partition

    At this point you should be able to freely create, edit and remove files mounted under the `/mnt/image` path.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loop device.

    ##### Unmount the EFI partition
    ```sh
    @@ -86,12 +86,12 @@ umount /mnt/image
    rm -rf /mnt/image
    ```

    ##### Unmount the image/loopback device
    ##### Unmount the image/loop device
    ```sh
    # Get the loopback device for the mounted image
    # Get the loop device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`

    # Unmount the loopback device
    # Unmount the loop device
    losetup -d $LOOP_DEV_PATH

    # Verify that the image is no longer mounted
    @@ -104,12 +104,12 @@ This section assumes that you no longer have the image mounted in any way. It al

    ##### Mount the image
    ```sh
    # Mount the image as a loopback device first
    # Mount the image as a loop device first
    # -f searches for the next free loop device (no need to manually select one)
    # -P triggers a scan for any available partitions in the image
    losetup -f -P image.img

    # Get the loopback device for the mounted image
    # Get the loop device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1

    # Mount the EFI partition to a local path (create path first if necessary)
    @@ -125,7 +125,7 @@ ls -lah /mnt/image
    ##### Working with files on the image

    At this point you should be able to freely create, edit and remove files mounted under the `/mnt/image` path.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loop device.

    ##### Unmount the image
    ```sh
    @@ -136,9 +136,9 @@ umount /mnt/image
    # DISCLAIMER: Be careful with this, as you could potentially lose data if unmounting was unsuccessful etc.
    rm -rf /mnt/image

    # Get the loopback device path
    # Get the loop device path
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`

    # Remove the loopback device
    # Remove the loop device
    losetup -d $LOOP_DEV_PATH
    ```
    ```
  2. Dids revised this gist Jul 15, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,7 @@ mkdir -p /mnt/image
    mount $LOOP_DEV_PATH /mnt/image
    ```

    ##### Copy files to /mnt/image etc.
    ##### Working with files on the EFI partition

    At this point you should be able to freely create, edit and remove files mounted under the `/mnt/image` path.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.
  3. Dids revised this gist Jul 15, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -73,6 +73,9 @@ mount $LOOP_DEV_PATH /mnt/image

    ##### Copy files to /mnt/image etc.

    At this point you should be able to freely create, edit and remove files mounted under the `/mnt/image` path.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.

    ##### Unmount the EFI partition
    ```sh
    # Unmount the EFI partition
  4. Dids revised this gist Jul 15, 2021. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@ For example, you might use it as temporary or even portable storage, mountable a

    **DISCLAIMER:** Be _very_ careful with the commands listed below, as you could potentially not only cause data loss, but even prevent your operating system from booting, no matter how unlikely either of those may be. Pay attention to the commands, comments and differences between the guide and your local environment.

    ---

    ### Creating the disk image

    ##### Create a blank disk image
    @@ -115,6 +117,8 @@ mount $LOOP_DEV_PATH /mnt/image
    ls -lah /mnt/image
    ```

    ---

    ##### Working with files on the image

    At this point you should be able to freely create, edit and remove files mounted under the `/mnt/image` path.
  5. Dids revised this gist Jul 15, 2021. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ For example, you might use it as temporary or even portable storage, mountable a

    **DISCLAIMER:** Be _very_ careful with the commands listed below, as you could potentially not only cause data loss, but even prevent your operating system from booting, no matter how unlikely either of those may be. Pay attention to the commands, comments and differences between the guide and your local environment.

    ## Creating the disk image
    ### Creating the disk image

    ##### Create a blank disk image
    ```sh
    @@ -24,7 +24,7 @@ losetup -f image.img
    losetup
    ```

    ### Create the EFI partition on the image
    ##### Create the EFI partition on the image
    ```sh
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`
    @@ -48,7 +48,7 @@ partprobe $LOOP_DEV_PATH
    ls $LOOP_DEV_PATH*
    ```

    ### Format the EFI partition
    ##### Format the EFI partition
    ```sh
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1
    @@ -57,7 +57,7 @@ LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1
    mkfs.fat -F32 $LOOP_DEV_PATH
    ```

    ### Mount the EFI partition
    ##### Mount the EFI partition
    ```sh
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1
    @@ -69,9 +69,9 @@ mkdir -p /mnt/image
    mount $LOOP_DEV_PATH /mnt/image
    ```

    ### Copy files to /mnt/image etc.
    ##### Copy files to /mnt/image etc.

    ### Unmount the EFI partition
    ##### Unmount the EFI partition
    ```sh
    # Unmount the EFI partition
    umount /mnt/image
    @@ -81,7 +81,7 @@ umount /mnt/image
    rm -rf /mnt/image
    ```

    ### Unmount the image/loopback device
    ##### Unmount the image/loopback device
    ```sh
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`
    @@ -93,11 +93,11 @@ losetup -d $LOOP_DEV_PATH
    losetup -l
    ```

    ## Working with the disk image
    ### Working with the disk image

    This section assumes that you no longer have the image mounted in any way. It also assumes your image is properly formatted and you're familiar with its partition layout.

    ### Mount the image
    ##### Mount the image
    ```sh
    # Mount the image as a loopback device first
    # -f searches for the next free loop device (no need to manually select one)
    @@ -115,7 +115,12 @@ mount $LOOP_DEV_PATH /mnt/image
    ls -lah /mnt/image
    ```

    ### Unmount the image
    ##### Working with files on the image

    At this point you should be able to freely create, edit and remove files mounted under the `/mnt/image` path.
    Once you're done, simply continue to the next step, where we will safely and cleanly unmount the partition and image/loopback device.

    ##### Unmount the image
    ```sh
    # Unmount the EFI partition
    umount /mnt/image
  6. Dids revised this gist Jul 15, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ For example, you might use it as temporary or even portable storage, mountable a

    ## Creating the disk image

    #### Create a blank disk image
    ##### Create a blank disk image
    ```sh
    dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
    ```
  7. Dids revised this gist Jul 15, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -9,12 +9,12 @@ For example, you might use it as temporary or even portable storage, mountable a

    ## Creating the disk image

    ### Create a blank disk image
    #### Create a blank disk image
    ```sh
    dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
    ```

    ### Mount the image
    ##### Mount the image
    ```sh
    # Mount the image on the first available loopback device
    losetup -f image.img
  8. Dids revised this gist Jul 15, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@ I originally wanted to create bootable disks for UEFI (i)PXE booting, meaning I
    While this guide serves my purpose well, it's also generic enough to be extended to almost any use case.
    For example, you might use it as temporary or even portable storage, mountable across different operating systems, or you might use it as a disk image for a virtual machine.

    **DISCLAIMER:** Be _very_ careful with the commands listed below, as you could potentially not only cause data loss, but even prevent your operating system from booting, no matter how unlikely either of those may be. Pay attention to the commands, comments and differences between the guide and your local environment.

    ## Creating the disk image

    ### Create a blank disk image
  9. Dids revised this gist Jul 15, 2021. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,11 @@
    # Creating the disk image
    # What is the purpose of this guide?

    I originally wanted to create bootable disks for UEFI (i)PXE booting, meaning I could directly boot premade disk images over the network, no matter what they may contain.

    While this guide serves my purpose well, it's also generic enough to be extended to almost any use case.
    For example, you might use it as temporary or even portable storage, mountable across different operating systems, or you might use it as a disk image for a virtual machine.

    ## Creating the disk image

    ### Create a blank disk image
    ```sh
    @@ -84,7 +91,7 @@ losetup -d $LOOP_DEV_PATH
    losetup -l
    ```

    # Working with the disk image
    ## Working with the disk image

    This section assumes that you no longer have the image mounted in any way. It also assumes your image is properly formatted and you're familiar with its partition layout.

  10. Dids revised this gist Jul 15, 2021. 1 changed file with 52 additions and 19 deletions.
    71 changes: 52 additions & 19 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -6,56 +6,87 @@ dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
    ```

    ### Mount the image
    (losetup will list all used loopback devices)
    (losetup loop4 will mount it on the next available loopback devices, eg n+1)
    ```sh
    # NOTE: For experienced users, you can use "losetup -f image.img" to automatically use the next available loop device
    losetup
    losetup loop4 image.img
    # Mount the image on the first available loopback device
    losetup -f image.img

    # List currently mounted loopback devices
    # (this is just to confirm it was mounted)
    losetup
    ```

    ### Create the EFI partition on the image
    ```sh
    gdisk /dev/loop4
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`

    # Partition the loopback device
    # (enter the commands/characters and press enter)
    gdisk $LOOP_DEV_PATH
    o
    y
    n
    <enter until hex code>
    # <keep clicking enter until it asks for a hex code>
    0xEF00
    w
    y
    partprobe /dev/loop4
    ls /dev/loop4*

    # Trigger partition discovery for the newly partitioned loopback device
    partprobe $LOOP_DEV_PATH

    # List the partitions of the loopback device
    # (this is only for confirming that the partitions are visible)
    ls $LOOP_DEV_PATH*
    ```

    ### Format the EFI partition
    ```sh
    mkfs.fat -F32 /dev/loop4p1
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1

    # Format the EFI partition as FAT32
    mkfs.fat -F32 $LOOP_DEV_PATH
    ```

    ### Mount the EFI partition
    ```sh
    mkdir /mnt/image
    mount /dev/loop4p1 /mnt/image
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1

    # Make sure the target mounting directory exists
    mkdir -p /mnt/image

    # Mount the EFI partition to a local path
    mount $LOOP_DEV_PATH /mnt/image
    ```

    ### Copy files to /mnt/image etc.

    ### Unmount the EFI partition
    ```sh
    # Unmount the EFI partition
    umount /mnt/image

    # Remove the mount point
    # DISCLAIMER: Be careful with this, as you could potentially lose data if unmounting was unsuccessful etc.
    rm -rf /mnt/image
    ```

    ### Unmount the image
    ### Unmount the image/loopback device
    ```sh
    losetup -d /dev/loop4
    losetup
    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`

    # Unmount the loopback device
    losetup -d $LOOP_DEV_PATH

    # Verify that the image is no longer mounted
    losetup -l
    ```

    # Working with the disk image

    This section assumes that you no longer have the image mounted in any way.
    This section assumes that you no longer have the image mounted in any way. It also assumes your image is properly formatted and you're familiar with its partition layout.

    ### Mount the image
    ```sh
    @@ -70,20 +101,22 @@ LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1
    # Mount the EFI partition to a local path (create path first if necessary)
    mkdir -p /mnt/image
    mount $LOOP_DEV_PATH /mnt/image

    # List the image contents to verify that it is mounted correctly
    ls -lah /mnt/image
    ```

    ### Unmount the image
    ```sh
    # Unmount the EFI partition
    umount /mnt/opencore
    umount /mnt/image

    # Remove the mount point
    # DISCLAIMER: Be careful with this, as you could potentially lose data if unmounting was unsuccessful etc.
    rm -rf /mnt/opencore
    rm -rf /mnt/image

    # Get the loopback device path
    LOOP_DEV_PATH=`losetup -a | grep opencore.img | awk -F: '{print $1;}'`
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`

    # Remove the loopback device
    losetup -d $LOOP_DEV_PATH
  11. Dids revised this gist Jul 15, 2021. 1 changed file with 40 additions and 1 deletion.
    41 changes: 40 additions & 1 deletion raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Creating the disk image

    ### Create a blank disk image
    ```sh
    dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
    @@ -7,6 +9,7 @@ dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
    (losetup will list all used loopback devices)
    (losetup loop4 will mount it on the next available loopback devices, eg n+1)
    ```sh
    # NOTE: For experienced users, you can use "losetup -f image.img" to automatically use the next available loop device
    losetup
    losetup loop4 image.img
    losetup
    @@ -48,4 +51,40 @@ umount /mnt/image
    ```sh
    losetup -d /dev/loop4
    losetup
    ```
    ```
    # Working with the disk image
    This section assumes that you no longer have the image mounted in any way.
    ### Mount the image
    ```sh
    # Mount the image as a loopback device first
    # -f searches for the next free loop device (no need to manually select one)
    # -P triggers a scan for any available partitions in the image
    losetup -f -P image.img

    # Get the loopback device for the mounted image
    LOOP_DEV_PATH=`losetup -a | grep image.img | awk -F: '{print $1;}'`p1

    # Mount the EFI partition to a local path (create path first if necessary)
    mkdir -p /mnt/image
    mount $LOOP_DEV_PATH /mnt/image
    ls -lah /mnt/image
    ```
    ### Unmount the image
    ```sh
    # Unmount the EFI partition
    umount /mnt/opencore

    # Remove the mount point
    # DISCLAIMER: Be careful with this, as you could potentially lose data if unmounting was unsuccessful etc.
    rm -rf /mnt/opencore

    # Get the loopback device path
    LOOP_DEV_PATH=`losetup -a | grep opencore.img | awk -F: '{print $1;}'`

    # Remove the loopback device
    losetup -d $LOOP_DEV_PATH
    ```
  12. Dids revised this gist Jul 14, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    ### Create a blank disk image
    ```sh
    dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
    ```

    ### Mount the image
    (losetup will list all used loopback devices)
  13. Dids revised this gist Jul 14, 2021. 1 changed file with 22 additions and 10 deletions.
    32 changes: 22 additions & 10 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,17 @@
    # Create a blank disk image
    ### Create a blank disk image
    dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync

    # Mount the image
    # (losetup will list all used loopback devices)
    # (losetup loop4 will mount it on the next available loopback devices, eg n+1)
    ### Mount the image
    (losetup will list all used loopback devices)
    (losetup loop4 will mount it on the next available loopback devices, eg n+1)
    ```sh
    losetup
    losetup loop4 image.img
    losetup
    ```

    # Create the EFI partition on the image
    ### Create the EFI partition on the image
    ```sh
    gdisk /dev/loop4
    o
    y
    @@ -19,19 +22,28 @@ w
    y
    partprobe /dev/loop4
    ls /dev/loop4*
    ```
    # Format the EFI partition
    ### Format the EFI partition
    ```sh
    mkfs.fat -F32 /dev/loop4p1
    ```
    # Mount the EFI partition
    ### Mount the EFI partition
    ```sh
    mkdir /mnt/image
    mount /dev/loop4p1 /mnt/image
    ```
    # Copy files to /mnt/image etc.
    ### Copy files to /mnt/image etc.
    # Unmount the EFI partition
    ### Unmount the EFI partition
    ```sh
    umount /mnt/image
    ```
    # Unmount the image
    ### Unmount the image
    ```sh
    losetup -d /dev/loop4
    losetup
    ```
  14. Dids created this gist Jul 14, 2021.
    37 changes: 37 additions & 0 deletions raw_img_gpt_efi.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # Create a blank disk image
    dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync

    # Mount the image
    # (losetup will list all used loopback devices)
    # (losetup loop4 will mount it on the next available loopback devices, eg n+1)
    losetup
    losetup loop4 image.img
    losetup

    # Create the EFI partition on the image
    gdisk /dev/loop4
    o
    y
    n
    <enter until hex code>
    0xEF00
    w
    y
    partprobe /dev/loop4
    ls /dev/loop4*

    # Format the EFI partition
    mkfs.fat -F32 /dev/loop4p1

    # Mount the EFI partition
    mkdir /mnt/image
    mount /dev/loop4p1 /mnt/image

    # Copy files to /mnt/image etc.

    # Unmount the EFI partition
    umount /mnt/image

    # Unmount the image
    losetup -d /dev/loop4
    losetup