TS-7800-V2 Stretch RO/RW partition

From embeddedTS Manuals

While keeping the OS read/write it may be beneficial to keep one partition read/write to save log or configuration data. Our default images use one partition for the "/" partition including all of the Linux rootfs, but this example will resize that partition and create one other for data. First, boot to the disk and check the size of the partition:

Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/root           3487  1484      1807  46% /
...

In this case the /dev/root filesystem is using 1484MB. As an example we will resize the filesystem to 1750MB which leave some room to grow if new packages are needed.

Use fdisk to get the sector count of the emmc: root@ts7800-v2:~# fdisk -l /dev/mmcblk0 Disk /dev/mmcblk0: 3.5 GiB, 3783262208 bytes, 7389184 sectors

Take a backup copy of the eMMC in a tar as described here. Copy this to your Linux workstation and the new image will be created there.

Create a file on your workstation the same size as the eMMC: dd if=/dev/zero bs=512 count=7389184 of=my-image.dd

LOOPDEV=$(losetup -f)
sudo losetup $LOOPDEV my-image.dd
sudo fdisk $LOOPDEV

Set up your partitions to your rootfs image size, and make the second partition cover the rest of the disk.

Welcome to fdisk (util-linux 2.30.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x8e8595de.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-7389183, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-7389183, default 7389183): +1750M

Created a new partition 1 of type 'Linux' and of size 1.7 GiB.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (3586048-7389183, default 3586048): 
Last sector, +sectors or +size{K,M,G,T,P} (3586048-7389183, default 7389183): 

Created a new partition 2 of type 'Linux' and of size 1.8 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

Detach the loopback device and recreate it with kpartx which will understand these new partitions.

sudo losetup -d $LOOPDEV
sudo kpartx -va my-image.dd

In my case this created loop1p1/loop1p2:

add map loop1p1 (253:0): 0 3584000 linear 7:1 2048
add map loop1p2 (253:1): 0 3803136 linear 7:1 3586048

Create the filesystems and extract your image:

sudo mkfs.ext4 /dev/mapper/loop1p1
sudo mkfs.ext4 /dev/mapper/loop1p2
sudo mkdir /mnt/emmc
sudo mount /dev/mapper/loop1p1 /mnt/emmc
sudo tar -xf /path/to/your/image.tar.xz -C /mnt/emmc
sudo mkdir /mnt/emmc/srv/

Open the /mnt/emmc/etc/fstab and add a line:

 /dev/mmcblk0p2   /srv          auto    ro                 0    2

This will mount the new partition automatically on startup.

sudo umount /mnt/emmc
sudo kpartx -d my-image.dd
xz my-image.dd

Write this my-image.dd.xz to your eMMC as described here. On startup the new data partition will be mounted at /srv. Make this partition read/write with:

mount -o remount,rw /srv/

# Whenever possible, make read only when done writing
mount -o remount,ro /srv/

The remount to read only will fail if there are any open file handles on this disk. See the lsof tool for more information on tracking down open file handles.