]> git.scottworley.com Git - nixos-qemu-vm-isolation/blob - modules/squashfs-tools-label.patch
Contemplate using bcarnes' squashfs label patches
[nixos-qemu-vm-isolation] / modules / squashfs-tools-label.patch
1 # From https://github.com/plougher/squashfs-tools/pull/264
2 diff --git a/squashfs-tools/mksquashfs.c b/squashfs-tools/mksquashfs.c
3 --- a/squashfs-tools/mksquashfs.c
4 +++ b/squashfs-tools/mksquashfs.c
5 @@ -180,6 +180,12 @@ int exit_on_error = FALSE;
6 /* Is filesystem stored at an offset from the start of the block device/file? */
7 long long start_offset = 0;
8
9 +/* Do we have an extra_info metadata block? */
10 +int extra_info = FALSE;
11 +/* parameters for the extra_info block */
12 +char *volume_label = NULL;
13 +char *uuid = NULL;
14 +
15 /* File count statistics used to print summary and fill in superblock */
16 unsigned int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0,
17 fifo_count = 0, sock_count = 0, id_count = 0;
18 @@ -340,7 +346,7 @@ char *option_table[] = { "comp", "b", "mkfs-time", "fstime", "all-time",
19 "vaf", "taf", "faf", "read-queue", "write-queue", "fragment-queue",
20 "root-time", "root-uid", "root-gid", "xattrs-exclude", "xattrs-include",
21 "xattrs-add", "default-mode", "default-uid", "default-gid",
22 - "mem-percent", NULL
23 + "mem-percent", "label", "uuid", NULL
24 };
25
26 char *sqfstar_option_table[] = { "comp", "b", "mkfs-time", "fstime", "all-time",
27 @@ -6239,6 +6245,8 @@ static void print_options(FILE *stream, char *name, int total_mem)
28 fprintf(stream, "-keep-as-directory\tif one source directory is specified, ");
29 fprintf(stream, "create a root\n");
30 fprintf(stream, "\t\t\tdirectory containing that directory, rather than the\n");
31 + fprintf(stream, "-label <volume-label>\tSet the volume label\n");
32 + fprintf(stream, "-uuid <UUID>\tSet the volume UUID\n");
33 fprintf(stream, "\t\t\tcontents of the directory\n");
34 fprintf(stream, "\nFilesystem filter options:\n");
35 fprintf(stream, "-p <pseudo-definition>\tadd pseudo file ");
36 @@ -7541,6 +7549,7 @@ int sqfstar(int argc, char *argv[])
37 SQUASHFS_MAJOR, SQUASHFS_MINOR,
38 destination_file, block_size);
39
40 + bytes = sizeof(struct squashfs_super_block);
41 /*
42 * store any compressor specific options after the superblock,
43 * and set the COMP_OPT flag to show that the filesystem has
44 @@ -7554,11 +7563,9 @@ int sqfstar(int argc, char *argv[])
45 sizeof(c_byte), &c_byte);
46 write_destination(fd, sizeof(struct squashfs_super_block) +
47 sizeof(c_byte), size, comp_data);
48 - bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
49 - + size;
50 + bytes += sizeof(c_byte) + size;
51 comp_opts = TRUE;
52 - } else
53 - bytes = sizeof(struct squashfs_super_block);
54 + }
55
56 if(path)
57 paths = add_subdir(paths, path);
58 @@ -7579,7 +7586,7 @@ int sqfstar(int argc, char *argv[])
59 sBlk.block_log = block_log;
60 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, noId, no_fragments,
61 always_use_fragments, duplicate_checking, exportable,
62 - no_xattrs, comp_opts);
63 + no_xattrs, comp_opts, extra_info);
64 sBlk.mkfs_time = mkfs_time_opt ? mkfs_time : time(NULL);
65
66 disable_info();
67 @@ -7629,6 +7636,70 @@ int sqfstar(int argc, char *argv[])
68 }
69
70
71 +void *info_dump(int *psize) {
72 + int bytes = 0, pos = 0;
73 + unsigned char *buf;
74 + const int UUID_LEN = 16; /* avoid depending on uuid-dev for now */
75 + int label_len;
76 +
77 + /* The extra_info section contains one or more entries of the form:
78 + TAG byte
79 + LEN byte
80 + VAL byte_1 ... byte_len
81 +
82 + Terminated by the tag: SQUASHFS_EXTRA_INFO_TAG_END
83 + */
84 + if (volume_label) {
85 + label_len = strlen(volume_label);
86 + if (label_len > 255) label_len = 255;
87 + bytes += 1 + 1 + label_len;
88 + }
89 + if (uuid)
90 + bytes += 1 + 1 + UUID_LEN;
91 + if (!bytes)
92 + return NULL;
93 + bytes += 1; /* TAG_END */
94 +
95 + buf = malloc(bytes);
96 +
97 + if (volume_label) {
98 + buf[pos++] = SQUASHFS_EXTRA_INFO_TAG_LABEL;
99 + buf[pos++] = label_len;
100 + memcpy(&buf[pos], volume_label, label_len);
101 + pos += label_len;
102 + }
103 + if (uuid) {
104 + char *puuid = uuid;
105 + buf[pos++] = SQUASHFS_EXTRA_INFO_TAG_UUID;
106 + buf[pos++] = UUID_LEN;
107 + /* poor man's uuid_parse() */
108 + for (int i = 0; i < UUID_LEN; i++) {
109 + char hex[3] = {};
110 + char *endptr = NULL;
111 + if (*puuid == '-') {
112 + puuid++;
113 + }
114 + if (puuid[0] && puuid[1]) {
115 + memcpy(&hex[0], puuid, 2);
116 + buf[pos++] = strtoul(hex, &endptr, 16);
117 + }
118 + if (endptr != hex + 2) {
119 + ERROR("invalid UUID: %s\n", uuid);
120 + exit(1);
121 + }
122 + puuid += 2;
123 + }
124 + }
125 + buf[pos++] = SQUASHFS_EXTRA_INFO_TAG_END;
126 + if (pos != bytes) {
127 + ERROR("extra_info legth mismatch\n");
128 + exit(1);
129 + }
130 + *psize = bytes;
131 + return buf;
132 +}
133 +
134 +
135 int main(int argc, char *argv[])
136 {
137 struct stat buf, source_buf;
138 @@ -8420,6 +8491,20 @@ int main(int argc, char *argv[])
139 } else if(strcmp(argv[i], "-comp") == 0) {
140 /* parsed previously */
141 i++;
142 + } else if (strcmp(argv[i], "-label") == 0) {
143 + if(++i == argc) {
144 + ERROR("%s: -label: missing label\n",
145 + argv[0]);
146 + exit(1);
147 + }
148 + volume_label = argv[i];
149 + } else if (strcmp(argv[i], "-uuid") == 0) {
150 + if(++i == argc) {
151 + ERROR("%s: -uuid: missing uuid\n",
152 + argv[0]);
153 + exit(1);
154 + }
155 + uuid = argv[i];
156 } else {
157 ERROR("%s: invalid option\n\n", argv[0]);
158 print_options(stderr, argv[0], total_mem);
159 @@ -8645,6 +8730,7 @@ int main(int argc, char *argv[])
160 exportable = SQUASHFS_EXPORTABLE(sBlk.flags);
161 no_xattrs = SQUASHFS_NO_XATTRS(sBlk.flags);
162 comp_opts = SQUASHFS_COMP_OPTS(sBlk.flags);
163 + extra_info = SQUASHFS_EXTRA_INFO(sBlk.flags);
164 }
165
166 initialise_threads(readq, fragq, bwriteq, fwriteq, !appending,
167 @@ -8666,33 +8752,44 @@ int main(int argc, char *argv[])
168 memset(dupl_frag, 0, block_size * sizeof(struct file_info *));
169
170 if(!appending) {
171 - int size;
172 + int comp_size;
173 void *comp_data = compressor_dump_options(comp, block_size,
174 - &size);
175 + &comp_size);
176 +
177 + int info_size;
178 + void *info_data = info_dump(&info_size);
179
180 if(!quiet)
181 printf("Creating %d.%d filesystem on %s, block size %d.\n",
182 SQUASHFS_MAJOR, SQUASHFS_MINOR,
183 destination_file, block_size);
184
185 + bytes = sizeof(struct squashfs_super_block);
186 /*
187 * store any compressor specific options after the superblock,
188 * and set the COMP_OPT flag to show that the filesystem has
189 * compressor specfic options
190 */
191 if(comp_data) {
192 - unsigned short c_byte = size | SQUASHFS_COMPRESSED_BIT;
193 + unsigned short c_byte = comp_size | SQUASHFS_COMPRESSED_BIT;
194
195 SQUASHFS_INSWAP_SHORTS(&c_byte, 1);
196 - write_destination(fd, sizeof(struct squashfs_super_block),
197 - sizeof(c_byte), &c_byte);
198 - write_destination(fd, sizeof(struct squashfs_super_block) +
199 - sizeof(c_byte), size, comp_data);
200 - bytes = sizeof(struct squashfs_super_block) + sizeof(c_byte)
201 - + size;
202 + write_destination(fd, bytes, sizeof(c_byte), &c_byte);
203 + bytes += sizeof(c_byte);
204 + write_destination(fd, bytes, comp_size, comp_data);
205 + bytes += comp_size;
206 comp_opts = TRUE;
207 - } else
208 - bytes = sizeof(struct squashfs_super_block);
209 + }
210 + if (info_data) {
211 + unsigned short i_byte = info_size | SQUASHFS_COMPRESSED_BIT;
212 +
213 + SQUASHFS_INSWAP_SHORTS(&i_byte, 1);
214 + write_destination(fd, bytes, sizeof(i_byte), &i_byte);
215 + bytes += sizeof(i_byte);
216 + write_destination(fd, bytes, info_size, info_data);
217 + bytes += info_size;
218 + extra_info = TRUE;
219 + }
220 } else {
221 unsigned int last_directory_block, inode_dir_file_size,
222 root_inode_size, inode_dir_start_block,
223 @@ -8852,7 +8949,7 @@ int main(int argc, char *argv[])
224 sBlk.block_log = block_log;
225 sBlk.flags = SQUASHFS_MKFLAGS(noI, noD, noF, noX, noId, no_fragments,
226 always_use_fragments, duplicate_checking, exportable,
227 - no_xattrs, comp_opts);
228 + no_xattrs, comp_opts, extra_info);
229 sBlk.mkfs_time = mkfs_time_opt ? mkfs_time : time(NULL);
230
231 disable_info();
232 diff --git a/squashfs-tools/squashfs_fs.h b/squashfs-tools/squashfs_fs.h
233 --- a/squashfs-tools/squashfs_fs.h
234 +++ b/squashfs-tools/squashfs_fs.h
235 @@ -72,6 +72,7 @@
236 #define SQUASHFS_NO_XATTR 9
237 #define SQUASHFS_COMP_OPT 10
238 #define SQUASHFS_NOID 11
239 +#define SQUASHFS_INFO 12
240
241 #define SQUASHFS_BIT(flag, bit) ((flag >> bit) & 1)
242
243 @@ -108,12 +109,16 @@
244 #define SQUASHFS_UNCOMPRESSED_IDS(flags) SQUASHFS_BIT(flags, \
245 SQUASHFS_NOID)
246
247 +#define SQUASHFS_EXTRA_INFO(flags) SQUASHFS_BIT(flags, \
248 + SQUASHFS_INFO)
249 +
250 #define SQUASHFS_MKFLAGS(noi, nod, nof, nox, noid, no_frag, always_frag, \
251 - duplicate_checking, exportable, no_xattr, comp_opt) (noi | \
252 + duplicate_checking, exportable, no_xattr, comp_opt, \
253 + extra_info) (noi | \
254 (nod << 1) | (nof << 3) | (no_frag << 4) | \
255 (always_frag << 5) | (duplicate_checking << 6) | \
256 (exportable << 7) | (nox << 8) | (no_xattr << 9) | \
257 - (comp_opt << 10) | (noid << 11))
258 + (comp_opt << 10) | (noid << 11) | (extra_info << 12))
259
260 /* Max number of types and file types */
261 #define SQUASHFS_DIR_TYPE 1
262 @@ -289,6 +294,10 @@ typedef long long squashfs_inode;
263 #define LZ4_COMPRESSION 5
264 #define ZSTD_COMPRESSION 6
265
266 +#define SQUASHFS_EXTRA_INFO_TAG_END 0
267 +#define SQUASHFS_EXTRA_INFO_TAG_LABEL 1
268 +#define SQUASHFS_EXTRA_INFO_TAG_UUID 2
269 +
270 struct squashfs_super_block {
271 unsigned int s_magic;
272 unsigned int inodes;