How-to: Modify the standard TS7800 disk image

The problem: you need to add static files, such as compiled binaries, to an existing image, before burning it to an SD card, where it mounts read-only on your TS7800 (or SBC of choice).

The solution: use the loop device.

First off, hats off, mad props, kudos, and all that to CP, for pointing me on the path to this discovery. It's a simple idea that should be obvious, but there is little discussion that I can find from brief Internet searches on this topic (except this excellent post about constructing an image for the BeagleBone Black). While my goal was to modify an existing image for the Technologic TS7800, you should be able to use this method to modify existing images for other SBCs as well, besides all the other boards Technologic produces.

What you will need

  • A Linux system, virtualized or no, where you have superuser powers; I used Utopic Unicorn
  • util-linux (possibly already installed, but use your package manager to find out)
  • A system image (latest for TS7800 is here)

Steps

1. Unpack the system image.

$ bzip2 -d 512mbsd-wheezy-2.6.34-mar-10-2015.dd.bz2


2. View the partition table.

$ fdisk -l 512mbsd-wheezy-2.6.34-mar-10-2015.dd  

Disk 512mbsd-wheezy-2.6.34-mar-10-2015.dd: 512 MiB, 536870912 bytes, 1048576 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: 0x00000000  

Device                                Boot Start     End Sectors   Size Id Type  
512mbsd-wheezy-2.6.34-mar-10-2015.dd1        256    8447    8192     4M  c W95 FAT32 (LBA)  
512mbsd-wheezy-2.6.34-mar-10-2015.dd2       8448   14591    6144     3M da Non-FS data  
512mbsd-wheezy-2.6.34-mar-10-2015.dd3      16640   22784    6145     3M da Non-FS data  
512mbsd-wheezy-2.6.34-mar-10-2015.dd4      22786 1048575 1025790 500.9M 83 Linux  


3. Take note of the Start and End sectors of the partition you want to mount, as well as the sector size. In our case, we want to mount the fourth partition, so:
- Start: 22786
- End: 1048575
- Sector size: 512 bytes
4. Multiply the Start sector number by the sector size to calculate the starting offset in bytes. Do the same for the End sector number.
- Starting offset: 22786 * 512 = 11666432
- End: 1048575 * 512 = 536870400
5. Use the values you just calculated to mount the partition to the loop device using losetup. On Ubuntu, this mounts at /media/username/disklabel.

$ sudo losetup --offset 11666432 --sizelimit 536870400 -f 512mbsd-wheezy-2.6.34-mar-10-2015.dd


6. Make your modifications to the mounted filesystem.
7. Unmount the partition.

$ sudo umount /media/_username_/_disklabel_  


8. Tell losetup to detach the partition.

$ sudo losetup -d /dev/loop0

Where to go from here

Now that your image is modified, you can write it to your SD card and fire up your TS7800.

There's obviously a lot more potential here than just adding a few files to an existing image; as the post referenced above mentions, there are several use cases for this, not the least of which is creating an automated build pipeline that results in a usable image for your SBC.