62

I have a Clonezilla installation on a USB stick and I'd like to make some modifications to the operating system. Specifically, I'd like to insert a runnable script into /usr/sbin to make it easy to run my own backup command to make backups less painful.

The main filesystem lives under /live/filesystem.squashfs on the USB FAT-32 partition.

How can I mount this read/write on my Linux machine in order to be able to add/remove/change files? I'm running an Ubuntu 12.04 derivative.

Naftuli Kay
  • 38,686
  • 85
  • 220
  • 311

6 Answers6

63

This assumes that you are root and that squashfs-tools is installed on your system:


Copy filesystem.squashfs to some empty dir, e.g.:

cp /path/to/filesystem.squashfs /path/to/workdir
cd /path/to/workdir

Unpack the file then move it somewhere else (so you still have it as a backup):

unsquashfs filesystem.squashfs
mv filesystem.squashfs /path/to/backup/

Go in squashfs-root, add/modify as per your taste then recreate1 filesystem.squashfs:

cd /path/to/workdir
mksquashfs squashfs-root filesystem.squashfs

Copy the newly created filesystem.squashfs over the existing one on your USB drive, e.g.:

cp filesystem.squashfs /mnt/clonezilla/live/

then reboot and use your LIVE USB.


1: Consult the manual for additional options that you can pass, like -b 4M -comp lz4 or -comp xz -Xbcj x86 etc

don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • This works great, but unfortunately, I get a message for all the root directories telling me `Source directory entry bin already used! - trying bin_1`. Sure enough, in my output filesystem, I have a `/bin` and a `/bin_1`, rather than merging the folder. Any ideas? If I run with `-noappend,` the filesystem simply doesn't work. – Naftuli Kay Jun 23 '13 at 19:25
  • 1
    @TKKocheran - I'm not getting any of those errors here after adding a custom script in `/usr/bin` and repacking with `mksquashfs`. USB drive boots fine and I can use my script from the live session. Make sure you no longer have the old `filesystem.squashfs` in the same directory with your modified `squashfs-root` before running `mksquashfs`. – don_crissti Jun 23 '13 at 19:53
  • 1
    @Naftuli: You seem to have skipped the "mv ... backup/" step. – Joachim Wagner Jun 14 '18 at 07:14
  • 1
    Does this work not under (fake)root? Just from extracting I get the error `create_inode: could not create character device squashfs-root/dev/audio, because you're not superuser!` – Wilf Aug 08 '18 at 09:43
  • @Wilf - you cannot create device files (`squashfs-root/dev/*`) as regular user, you need root privileges. – don_crissti Aug 08 '18 at 10:26
  • 1
    Why did you specify all those flags to `mksquashfs`? I read the [man page](https://manpages.debian.org/jessie/squashfs-tools/mksquashfs.1.en.html) but are they needed? – lucidbrot Apr 06 '20 at 19:19
  • If I `unsquash` my debian live iso on an Ubuntu 20 system I get errors for symlinks and also for some files. Is this due to my host system not permitting the symlink or is this a problem with `unsquash`? The errors are like `create_inode: failed to create symlink squashfs-root/etc/alternatives/xinput-zh_TW, because Operation not permitted` – bomben Dec 05 '21 at 12:22
13

If your system supports some uion-filesystem, such as aufs or overlayfs, you don't have to extract your original squashfs file.

For example the overlayfs is used( a kernel option to enable it): You can mount your squashfs.file to /fm or somewhere else first. Prepare a writable filesystem with 2 directories in it, say /to and /temp. prepare another writable directory /fin for the merged results. Mount them together as an overlayfs to your system ---

mount -t overlay -o lowerdir=/fm,upperdir=/to,workdir=/temp overlay /fin

Now you can add/modify files in /fin. Once everything done, you can mksquashfs /fin to a new squashfs file,

mksquashfs /fin newfile; umount /fin

, then clear/unmount all the other used directories as you will.

The squashfs and some unionfs are commonly used for a live-cd.

Zhi-Ming Chen
  • 131
  • 1
  • 3
8

Here, I found an other answer:

bash# mount dir.sqsh /mnt/dir -t squashfs -o loop
User
  • 197
  • 1
  • 3
  • 8
    The above command will mount it read-only, which is better than not mounting it at all; alas, not a complete answer to the question at hand. – harperville Mar 23 '17 at 16:25
  • I agree that it is not a complete answer. Together with https://unix.stackexchange.com/a/377269/27328 you can build an answer where you do not need to unpack your file system to modify it - less space, less time. – User Mar 02 '19 at 19:16
  • Simplest method, worked on CentOS 7 – Seff Apr 11 '19 at 06:02
  • would be good if you added needed steps to make is writable. For some (like me) seeing "together with" means lots of additional work to do (though might be beneficial for learning). – Martian2020 Nov 28 '21 at 00:11
1

Note: question written in 2013, now is 2021, I assume overlayfs (one of unionfs filesystems) is supported. This answer is basically merge of two other answers with some things written explicitly, proficient Linux users might see something as obvious (like using sudo), but not everybody is at that level, I've understood some things along the way and writing complete (IMO) instructions. Texts after # are comments, no need to copy them, on my system bash safely ignores them.

cd somefolder # some folder, no need for much free space, enough for modified data only
mkdir fm # for mounting original
mkdir to # for upper unionfs layers
mkdir temp # some overlayfs technical folder
mkdir fin # resulting folders/files would be there

sudo mount /full_path/filesystem.squashfs fm -t squashfs -o loop
sudo mount -t overlay -o lowerdir=fm,upperdir=to,workdir=temp overlay fin

Now can modify/add/delete files/folders in either "to" or "fin" folders. Changes to them are "mirrored".
To undo deletion of original file delete "deleted" file from "to" with sudo rm path/file.
After done with modifications to make new squashfs file in full_path folder, needs to be free space there:

sudo mksquashfs fin /full_path/filesystem.squashfs 

When you don't need your working files anymore:

sudo umount fin
sudo umount fm
sudo rm -R fm fin temp to

P.S. After change to quashfs I wanted to recreate iso file of modern distro which support both legacy and EFI boot. Why some options to below genisoimage command are critical, I don't know, for me I was trial-and-error way. Boots both EFI and legacy, however start of iso is different: starts 33 ed 90 instead of 45 52 08, e.g. mjg59.dreamwidth.org/11285.html hints me Apple support is missing.

mkdir iso,efi

sudo losetup --partscan --show --find original.iso

# if output of previous loop0
sudo mount /dev/loop0p1 iso
sudo mount /dev/loop0p2 efi # not necessary, just to see contents

sudo mount -t overlay -o lowerdir=iso,upperdir=to,workdir=temp overlay fin

Replace what is needed in fin. Initially did sudo dd if=/dev/loop0p2 fin/EFI/BOOT/usb.efi to make image for efi, then found out it is already present in grub folder. If one takes available efi image, than losetup+mount /dev/loop steps can be replaced by simpler sudo mount original.iso iso

sudo genisoimage -lJr -o new.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 --boot-info-table -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot fin

sudo isohybrid --uefi new.iso
Martian2020
  • 1,039
  • 7
  • 20
1

Here is a script based on Martian2020's answer that can do this using overlayfs. I am creating an fstab file so that it is easy to restore using mount -a --fstab

#!/bin/bash
set -eu
src=$(realpath $1)
tgt_dir=$(dirname $(realpath ${2-$src}))
tmp=${2-$(dirname $src)/$(basename --suffix=.squashfs $src)}
tgt_name=$(basename $tmp)
target=$tgt_dir/$tgt_name
workdir=$tgt_dir/.$tgt_name-work
lowerdir=$tgt_dir/.$tgt_name-lower
upperdir=$tgt_dir/.$tgt_name-upper
fstab=$target/fstab
echo $src $target $workdir $lowerdir $upperdir
set -x
mkdir $workdir
mkdir $upperdir
mkdir $lowerdir
mkdir $target
cat > $fstab <<EOT
# This fstab can be used with "sudo mount --fstab $fstab -a" or appended to
# /etc/fstab
$src $lowerdir squashfs loop,ro 0 0
overlay $target overlay lowerdir=$lowerdir,upperdir=$upperdir,workdir=$workdir 0 0
EOT
sudo mount --fstab $fstab -a

0

Using overlayfs as shown is the best way to have pseudo "squashfs rw" ; It requires however to run on > 4.x kernel (or ubuntu >14.x trusty ).

An alternative solution when one is sitting on older live cd without any overlayfs/aufs/unionfs is to make use of squashfs'own capabilities

Important: Without unsquashfs, so this can be done on low storage system

Example:

Modify squashfs's "usr" directory

mount squashfs_file /mnt    # 1
cp -a /mnt/usr $HOME        # 2  Modify whatever $HOME/usr as needed
mksquashfs /mnt new_squashfs_file -wildcards -e usr   # 3
mksquashfs $HOME/usr new_squashfs_file -keep-as-directory # 4
umount /mnt  # 5  Cleanup

Line 3 builds temporarily squashfsfile excluding olddir_usr
Line 4 appends modified-usr-dir into new_squashfsfile

See here append squashfs

muru
  • 69,900
  • 13
  • 192
  • 292