Creating rootfs.tar.gz from Linux LiveCD

While the user facing part of LiveCD works mostly the same across Linux distros, the internals can be more or less different. That's why in this text I'll present how to extract filesystem from two distributions: Debian and Fedora. That ought to give anyone enough overview to reproduce for any other distribution.



sudo mkdir /mnt/iso
sudo mount -o loop debian-live-12.1.0-amd64-standard.iso /mnt/iso
cd /mnt/iso
sudo unsquashfs -d /tmp/squashfs ./live/filesystem.squashfs
cd /tmp/squashfs
sudo tar -vzcp -f ~/rootfs.tar.gz .
sudo chown $USER:$USER ~/rootfs.tar.gz
cd ~
sudo umount /mnt/iso
sudo rmdir /mnt/iso
sudo rm -r /tmp/squashfs


sudo mkdir /mnt/iso
sudo mount -o loop Fedora-Workstation-Live-x86_64-38-1.6.iso /mnt/iso
cd /mnt/iso
sudo unsquashfs -d /tmp/squashfs ./LiveOS/squashfs.img
cd /tmp/squashfs
sudo mkdir /mnt/rootfs
sudo mount -o loop LiveOS/rootfs.img /mnt/rootfs
cd /mnt/rootfs
sudo tar -vzcp -f ~/rootfs.tar.gz .
sudo chown $USER:$USER ~/rootfs.tar.gz
cd ~
sudo umount /mnt/iso
sudo umount /mnt/rootfs
sudo rmdir /mnt/iso /mnt/rootfs
sudo rm -r /tmp/squashfs

Prerequisites #

Step-by-step #

Get the LiveCDs #

This step is self-explanatory.

As of writing those words the latest ISOs are:

Mounting ISOs #

First we need to create mountpoints:

$ sudo mkdir /mnt/debian
$ sudo mkdir /mnt/fedora

(it doesn't need to be /mnt/iso, you can as well use ~/foo or something else)

The syntax for mounting an ISO file is: mount -o loop disk1.iso /mnt/disk

Therefore let's mount our images:

$ sudo mount -o loop debian-live-12.1.0-amd64-standard.iso /mnt/debian
$ sudo mount -o loop Fedora-Workstation-Live-x86_64-38-1.6.iso /mnt/fedora

Finding SquashFS #

$ find /mnt/debian -name '*squashfs*'
$ find /mnt/fedora -name '*squashfs*'

As we can see, we have:

Those are out files of interest.

Unsquashing #

Now let's uncompress the files; I'll extract them in $TMPDIR (be sure it has sufficient size or choose different location).

$ TMPDIR="${TMPDIR:-/tmp}"
$ sudo unsquashfs -d "$TMPDIR"/debian /mnt/debian/live/filesystem.squashfs
$ sudo unsquashfs -d "$TMPDIR"/fedora /mnt/fedora/LiveOS/squashfs.img

Let's have a look at what we got so far:

$ ls "$TMPDIR"/debian
bin@   etc/         lib@    libx32@  opt/   run/   sys/  var/
boot/  home/        lib32@  media/   proc/  sbin@  tmp/  vmlinuz@
dev/   initrd.img@  lib64@  mnt/     root/  srv/   usr/
$ ls "$TMPDIR"/fedora
$ ls "$TMPDIR"/fedora/LiveOS

Mounting (Fedora) filesystem image #

As we seen in previous step, for Debian we already dug out its filesystem, but there's only rootfs.img file for Fedora. We can mount it as we did with ISOs to reach its content.

$ sudo mkdir /mnt/fedora_fs
$ sudo mount -o loop "$TMPDIR"/fedora/LiveOS/rootfs.img /mnt/fedora_fs
$ ls /mnt/fedora_fs
afs/  boot/  etc/   lib@    lost+found/  mnt/  proc/  run/   srv/  tmp/  var/
bin@  dev/   home/  lib64@  media/       opt/  root/  sbin@  sys/  usr/

Creating tarballs #

Now all that's left is just archiving the filesystems into rootfs.tar.gz files:

$ cd "$TMPDIR"/debian
$ sudo tar -vzcp -f ~/debian.tar.gz .
$ sudo chown $USER:$USER ~/debian.tar.gz
$ cd /mnt/fedora_fs
$ sudo tar -vzcp -f ~/fedora.tar.gz .
$ sudo chown $USER:$USER ~/fedora.tar.gz

Cleanup #

$ sudo umount /mnt/debian /mnt/fedora /mnt/fedora_fs
$ sudo rmdir /mnt/debian /mnt/fedora /mnt/fedora_fs
$ sudo rm -r "$TMPDIR"/debian "$TMPDIR"/fedora

Done #

Great! Now that we know how get filesystem from Linux LiveCD ISO into rootfs.tar.gz archive, we can use it to e.g. create an instance of Windows Subsystem for Linux (WSL).