| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 1 | SQUASHFS 1.1 - A squashed read-only filesystem for Linux |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 2 | |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 3 | Copyright 2003 Phillip Lougher (phillip@lougher.demon.co.uk) |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 4 | |
| 5 | Released under the GPL licence (version 2 or later). |
| 6 | |
| 7 | Squashfs is a highly compressed read-only filesystem for Linux (kernel 2.4.x). |
| 8 | It uses zlib compression to compress both files, inodes and directories. |
| 9 | Inodes in the system are very small and all blocks are packed to minimise |
| 10 | data overhead. Block sizes greater than 4K are supported up to a maximum |
| 11 | of 32K. |
| 12 | |
| 13 | Squashfs is intended for general read-only filesystem use, for archival |
| 14 | use (i.e. in cases where a .tar.gz file may be used), and in constrained |
| 15 | block device/memory systems (e.g. embedded systems) where low overhead is |
| 16 | needed. |
| 17 | |
| 18 | The filesystem is currently stable, and has been tested on PowerPC, i586 |
| 19 | and Sparc architectures. |
| 20 | |
| 21 | Squashfs overview |
| 22 | ----------------- |
| 23 | |
| 24 | 1. Data, inodes and directories are compressed. |
| 25 | |
| 26 | 2. Squashfs stores full uid/gids (32 bits), and file creation time. |
| 27 | |
| 28 | 3. Files up to 2^32 bytes are supported. Filesystems can be up to |
| 29 | 2^32 bytes. |
| 30 | |
| 31 | 4. Inode and directory data are highly compacted, and packed on byte |
| 32 | boundaries. Each compressed inode is on average 8 bytes in length |
| 33 | (the exact length varies on file type, i.e. regular file, directory, |
| 34 | symbolic link, and block/char device inodes have different sizes). |
| 35 | |
| 36 | 5. Squashfs can use block sizes up to 32K (the default size is 32K). |
| 37 | Using 32K blocks achieves greater compression ratios than the normal |
| 38 | 4K block size. |
| 39 | |
| 40 | 6. File duplicates are detected and removed. |
| 41 | |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 42 | 7. Both big and little endian architectures are supported. Squashfs can |
| 43 | mount filesystems created on different byte order machines. |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 44 | |
| 45 | |
| 46 | mksquashfs |
| 47 | ---------- |
| 48 | |
| 49 | As squashfs is a read-only filesystem, the mksquashfs program must be used to |
| 50 | create populated squashfs filesystems. |
| 51 | |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 52 | SYNTAX:./mksquashfs source1 source2 ... dest [options] [-e list of exclude dirs/files] |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 53 | |
| 54 | Options are |
| 55 | -info print files written to filesystem |
| 56 | -b block size size of blocks in filesystem, default 32768 |
| 57 | -noI -noInodeCompression do not compress inode table |
| 58 | -noD -noDataCompression do not compress data blocks |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 59 | -nopad do not pad filesystem to a multiple of 4K |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 60 | -check_data add checkdata for greater filesystem integrity checks |
| 61 | -le create a little endian filesystem |
| 62 | -be create a big endian filesystem |
| 63 | |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 64 | Source1 source2 ... are the source directories/files containing the |
| 65 | files/directories that will form the squashfs filesystem. If a single |
| 66 | directory is specified (i.e. mksquashfs source output_fs) the squashfs |
| 67 | filesystem will consist of that directory, with the top-level root |
| 68 | directory corresponding to the source directory. |
| 69 | |
| 70 | If multiple source directories or files are specified, mksquashfs will merge |
| 71 | the specified sources into a single filesystem, with the root directory |
| 72 | containing each of the source files/directories. The name of each directory |
| 73 | entry will be the basename of the source path. If more than one source |
| 74 | entry maps to the same name, the conflicts are named xxx_1, xxx_2, etc. where |
| 75 | xxx is the original name, i.e. |
| 76 | |
| 77 | %mksquashfs /home/phillip/test /tmp/source2 source3 /tmp/test output_fs |
| 78 | |
| 79 | Will create a filesystem with the root directory containing directory |
| 80 | entries test source2 source3 test_1 |
| 81 | |
| 82 | Multiple sources allow filesystems to be generated without needing to |
| 83 | copy all source files into a common directory. This simplifies creating |
| 84 | filesystems. |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 85 | |
| 86 | Dest is the destination where the squashfs filesystem will be written. This |
| 87 | can either be a conventional file or a block device. If the file doesn't exist |
| 88 | it will be created, if it does exist it will be truncated. |
| 89 | |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 90 | The -e option allows files/directories to be specified which are |
| 91 | excluded from the output filesystem. If an exclude file/directory is |
| 92 | absolute (i.e. prefixed with /, ../, or ./) the entry is treated as |
| 93 | absolute, however, if an exclude file/directory is relative, it is |
| 94 | treated as being relative to each of the sources in turn, i.e. |
| 95 | |
| 96 | %mksquashfs /tmp/source1 source2 output_fs -e ex1 /tmp/source1/ex2 out/ex3 |
| 97 | |
| 98 | Will generate exclude files /tmp/source1/ex2, /tmp/source1/ex1, source2/ex1, |
| 99 | /tmp/source1/out/ex3 and source2/out/ex3. |
| 100 | |
| 101 | The -e exclude option is usefully used in archiving the entire filesystem, |
| 102 | where it is wished to avoid archiving /proc, and the filesystem being |
| 103 | generated, i.e. |
| 104 | |
| 105 | %mksquashfs / /tmp/root.sqsh -e proc /tmp/root.sqsh |
| 106 | |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 107 | The -info option displays the files/directories as they are compressed and |
| 108 | added to the filesystem. The compression percentage achieved is printed, with |
| 109 | the original uncompressed size. If the compression percentage is listed as |
| 110 | 0% it means the file is a duplicate. |
| 111 | |
| 112 | The -b option allows the block size to be selected, this can be either |
| 113 | 512, 1024, 2048, 4096, 8192, 16384, or 32768 bytes. |
| 114 | |
| 115 | The -noI and -noD options (also -noInodeCompression and -noDataCompression) |
| 116 | can be used to force mksquashfs to not compress inodes/directories and data |
| 117 | respectively. Giving both options generates an uncompressed filesystem. |
| 118 | |
| 119 | The -le and -be options can be used to force mksquashfs to generate a little |
| 120 | endian or big endian filesystem. Normally mksquashfs will generate a |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 121 | filesystem in the host byte order. Squashfs, for portability, will |
| 122 | mount different ordered filesystems (i.e. it can mount big endian filesystems |
| 123 | running on a little endian machine), but these options can be used for |
| 124 | greater optimisation. |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 125 | |
| Phillip Lougher | e162193 | 2014-08-08 05:30:01 +0100 | [diff] [blame^] | 126 | The -nopad option informs mksquashfs to not pad the filesystem to a 4K multiple. |
| 127 | This is performed by default to enable the output filesystem file to be mounted |
| 128 | by loopback, which requires files to be a 4K multiple. If the filesystem is |
| 129 | being written to a block device, or is to be stored in a bootimage, the extra |
| 130 | pad bytes are not needed. |
| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame] | 131 | |
| 132 | Filesystem layout |
| 133 | ----------------- |
| 134 | |
| 135 | Brief filesystem design notes follow. |
| 136 | |
| 137 | A squashfs filesystem consists of five parts, packed together on a byte alignment: |
| 138 | |
| 139 | --------------- |
| 140 | | superblock | |
| 141 | |---------------| |
| 142 | | data | |
| 143 | | blocks | |
| 144 | |---------------| |
| 145 | | inodes | |
| 146 | |---------------| |
| 147 | | directories | |
| 148 | |---------------| |
| 149 | | uid/gid | |
| 150 | | lookup table | |
| 151 | --------------- |
| 152 | |
| 153 | Compressed data blocks are written to the filesystem as files are read from |
| 154 | the source directory, and checked for duplicates. Once all file data has been |
| 155 | written the completed inode, directory and uid/gid lookup tables are written. |
| 156 | |
| 157 | Metadata |
| 158 | -------- |
| 159 | |
| 160 | Metadata (inodes and directories) are compressed in 8Kbyte blocks. Each |
| 161 | compressed block is prefixed by a two byte length, the top bit is set if the |
| 162 | block is uncompressed. A block will be uncompressed if the -noI option is set, |
| 163 | or if the compressed block was larger than the uncompressed block. |
| 164 | |
| 165 | Inodes are packed into the metadata blocks, and are not aligned to block |
| 166 | boundaries, therefore inodes overlap compressed blocks. An inode is |
| 167 | identified by a two field tuple <start address of compressed block : offset |
| 168 | into de-compressed block>. |
| 169 | |
| 170 | Inode contents vary depending on the file type. The base inode consists of: |
| 171 | |
| 172 | base inode: |
| 173 | Inode type |
| 174 | Mode |
| 175 | uid index |
| 176 | gid index |
| 177 | |
| 178 | The inode type is 4 bits in size, and the mode is 12 bits. |
| 179 | |
| 180 | The uid and gid indexes are 4 bits in length. Ordinarily, this will allow 16 |
| 181 | unique indexes into the uid table. To minimise overhead, the uid index is |
| 182 | used in conjunction with the spare bit in the file type to form a 48 entry |
| 183 | index as follows: |
| 184 | |
| 185 | inode type 1 - 5: uid index = uid |
| 186 | inode type 5 -10: uid index = 16 + uid |
| 187 | inode type 11 - 15: uid index = 32 + uid |
| 188 | |
| 189 | In this way 48 unique uids are supported using 4 bits, minimising data inode |
| 190 | overhead. The 4 bit gid index is used to index into a 15 entry gid table. |
| 191 | Gid index 15 is used to indicate that the gid is the same as the uid. |
| 192 | This prevents the 15 entry gid table filling up with the common case where |
| 193 | the uid/gid is the same. |
| 194 | |
| 195 | The data contents of symbolic links are stored immediately after the symbolic |
| 196 | link inode, inside the inode table. This allows the normally small symbolic |
| 197 | link to be compressed as part of the inode table, achieving much greater |
| 198 | compression than if the symbolic link was compressed individually. |
| 199 | |
| 200 | Similarly, the block index for regular files is stored immediately after the |
| 201 | regular file inode. The block index is a list of block lengths (two bytes |
| 202 | each), rather than block addresses, saving two bytes per block. The block |
| 203 | address for a given block is computed by the summation of the previous |
| 204 | block lengths. This takes advantage of the fact that the blocks making up a |
| 205 | file are stored contiguously in the filesystem. The top bit of each block |
| 206 | length is set if the block is uncompressed, either because the -noD option is |
| 207 | set, or if the compressed block was larger than the uncompressed block. |
| 208 | |
| 209 | Directories |
| 210 | ----------- |
| 211 | |
| 212 | Like inodes, directories are packed into the metadata blocks, and are not |
| 213 | aligned on block boundaries, therefore directories can overlap compressed |
| 214 | blocks. A directory is, again, identified by a two field tuple |
| 215 | <start address of compressed block containing directory start : offset |
| 216 | into de-compressed block>. |
| 217 | |
| 218 | Directories are organised in a slightly complex way, and are not simply |
| 219 | a list of file names and inode tuples. The organisation takes advantage of the |
| 220 | observation that in most cases, the inodes of the files in the directory |
| 221 | will be in the same compressed metadata block, and therefore, the |
| 222 | inode tuples will have the same start block. |
| 223 | |
| 224 | Directories are therefore organised in a two level list, a directory |
| 225 | header containing the shared start block value, and a sequence of |
| 226 | directory entries, each of which share the shared start block. A |
| 227 | new directory header is written once/if the inode start block |
| 228 | changes. The directory header/directory entry list is repeated as many times |
| 229 | as necessary. The organisation is as follows: |
| 230 | |
| 231 | directory_header: |
| 232 | count (8 bits) |
| 233 | inode start block (24 bits) |
| 234 | |
| 235 | directory entry: * count |
| 236 | inode offset (13 bits) |
| 237 | inode type (3 bits) |
| 238 | filename size (8 bits) |
| 239 | filename |
| 240 | |
| 241 | This organisation saves on average 3 bytes per filename. |
| 242 | |
| 243 | File data |
| 244 | --------- |
| 245 | |
| 246 | File data is compressed on a block by block basis and written to the |
| 247 | filesystem. The filesystem supports up to 32K blocks, which achieves |
| 248 | greater compression ratios than the Linux 4K page size. |
| 249 | |
| 250 | The disadvantage with using greater than 4K blocks (and the reason why |
| 251 | most filesystems do not), is that the VFS reads data in 4K pages. |
| 252 | The filesystem reads and decompresses a larger block containing that page |
| 253 | (e.g. 32K). However, only 4K can be returned to the VFS, resulting in a |
| 254 | very inefficient filesystem, as 28K must be thrown away. Squashfs, |
| 255 | solves this problem by explicitly pushing the extra pages into the page |
| 256 | cache. |