Skip to content

Instantly share code, notes, and snippets.

@Dids
Last active January 11, 2024 12:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dids/c6fa12602ae812e873db539f71c52a3a to your computer and use it in GitHub Desktop.
Save Dids/c6fa12602ae812e873db539f71c52a3a to your computer and use it in GitHub Desktop.
Working with raw disk images with GPT + EFI partition in linux

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.

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
dd if=/dev/zero of=image.img iflag=fullblock bs=1M count=100 && sync
Mount the image
# 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
# 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
# <keep clicking enter until it asks for a hex code>
0xEF00
w
y

# 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
# 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
# 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
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.

Unmount the EFI partition
# 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/loopback device
# 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. It also assumes your image is properly formatted and you're familiar with its partition layout.

Mount the image
# 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

# List the image contents to verify that it is mounted correctly
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.

Unmount the image
# 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

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

# Remove the loopback device
losetup -d $LOOP_DEV_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment