squashfs howto - make changes the read-only filesystem in OpenELEC

After installing the squashfs-tools addon (and reboot) you can make changes to the read-only part of your OpenELEC installation using only a spare usb stick.
You can do this steps from an ssh session or from OpenELEC machine if you enable debugging in extlinux.conf like this:

mount -o remount,rw /flash
nano /flash/extlinux.conf
(you must have nano addon installed)

and add the word ''debugging" (without quotes) in the last line after the word "quiet" (also without quotes). After rebooting with Ctrl+Alt+F3 you will have a command line terminal to go back to XBMC Ctrl+Alt+F1.
Before inserting the flash disk to OpenELEC backup what you have on it because everything will be wiped out from the stick.

First step is to identify your device.
Without the stick inserted look what devices are mounted in your machine.

df -h

you will have something like this

Filesystem      Size          Used        Available        Use%        Mounted on
none            1.4G        332.0K             1.4G          0%        /dev
/dev/sdc1     173.3M        131.9M            32.5M         80%        /flash
/dev/sdc2       1.6G         58.9M             1.5G          4%        /storage
/dev/loop     121.9M        121.9M                0        100%                /
none            1.4G             0             1.4G          0%        /dev/shm
none            1.4G        328.0K             1.4G          0%        /run
/dev/sda4      83.2G         77.4G             5.8G         93%        /var/media/…
/dev/sda1       100M         24.9M            75.1M         25%                /var/media/…
/dev/sda3     136.7G        124.9G            11.8G         91%                /var/media/…
/dev/sda2      78.0G         66.7G            11.3G         85%        /var/media/…

Insert your stick and wait for mounting and issue the same command

 df -h

you will see that your device is added

Filesystem      Size          Used        Available        Use%        Mounted on
none            1.4G        332.0K             1.4G          0%        /dev
/dev/sdc1     173.3M        131.9M            32.5M         80%        /flash
/dev/sdc2       1.6G         58.9M             1.5G          4%        /storage
/dev/loop     121.9M        121.9M                0                100%        /
none            1.4G             0             1.4G          0%        /dev/shm
none            1.4G        328.0K             1.4G          0%        /run
/dev/sda4      83.2G         77.4G             5.8G         93%        /var/media/…
/dev/sda1       100M         24.9M            75.1M         25%        /var/media/…
/dev/sda3     136.7G        124.9G            11.8G         91%        /var/media/…
/dev/sda2      78.0G         66.7G            11.3G         85%        /var/media/…
/dev/sdd1       1.9G             0             1.9G          0%        /var/media/…

The last device /dev/sdd1 is your stick. Check very carefuly to not make some stupid things and erase your hdd content; you see that a 2GB flash has been inserted.
The listings above are from my laptop and they are the windows partitions /dev/sda4, /dev/sda1, /dev/sda3 and /dev/sda2 but also OpenELEC partitions /dev/sdc1 and /dev/sdc2. For sure I will not touch these partitions.
My interest is only with /dev/sdd1.
Now unmount de flash.

umount /dev/sdd1                                                           

The next step is to format the flash to ext4.
mkfs.ext4 /dev/sdd –L TEST                                                 

the system will react with

/dev/sdd is entire device, not just one partition!
Proceed anyway (y,n)

Press y

If you have forgotten to unmount the device the formatting will not take place

/dev/sdd is mounted; will not make a filesystem here!

so unmount the stick and try again.

After formatting is completed, your flash will be automounted and you can change the directory to your new filesystem.

If your flash is not automounted 

mount /dev/sdd /media/TEST


cd /media/TEST/

Make two new directories

mkdir source
mkdir system.new

source will host the original filesystem (read-only) and system.new will have a writable copy of it.

Now we can mount the squashfs file system.

mount -t squashfs -o loop /flash/SYSTEM /media/TEST/source/

change directory to source
cd source

and copy everything to system.new
cp -a * /media/TEST/system.new

You can unmount now the source filesystem

umount /media/TEST/source/

This is the moment when you can make your changes the way you would like - for an example look at the bluez-tools howto.

After you finished, step back to your flash root. Then it's time to pack your new system.

cd /media/TEST
mksquashfs /media/system.new SYSTEM.new

for RaspberryPi

mksquashfs /media/system.new SYSTEM.new -comp lzo (compressing will take about half an hour! so be patient)

and finally you can prepare to update your OpenELEC machine

cp SYSTEM.new /storage/.update/SYSTEM


cp /flash/KERNEL /storage/.update

for Raspberry Pi

cp /flash/kernel.img /storage/update/KERNEL

so we have now the new SYSTEM file and KERNEL file in update directory.

For recent OpenELEC releases it is necessary to have md5 checksums for the SYSTEM and KERNEL files so we must generate them.

md5sum /storage/.update/SYSTEM > /storage/.update/SYSTEM.md5
md5sum /storage/.update/KERNEL > /storage/.update/KERNEL.md5

and reboot, the system will update, that's all.

P.S. Take care what changes you will make to not end up with an unbootable system, but if it will happen you must reinstall your system so before you update you should save your settings; for example you can save the whole storage partition (if you have enough free space on the TEST flash) or you can copy only the .config and .xbmc directories (where the most important user settings resides)

mkdir /media/TEST/settings_backup

#backup whole storage partition
cp -a /storage /media/TEST/settings_backup

#backup .xbmc and .config directories
cp -a /storage/.xbmc /media/TEST/settings_backup/.xbmc
cp -a /storage/.config /media/TEST/settings_backup/.config

hits counter