When I create a squashfs image from multiple directories such as:

  • /bin/
  • /usr/bin/
  • /usr/local/bin/
  • /some/other/random/folder/
  • /another/long/path/

If I use command...

mksquashfs /bin /usr/bin /usr/local/bin /some/other/random/folder /another/long/path MyNewImage.squashfs

It gives me an image with these top-level folders:

  • bin/
  • bin_1/
  • bin_2/
  • folder/
  • path/

I want my image to contain the full paths from the original filesystem:

  • bin/
  • usr/bin/
  • usr/local/bin/
  • some/other/random/folder/
  • another/long/path/

Is there any simple way to do this without first making copies or moving the original files before creating the image?


There probably is no simple way, but you can do it with a lot of effort by using the extended wildcard exclude syntax. This may not be documented in the man page but is mentioned more in the README. However, for details of the syntax you need to see the man page for fnmatch(3).

Basically, you can use !(somedir) as an exclude to not exclude, so you end up only including that directory. Imagine you have the following example tree in /tmp

$ mkdir -p a/b   d/e   d/e2
$ touch    a/b/c d/e/f d/e2/f2

and you only want to copy a and d/e whilst preserving these full pathnames. You can use

$ echo '!(a)' >exclude
$ mksquashfs /tmp mysq -ef exclude -wildcards
$ echo -e '!(d)/\nd/!(e)' >exclude
$ mksquashfs /tmp mysq -ef exclude -wildcards

Listing the filesystem with unsquashfs -l mysq produces the output


Each time, the source directory is /tmp, but the first time we exclude everything except directory a, and the second time we exclude everything except directory d and d/e. This uses a multi-level exclude file where on each line we exclude one more directory step in the path except the one that is to be retained.

A simpler solution is to create the wanted hierarchy of directories somewhere, and mount -bind the final directory to the real directory. For example,

$ mkdir -p a/usr/local/bin a/some/other/bin
$ sudo mount -o bind /usr/local/bin a/usr/local/bin
$ sudo mount -o bind /some/other/bin a/some/other/bin
$ mksquashfs a ~/mysq
$ sudo umount a/usr/local/bin a/some/other/bin

man mksquashfs:

       if  one source directory is specified, create a root directory con‐
       taining that directory, rather than the contents of the directory.
  • Already tried that. It doesn't do it. – LaVache Jul 30 '18 at 12:29

Based on @meuh's answer (which was a little bit hard for me to grasp):

mksquashfs / MyNewImage.squashfs -wildcards -e \
  !(bin|usr|some|another) \
  usr/!(bin|local) \
  usr/local/!(bin) \
  some/!(other) \
  some/other/!(random) \
  some/other/random/!(folder) \
  another/!(long) \

I'd also be glad of a more comprehensible plus more concise possibility and filed a feature request for it: https://github.com/plougher/squashfs-tools/issues/80 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.