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