| Phillip Lougher | 9e37ac0 | 2014-08-08 05:15:38 +0100 | [diff] [blame^] | 1 | SQUASHFS - A squashed read-only filesystem for Linux |
| 2 | |
| 3 | Copyright 2002 Phillip Lougher (phillip@lougher.demon.co.uk) |
| 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 | |
| 42 | 7. Both big and little endian architectures are supported. The mksquashfs |
| 43 | program can generate filesystems for different endian architectures for |
| 44 | cases where the host byte ordering is different to the target. This is |
| 45 | useful for embedded systems. |
| 46 | |
| 47 | |
| 48 | Installing squashfs |
| 49 | ------------------- |
| 50 | |
| 51 | The squashfs1.0.tar.gz file contains this README, the squashfs patch file |
| 52 | (squashfs-patch), and the squashfs-tools directory (mksquashfs). |
| 53 | |
| 54 | The squashfs-patch file has been generated against linux-2.4.19, but may work |
| 55 | with older 2.4.x kernels. It assumes the kernel has inflatefs support, and |
| 56 | therefore definately will not work with anything much older than 2.4.17. |
| 57 | |
| 58 | The squashfs patch patches the relevant kernel files to add configure support, |
| 59 | initrd support, include files, and the squashfs directory under linux/fs/. |
| 60 | |
| 61 | Once patched, the kernel must be reconfigured, with squashfs support turned on |
| 62 | (either Y/M) to ensure that inflatefs is built into the kernel. |
| 63 | |
| 64 | |
| 65 | mksquashfs |
| 66 | ---------- |
| 67 | |
| 68 | As squashfs is a read-only filesystem, the mksquashfs program must be used to |
| 69 | create populated squashfs filesystems. |
| 70 | |
| 71 | mksquashfs usage: mksquashfs source dest [options] |
| 72 | |
| 73 | Options are |
| 74 | -info print files written to filesystem |
| 75 | -b block size size of blocks in filesystem, default 32768 |
| 76 | -noI -noInodeCompression do not compress inode table |
| 77 | -noD -noDataCompression do not compress data blocks |
| 78 | -check_data add checkdata for greater filesystem integrity checks |
| 79 | -le create a little endian filesystem |
| 80 | -be create a big endian filesystem |
| 81 | |
| 82 | Source is the source directory containing the files/directories that will |
| 83 | form the squashfs filesystem. |
| 84 | |
| 85 | Dest is the destination where the squashfs filesystem will be written. This |
| 86 | can either be a conventional file or a block device. If the file doesn't exist |
| 87 | it will be created, if it does exist it will be truncated. |
| 88 | |
| 89 | The -info option displays the files/directories as they are compressed and |
| 90 | added to the filesystem. The compression percentage achieved is printed, with |
| 91 | the original uncompressed size. If the compression percentage is listed as |
| 92 | 0% it means the file is a duplicate. |
| 93 | |
| 94 | The -b option allows the block size to be selected, this can be either |
| 95 | 512, 1024, 2048, 4096, 8192, 16384, or 32768 bytes. |
| 96 | |
| 97 | The -noI and -noD options (also -noInodeCompression and -noDataCompression) |
| 98 | can be used to force mksquashfs to not compress inodes/directories and data |
| 99 | respectively. Giving both options generates an uncompressed filesystem. |
| 100 | |
| 101 | The -le and -be options can be used to force mksquashfs to generate a little |
| 102 | endian or big endian filesystem. Normally mksquashfs will generate a |
| 103 | filesystem in the host byte order. |
| 104 | |
| 105 | |
| 106 | Filesystem layout |
| 107 | ----------------- |
| 108 | |
| 109 | Brief filesystem design notes follow. |
| 110 | |
| 111 | A squashfs filesystem consists of five parts, packed together on a byte alignment: |
| 112 | |
| 113 | --------------- |
| 114 | | superblock | |
| 115 | |---------------| |
| 116 | | data | |
| 117 | | blocks | |
| 118 | |---------------| |
| 119 | | inodes | |
| 120 | |---------------| |
| 121 | | directories | |
| 122 | |---------------| |
| 123 | | uid/gid | |
| 124 | | lookup table | |
| 125 | --------------- |
| 126 | |
| 127 | Compressed data blocks are written to the filesystem as files are read from |
| 128 | the source directory, and checked for duplicates. Once all file data has been |
| 129 | written the completed inode, directory and uid/gid lookup tables are written. |
| 130 | |
| 131 | Metadata |
| 132 | -------- |
| 133 | |
| 134 | Metadata (inodes and directories) are compressed in 8Kbyte blocks. Each |
| 135 | compressed block is prefixed by a two byte length, the top bit is set if the |
| 136 | block is uncompressed. A block will be uncompressed if the -noI option is set, |
| 137 | or if the compressed block was larger than the uncompressed block. |
| 138 | |
| 139 | Inodes are packed into the metadata blocks, and are not aligned to block |
| 140 | boundaries, therefore inodes overlap compressed blocks. An inode is |
| 141 | identified by a two field tuple <start address of compressed block : offset |
| 142 | into de-compressed block>. |
| 143 | |
| 144 | Inode contents vary depending on the file type. The base inode consists of: |
| 145 | |
| 146 | base inode: |
| 147 | Inode type |
| 148 | Mode |
| 149 | uid index |
| 150 | gid index |
| 151 | |
| 152 | The inode type is 4 bits in size, and the mode is 12 bits. |
| 153 | |
| 154 | The uid and gid indexes are 4 bits in length. Ordinarily, this will allow 16 |
| 155 | unique indexes into the uid table. To minimise overhead, the uid index is |
| 156 | used in conjunction with the spare bit in the file type to form a 48 entry |
| 157 | index as follows: |
| 158 | |
| 159 | inode type 1 - 5: uid index = uid |
| 160 | inode type 5 -10: uid index = 16 + uid |
| 161 | inode type 11 - 15: uid index = 32 + uid |
| 162 | |
| 163 | In this way 48 unique uids are supported using 4 bits, minimising data inode |
| 164 | overhead. The 4 bit gid index is used to index into a 15 entry gid table. |
| 165 | Gid index 15 is used to indicate that the gid is the same as the uid. |
| 166 | This prevents the 15 entry gid table filling up with the common case where |
| 167 | the uid/gid is the same. |
| 168 | |
| 169 | The data contents of symbolic links are stored immediately after the symbolic |
| 170 | link inode, inside the inode table. This allows the normally small symbolic |
| 171 | link to be compressed as part of the inode table, achieving much greater |
| 172 | compression than if the symbolic link was compressed individually. |
| 173 | |
| 174 | Similarly, the block index for regular files is stored immediately after the |
| 175 | regular file inode. The block index is a list of block lengths (two bytes |
| 176 | each), rather than block addresses, saving two bytes per block. The block |
| 177 | address for a given block is computed by the summation of the previous |
| 178 | block lengths. This takes advantage of the fact that the blocks making up a |
| 179 | file are stored contiguously in the filesystem. The top bit of each block |
| 180 | length is set if the block is uncompressed, either because the -noD option is |
| 181 | set, or if the compressed block was larger than the uncompressed block. |
| 182 | |
| 183 | Directories |
| 184 | ----------- |
| 185 | |
| 186 | Like inodes, directories are packed into the metadata blocks, and are not |
| 187 | aligned on block boundaries, therefore directories can overlap compressed |
| 188 | blocks. A directory is, again, identified by a two field tuple |
| 189 | <start address of compressed block containing directory start : offset |
| 190 | into de-compressed block>. |
| 191 | |
| 192 | Directories are organised in a slightly complex way, and are not simply |
| 193 | a list of file names and inode tuples. The organisation takes advantage of the |
| 194 | observation that in most cases, the inodes of the files in the directory |
| 195 | will be in the same compressed metadata block, and therefore, the |
| 196 | inode tuples will have the same start block. |
| 197 | |
| 198 | Directories are therefore organised in a two level list, a directory |
| 199 | header containing the shared start block value, and a sequence of |
| 200 | directory entries, each of which share the shared start block. A |
| 201 | new directory header is written once/if the inode start block |
| 202 | changes. The directory header/directory entry list is repeated as many times |
| 203 | as necessary. The organisation is as follows: |
| 204 | |
| 205 | directory_header: |
| 206 | count (8 bits) |
| 207 | inode start block (24 bits) |
| 208 | |
| 209 | directory entry: * count |
| 210 | inode offset (13 bits) |
| 211 | inode type (3 bits) |
| 212 | filename size (8 bits) |
| 213 | filename |
| 214 | |
| 215 | This organisation saves on average 3 bytes per filename. |
| 216 | |
| 217 | File data |
| 218 | --------- |
| 219 | |
| 220 | File data is compressed on a block by block basis and written to the |
| 221 | filesystem. The filesystem supports up to 32K blocks, which achieves |
| 222 | greater compression ratios than the Linux 4K page size. |
| 223 | |
| 224 | The disadvantage with using greater than 4K blocks (and the reason why |
| 225 | most filesystems do not), is that the VFS reads data in 4K pages. |
| 226 | The filesystem reads and decompresses a larger block containing that page |
| 227 | (e.g. 32K). However, only 4K can be returned to the VFS, resulting in a |
| 228 | very inefficient filesystem, as 28K must be thrown away. Squashfs, |
| 229 | solves this problem by explicitly pushing the extra pages into the page |
| 230 | cache. |