Hi ashfame
... I need an automated way of preparing the disk image that's ready to boot for my use-case. Thank you!
The first thing you need to do is expand the second partition in the image file to make room for more extensions. I'm using a copy of
piCore-9.0.3.img to demonstrate the steps required. Adjust sizes for your own needs. You will need the following extensions for this:
parted.tcz
util-linux.tcz
These are the characteristics of the original image:
tc@E310:~/PiCore/TC9$ fdisk -l piCore-9.0.3.img
Disk piCore-9.0.3.img: 49 MiB, 51380224 bytes, 100352 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0009bf4f
Device Boot Start End Sectors Size Id Type
piCore-9.0.3.img1 8192 77823 69632 34M c W95 FAT32 (LBA)
piCore-9.0.3.img2 77824 100351 22528 11M 83 Linux
tc@E310:~/PiCore/TC9$
It is 49M in size. 4M + 34M + 11M = 49M. The 4M comes from partition 1 being offset 4M from the beginning of the image.
First we create a destination for a new image about 60M in size:
tc@E310:~/PiCore/TC9$ dd if=/dev/zero of=1.img bs=1K count=60176
60176+0 records in
60176+0 records out
61620224 bytes (62 MB, 59 MiB) copied, 0.690125 s, 89.3 MB/s
tc@E310:~/PiCore/TC9$
Next we loop mount the file as a device:
tc@E310:~/PiCore/TC9$ sudo losetup --show --find --partscan 1.img
/dev/loop332
tc@E310:~/PiCore/TC9$
Then copy the original image to the larger file:
tc@E310:~/PiCore/TC9$ dd if=piCore-9.0.3.img of=/dev/loop332
100352+0 records in
100352+0 records out
51380224 bytes (51 MB, 49 MiB) copied, 1.58843 s, 32.3 MB/s
tc@E310:~/PiCore/TC9$
This is the result. The new image has the same characteristics as the original except it's in a larger file.
tc@E310:~/PiCore/TC9$ fdisk -l /dev/loop332
Disk /dev/loop332: 58.8 MiB, 61620224 bytes, 120352 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0009bf4f
Device Boot Start End Sectors Size Id Type
/dev/loop332p1 8192 77823 69632 34M c W95 FAT32 (LBA)
/dev/loop332p2 77824 100351 22528 11M 83 Linux
tc@E310:~/PiCore/TC9$
Expand partition 2 to the end of the file:
tc@E310:~/PiCore/TC9$ sudo parted -s /dev/loop332 resizepart 2 100%
This needs to be done before expanding the filesystem to inform the kernel the MBR changed:
tc@E310:~/PiCore/TC9$ e2fsck -f /dev/loop332p2
e2fsck 1.44.4 (18-Aug-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop332p2: 55/2816 files (1.8% non-contiguous), 9660/11264 blocks
tc@E310:~/PiCore/TC9$
Finally, we expand the filesystem:
tc@E310:~/PiCore/TC9$ resize2fs -p /dev/loop332p2
resize2fs 1.44.4 (18-Aug-2018)
Resizing the filesystem on /dev/loop332p2 to 21264 (1k) blocks.
The filesystem on /dev/loop332p2 is now 21264 (1k) blocks long.
tc@E310:~/PiCore/TC9$
Checking the new image you'll see partition 2 has been expanded from 11M to 20.8M:
tc@E310:~/PiCore/TC9$ fdisk -l 1.img
Disk 1.img: 58.8 MiB, 61620224 bytes, 120352 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0009bf4f
Device Boot Start End Sectors Size Id Type
1.img1 8192 77823 69632 34M c W95 FAT32 (LBA)
1.img2 77824 120351 42528 20.8M 83 Linux
tc@E310:~/PiCore/TC9$
... So how do I install an extension while modifying the image file, prior to writing files to the SD card? And when an extension is installed, does it still reside on the second partition (tce/optional)?
Mount the second partition:
tc@E310:~/PiCore/TC9$ mkdir p2
tc@E310:~/PiCore/TC9$ sudo mount /dev/loop332p2 p2
tc@E310:~/PiCore/TC9$
Now you can add extensions to p2/tce/optional and update p2/tce/onboot.lst.
When you are done, unmount partition 2 and release the loop device:
tc@E310:~/PiCore/TC9$ sudo umount p2
tc@E310:~/PiCore/TC9$ sudo losetup -d /dev/loop332
[EDIT]: Added required extensions list to instructions. Rich
[EDIT]: Added missing sudo prefix to the losetup commands. Rich