SquashFS compressed filesystem reader and writer in pure OCaml
README.md

squashfs #

SquashFS images read and written in pure OCaml.

SquashFS is the compressed read-only filesystem Linux uses for Snap packages (Canonical's application packages, each a SquashFS image mounted read-only while the application runs), live CD and USB distributions and most embedded root filesystems. (An initramfs, the small filesystem the kernel unpacks into memory at boot to find the real root, is a cpio archive rather than SquashFS.) Reading or building one usually means shelling out to unsquashfs and mksquashfs from squashfs-tools. This library reads and writes SquashFS 4.0 images directly, so a build tool or a unikernel can produce or inspect an image without those binaries.

The reader opens an image from a string or a bytesrw reader, walks directories, reads files, symlinks, device numbers and extended attributes, and decompresses gzip, xz, lz4 and zstd blocks; it refuses lzma and lzo images. It treats the image as untrusted: every read is bounds-checked, block sizes above 1 MiB are refused, read_file is capped (100 MB by default) and safe_read_link refuses a symlink target that leaves the image. 1 MiB is the largest block size the SquashFS 4.0 format allows (the superblock stores its base-2 logarithm, at most 20), so a larger value can only come from a corrupt or hostile image. The 100 MB cap bounds the memory one file read from an untrusted image can take; a caller raises it with ~max_size.

Squashfs.Writer builds an image in memory from files, directories, symlinks and device nodes, compressed with gzip or stored uncompressed. The squashfs-eio library adds a directory tree from disk to a writer.

Installation #

Install with opam:

$ opam install squashfs squashfs-eio

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install squashfs squashfs-eio

Usage #

let read_image data =
  match Squashfs.of_string data with
  | Error msg -> Fmt.epr "Error: %s@." msg
  | Ok fs -> (
      Fmt.pr "%a@." Squashfs.pp_superblock (Squashfs.superblock fs);
      match Squashfs.readdir fs (Squashfs.root fs) with
      | Error msg -> Fmt.epr "Error: %s@." msg
      | Ok entries ->
          List.iter
            (fun e ->
              Fmt.pr "%s (%a)@." e.Squashfs.name Squashfs.pp_file_type
                e.Squashfs.file_type)
            entries)

API #

  • Squashfs.of_string and Squashfs.of_reader open an image.
  • Squashfs.root, readdir, lookup and resolve walk the directory tree; fold and iter visit every path.
  • Squashfs.read_file and read_link read a file or a symlink target, and safe_read_link refuses a target that escapes the image.
  • Squashfs.inode_type, inode_mode, inode_size, inode_mtime, inode_uid and inode_gid read inode properties; xattr and list_xattrs read extended attributes.
  • Squashfs.Writer.v, add_file, add_directory, add_symlink, add_device and finalize build an image.

References #

Licence #

ISC License. See LICENSE.md for details.