1 # Initially from https://github.com/plougher/squashfs-tools/pull/264
2 # Ported from squashfs-tools 4.6 → 4.7
3 diff --git a/squashfs-tools/mksquashfs.c b/squashfs-tools/mksquashfs.c
4 --- a/squashfs-tools/mksquashfs.c
5 +++ b/squashfs-tools/mksquashfs.c
6 @@ -207,6 +207,12 @@ int mem_options_disabled = FALSE;
7 /* Is filesystem stored at an offset from the start of the block device/file? */
8 long long start_offset = 0;
10 +/* Do we have an extra_info metadata block? */
11 +int extra_info = FALSE;
12 +/* parameters for the extra_info block */
13 +char *volume_label = NULL;
16 /* Is Mksquashfs streaming the output filesystem to stdout? */
17 int streaming = FALSE;
19 @@ -378,7 +384,7 @@ char *option_table[] = { "comp", "b", "mkfs-time", "fstime", "inode-time",
20 "mem-percent", "-pd", "-pseudo-dir", "help-option", "ho", "help-section",
21 "hs", "info-file", "force-file-mode", "force-dir-mode",
22 "small-readers", "block-readers", "uid-gid-offset", "all-time",
23 - "overcommit", "repro-time", "cols", NULL
24 + "overcommit", "repro-time", "cols", "label", "uuid", NULL
27 char *sqfstar_option_table[] = { "comp", "b", "mkfs-time", "fstime",
28 @@ -6546,6 +6552,68 @@ static void fix_file(char *filename)
29 BAD_ERROR("Failed to truncate file \"%s\" because %s\n", filename, strerror(errno));
32 +void *info_dump(int *psize) {
33 + int bytes = 0, pos = 0;
35 + const int UUID_LEN = 16; /* avoid depending on uuid-dev for now */
38 + /* The extra_info section contains one or more entries of the form:
41 + VAL byte_1 ... byte_len
43 + Terminated by the tag: SQUASHFS_EXTRA_INFO_TAG_END
46 + label_len = strlen(volume_label);
47 + if (label_len > 255) label_len = 255;
48 + bytes += 1 + 1 + label_len;
51 + bytes += 1 + 1 + UUID_LEN;
54 + bytes += 1; /* TAG_END */
56 + buf = malloc(bytes);
59 + buf[pos++] = SQUASHFS_EXTRA_INFO_TAG_LABEL;
60 + buf[pos++] = label_len;
61 + memcpy(&buf[pos], volume_label, label_len);
66 + buf[pos++] = SQUASHFS_EXTRA_INFO_TAG_UUID;
67 + buf[pos++] = UUID_LEN;
68 + /* poor man's uuid_parse() */
69 + for (int i = 0; i < UUID_LEN; i++) {
71 + char *endptr = NULL;
72 + if (*puuid == '-') {
75 + if (puuid[0] && puuid[1]) {
76 + memcpy(&hex[0], puuid, 2);
77 + buf[pos++] = strtoul(hex, &endptr, 16);
79 + if (endptr != hex + 2) {
80 + ERROR("invalid UUID: %s\n", uuid);
86 + buf[pos++] = SQUASHFS_EXTRA_INFO_TAG_END;
88 + ERROR("extra_info legth mismatch\n");
95 static int sqfstar(int argc, char *argv[])
97 @@ -6563,7 +6631,7 @@ static int sqfstar(int argc, char *argv[])
100 struct file_buffer **fragment = NULL;
104 int overcommit = OVERCOMMIT_DEFAULT;
105 int repro_opt = FALSE;
106 @@ -7404,7 +7472,10 @@ static int sqfstar(int argc, char *argv[])
107 memset(dupl_block, 0, 1048576 * sizeof(struct file_info *));
108 memset(dupl_frag, 0, block_size * sizeof(struct file_info *));
110 - comp_data = compressor_dump_options(comp, block_size, &size);
111 + comp_data = compressor_dump_options(comp, block_size, &comp_size);
114 + void *info_data = info_dump(&info_size);
117 printf("Creating %d.%d filesystem on %s, block size %d.\n",
118 @@ -7426,18 +7497,28 @@ static int sqfstar(int argc, char *argv[])
119 * and set the COMP_OPT flag to show that the filesystem has
120 * compressor specfic options
122 + long long super_end = sizeof(struct squashfs_super_block);
124 - unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
125 + unsigned short c_byte = comp_size | SQUASHFS_COMPRESSED_BIT;
127 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
128 - write_destination(fd, sizeof(struct squashfs_super_block),
129 - sizeof(c_byte), &c_byte);
130 - write_destination(fd, sizeof(struct squashfs_super_block) +
131 - sizeof(c_byte), size, comp_data);
132 - set_pos(sizeof(struct squashfs_super_block) + sizeof(c_byte) + size);
133 + write_destination(fd, super_end, sizeof(c_byte), &c_byte);
134 + super_end += sizeof(c_byte);
135 + write_destination(fd, super_end, comp_size, comp_data);
136 + super_end += comp_size;
139 - set_pos(sizeof(struct squashfs_super_block));
142 + unsigned short i_byte = info_size | SQUASHFS_COMPRESSED_BIT;
144 + SQUASHFS_INSWAP_SHORTS(&i_byte, 1);
145 + write_destination(fd, super_end, sizeof(i_byte), &i_byte);
146 + super_end += sizeof(i_byte);
147 + write_destination(fd, super_end, info_size, info_data);
148 + super_end += info_size;
151 + set_pos(super_end);
154 paths = add_subdir(paths, path);
155 @@ -7458,7 +7539,7 @@ static int sqfstar(int argc, char *argv[])
156 sBlk.block_log = block_log;
157 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, noId, no_fragments,
158 always_use_fragments, duplicate_checking, exportable,
159 - no_xattrs, comp_opts);
160 + no_xattrs, comp_opts, extra_info);
162 sBlk.mkfs_time = mkfs_time;
163 else if(mkfs_inode_opt)
164 @@ -7513,7 +7594,6 @@ static int sqfstar(int argc, char *argv[])
169 int main(int argc, char *argv[])
171 struct stat buf, source_buf;
172 @@ -8337,6 +8417,20 @@ int main(int argc, char *argv[])
173 !parse_number(argv[i], &overcommit, 2) ||
175 mksquashfs_option_help(argv[i - 1], "mksquashfs: -overcommit missing or invalid percentage: it should be 0 - 100%%\n");
176 + } else if (strcmp(argv[i], "-label") == 0) {
178 + ERROR("%s: -label: missing label\n",
182 + volume_label = argv[i];
183 + } else if (strcmp(argv[i], "-uuid") == 0) {
185 + ERROR("%s: -uuid: missing uuid\n",
191 mksquashfs_invalid_option(argv[i]);
193 @@ -8706,6 +8800,7 @@ int main(int argc, char *argv[])
194 exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
195 no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
196 comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
197 + extra_info = SQUASHFS_EXTRA_INFO(sBlk.flags);
200 initialise_threads(readq, fragq, bwriteq, fwriteq, !appending,
201 @@ -8721,9 +8816,12 @@ int main(int argc, char *argv[])
202 memset(dupl_frag, 0, block_size * sizeof(struct file_info *));
207 void *comp_data = compressor_dump_options(comp, block_size,
212 + void *info_data = info_dump(&info_size);
215 printf("Creating %d.%d filesystem on %s, block size %d.\n",
216 @@ -8746,18 +8844,28 @@ int main(int argc, char *argv[])
217 * and set the COMP_OPT flag to show that the filesystem has
218 * compressor specfic options
220 + long long super_end = sizeof(struct squashfs_super_block);
222 - unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
223 + unsigned short c_byte = comp_size | SQUASHFS_COMPRESSED_BIT;
225 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
226 - write_destination(fd, sizeof(struct squashfs_super_block),
227 - sizeof(c_byte), &c_byte);
228 - write_destination(fd, sizeof(struct squashfs_super_block) +
229 - sizeof(c_byte), size, comp_data);
230 - set_pos(sizeof(struct squashfs_super_block) + sizeof(c_byte) + size);
231 + write_destination(fd, super_end, sizeof(c_byte), &c_byte);
232 + super_end += sizeof(c_byte);
233 + write_destination(fd, super_end, comp_size, comp_data);
234 + super_end += comp_size;
237 - set_pos(sizeof(struct squashfs_super_block));
240 + unsigned short i_byte = info_size | SQUASHFS_COMPRESSED_BIT;
242 + SQUASHFS_INSWAP_SHORTS(&i_byte, 1);
243 + write_destination(fd, super_end, sizeof(i_byte), &i_byte);
244 + super_end += sizeof(i_byte);
245 + write_destination(fd, super_end, info_size, info_data);
246 + super_end += info_size;
249 + set_pos(super_end);
251 unsigned int last_directory_block, inode_dir_file_size,
252 root_inode_size, inode_dir_start_block,
253 @@ -8913,7 +9021,7 @@ int main(int argc, char *argv[])
254 sBlk.block_log = block_log;
255 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, noId, no_fragments,
256 always_use_fragments, duplicate_checking, exportable,
257 - no_xattrs, comp_opts);
258 + no_xattrs, comp_opts, extra_info);
260 sBlk.mkfs_time = mkfs_time;
261 else if(mkfs_inode_opt)
262 diff --git a/squashfs-tools/mksquashfs_help.c b/squashfs-tools/mksquashfs_help.c
263 --- a/squashfs-tools/mksquashfs_help.c
264 +++ b/squashfs-tools/mksquashfs_help.c
265 @@ -243,6 +243,8 @@ static char *mksquashfs_text[]={
266 "-keep-as-directory\tif one source directory is specified, create a "
267 "root directory containing that directory, rather than the "
268 "contents of the directory\n",
269 + "-label <volume-label>\tSet the volume label\n",
270 + "-uuid <UUID>\tSet the volume UUID\n"
271 "\n", "Filesystem time options:", "\n",
272 "-mkfs-time <time>\tset filesystem creation timestamp to <time>. "
273 "<time> can be \"inode\", which means use the latest inode "
274 diff --git a/squashfs-tools/squashfs_fs.h b/squashfs-tools/squashfs_fs.h
275 --- a/squashfs-tools/squashfs_fs.h
276 +++ b/squashfs-tools/squashfs_fs.h
278 #define SQUASHFS_NO_XATTR 9
279 #define SQUASHFS_COMP_OPT 10
280 #define SQUASHFS_NOID 11
281 +#define SQUASHFS_INFO 12
283 #define SQUASHFS_BIT(flag, bit) ((flag >> bit) & 1)
285 @@ -110,12 +111,16 @@
286 #define SQUASHFS_UNCOMPRESSED_IDS(flags) SQUASHFS_BIT(flags, \
289 +#define SQUASHFS_EXTRA_INFO(flags) SQUASHFS_BIT(flags, \
292 #define SQUASHFS_MKFLAGS(noi, nod, nof, nox, noid, no_frag, always_frag, \
293 - duplicate_checking, exportable, no_xattr, comp_opt) (noi | \
294 + duplicate_checking, exportable, no_xattr, comp_opt, \
295 + extra_info) (noi | \
296 (nod << 1) | (nof << 3) | (no_frag << 4) | \
297 (always_frag << 5) | (duplicate_checking << 6) | \
298 (exportable << 7) | (nox << 8) | (no_xattr << 9) | \
299 - (comp_opt << 10) | (noid << 11))
300 + (comp_opt << 10) | (noid << 11) | (extra_info << 12))
302 /* Max number of types and file types */
303 #define SQUASHFS_DIR_TYPE 1
304 @@ -291,6 +296,10 @@ typedef long long squashfs_inode;
305 #define LZ4_COMPRESSION 5
306 #define ZSTD_COMPRESSION 6
308 +#define SQUASHFS_EXTRA_INFO_TAG_END 0
309 +#define SQUASHFS_EXTRA_INFO_TAG_LABEL 1
310 +#define SQUASHFS_EXTRA_INFO_TAG_UUID 2
312 struct squashfs_super_block {
313 unsigned int s_magic;