Linux – Layouting a disk image and copying files into it

command linegptlinuxpartitioning

Before asking here, I tried to search on SU/SE and Google without success. This is the closest thing I got.

Basically, I'm trying to do the following:
– create a disk image of about 128MB;
– create a GPT;
– create a FAT32 partition in the first 32MB(I'll call it UEFI);
– create a FAT32 partition in the remaining bytes(I'll call it SYSTEM);
– create the following subdirectory in the UEFI partition: /EFI/BOOT/;
– copy a file in the subdirectory;
– copy another file in the SYSTEM partition.

This what I've done until now:

dd if=/dev/zero of=disk.img bs=1M count=128

parted disk.img
(parted) mktable gpt
(parted) mkpart primary fat32 2048s 65535s
(parted) mkpart primary fat32 65536s 100%
(parted) align-check optimal 1
(parted) align-check optimal 2
(parted) name 1 UEFI
(parted) name 2 SYSTEM

This is my output:

Disk disk.img: 134MB  
Sector size (logical/physical): 512B/512B  
Partition Table: gpt  
Disk Flags:  

Number  Start   End     Size    File system  Name    Flags  
 1      1049kB  33,6MB  32,5MB               UEFI    msftdata
 2      33,6MB  134MB   101MB                SYSTEM  msftdata

The alternate way is to create two different partitions and later merge them:

dd if=/dev/zero of=uefi.img bs=1M count=32
mformat -i uefi.img -v UEFI -f 1440 -c 1 ::
mmd -i uefi.img ::/EFI
mmd -i uefi.img ::/EFI/BOOT
mcopy -i uefi.img BOOTX64.EFI ::/EFI/BOOT
dd if=/dev/zero of=system.img bs=1M count=100
mformat -i system.img -v UEFI -f 1440 -c 1 ::
mcopy -i system.img myawesomeOS ::

In the former way I cannot figure out why the "File system" column is empty(it should say fat32) and how to create subdirectories and copy files.
In the latter way I am not able to create a proper FAT32 filesystem for the two images, if I append -F to mformat I get

Too few clusters for this fat size. Please choose a 16-bit fat in your /etc/mtools.conf or .mtoolsrc file

If I'd manage to do it, I still don't know how to set up the GPT and merge the two images in a single file.

How can I achieve this? Any help or hint is appreciated.

Best Answer

After struggling for a couple of hours, I solved it by myself. I'll post the solution here, just in case:

# Creates an empty disk image of 256MB, creates its GPT, and then it makes
# two partitions: the first one of approximatively 60MB, the second one fills
# the remaining bytes. Both partitions are aligned.
dd if=/dev/zero of=disk.img bs=1M count=256
parted disk.img
mktable gpt
mkpart primary fat32 2048s 131071s
mkpart primary fat32 131072s 100%
align-check optimal 1
align-check optimal 2
name 1 UEFI
name 2 SYSTEM
quit

# Creates an empty disk image of 33MB.
dd if=/dev/zero of=uefi.img bs=1M count=33
# Formats the disk image as FAT32
mkfs.vfat uefi.img -F 32
# Creates the requested subdirectories.
mmd -i uefi.img ::/EFI
mmd -i uefi.img ::/EFI/BOOT
# Copies the boot file.
mcopy -i uefi.img BOOTX64.EFI ::/EFI/BOOT

# Creates an empty disk image of 100MB.
dd if=/dev/zero of=system.img bs=1M count=100
# Formats the disk image as FAT32
mkfs.vfat system.img -F 32
# Copies the OS file.
mcopy -i system.img SYSTEM.EFI ::

# Enables the kernel module requested by kpartx, just in case.
sudo modprobe dm-mod
# Maps and mounts the two partitions in disk.img to /dev/mapper/loop0p1 and
# /dev/mapper/loop0p2
sudo kpartx -av disk.img
# Copies the UEFI partion into the disk image.
sudo dd if=uefi.img of=/dev/mapper/loop0p1 bs=1M
# Copies the SYSTEM partion into the disk image.
sudo dd if=system.img of=/dev/mapper/loop0p2 bs=1M
# Unmounts the two partitions
sudo kpartx -dv disk.img
Related Question