Debian Codegraph @

src/sysupdate/sysupdate-partition.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/file.h>
4
5 #include "alloc-util.h"
6 #include "fdisk-util.h"
7 #include "gpt.h"
8 #include "log.h"
9 #include "string-util.h"
10 #include "sysupdate-partition.h"
11
12 /* App-specific IDs used as HMAC keys to derive "partial" and "pending" partition type UUIDs from the
13 * original partition type UUID. This way we can indicate sysupdate transfer state via a separate GPT
14 * partition type UUID instead of using a label prefix, saving precious label space. */
15 #define GPT_SYSUPDATE_PARTIAL_APP_ID SD_ID128_MAKE(ac,cf,a0,c2,da,24,46,0a,9f,c9,0b,b8,fc,78,52,19)
16 #define GPT_SYSUPDATE_PENDING_APP_ID SD_ID128_MAKE(80,f3,d6,1e,23,83,43,b9,81,f5,ce,37,93,f4,7d,4c)
17
18 int gpt_partition_type_uuid_for_sysupdate_partial(sd_id128_t type, sd_id128_t *ret) {
19 return sd_id128_get_app_specific(type, GPT_SYSUPDATE_PARTIAL_APP_ID, ret);
20 }
21
22 int gpt_partition_type_uuid_for_sysupdate_pending(sd_id128_t type, sd_id128_t *ret) {
23 return sd_id128_get_app_specific(type, GPT_SYSUPDATE_PENDING_APP_ID, ret);
24 }
25
26 void partition_info_destroy(PartitionInfo *p) {
27 assert(p);
28
29 p->label = mfree(p->label);
30 p->device = mfree(p->device);
31 }
32
33 int partition_info_copy(PartitionInfo *dest, const PartitionInfo *src) {
34 int r;
35
36 assert(dest);
37 assert(src);
38
39 r = free_and_strdup_warn(&dest->label, src->label);
40 if (r < 0)
41 return r;
42
43 r = free_and_strdup_warn(&dest->device, src->device);
44 if (r < 0)
45 return r;
46
47 dest->partno = src->partno;
48 dest->start = src->start;
49 dest->size = src->size;
50 dest->flags = src->flags;
51 dest->type = src->type;
52 dest->uuid = src->uuid;
53 dest->no_auto = src->no_auto;
54 dest->read_only = src->read_only;
55 dest->growfs = src->growfs;
56
57 return 0;
58 }
59
60 int read_partition_info(
61 struct fdisk_context *c,
62 struct fdisk_table *t,
63 size_t i,
64 PartitionInfo *ret) {
65
66 _cleanup_free_ char *label_copy = NULL, *device = NULL;
67 const char *label;
68 struct fdisk_partition *p;
69 uint64_t start, size, flags;
70 unsigned long ssz;
71 sd_id128_t ptid, id;
72 GptPartitionType type;
73 size_t partno;
74 int r;
75
76 assert(c);
77 assert(t);
78 assert(ret);
79
80 p = sym_fdisk_table_get_partition(t, i);
81 if (!p)
82 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata.");
83
84 if (sym_fdisk_partition_is_used(p) <= 0) {
85 *ret = (PartitionInfo) PARTITION_INFO_NULL;
86 return 0; /* not found! */
87 }
88
89 if (sym_fdisk_partition_has_partno(p) <= 0 ||
90 sym_fdisk_partition_has_start(p) <= 0 ||
91 sym_fdisk_partition_has_size(p) <= 0)
92 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a number, position or size.");
93
94 partno = sym_fdisk_partition_get_partno(p);
95
96 start = sym_fdisk_partition_get_start(p);
97 ssz = sym_fdisk_get_sector_size(c);
98 assert(start <= UINT64_MAX / ssz);
99 start *= ssz;
100
101 size = sym_fdisk_partition_get_size(p);
102 assert(size <= UINT64_MAX / ssz);
103 size *= ssz;
104
105 label = sym_fdisk_partition_get_name(p);
106 if (!label)
107 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a label.");
108
109 r = fdisk_partition_get_type_as_id128(p, &ptid);
110 if (r < 0)
111 return log_error_errno(r, "Failed to read partition type UUID: %m");
112
113 r = fdisk_partition_get_uuid_as_id128(p, &id);
114 if (r < 0)
115 return log_error_errno(r, "Failed to read partition UUID: %m");
116
117 r = fdisk_partition_get_attrs_as_uint64(p, &flags);
118 if (r < 0)
119 return log_error_errno(r, "Failed to get partition flags: %m");
120
121 r = sym_fdisk_partition_to_string(p, c, FDISK_FIELD_DEVICE, &device);
122 if (r != 0)
123 return log_error_errno(r, "Failed to get partition device name: %m");
124
125 label_copy = strdup(label);
126 if (!label_copy)
127 return log_oom();
128
129 type = gpt_partition_type_from_uuid(ptid);
130
131 *ret = (PartitionInfo) {
132 .partno = partno,
133 .start = start,
134 .size = size,
135 .flags = flags,
136 .type = ptid,
137 .uuid = id,
138 .label = TAKE_PTR(label_copy),
139 .device = TAKE_PTR(device),
140 .no_auto = FLAGS_SET(flags, SD_GPT_FLAG_NO_AUTO) && gpt_partition_type_knows_no_auto(type),
141 .read_only = FLAGS_SET(flags, SD_GPT_FLAG_READ_ONLY) && gpt_partition_type_knows_read_only(type),
142 .growfs = FLAGS_SET(flags, SD_GPT_FLAG_GROWFS) && gpt_partition_type_knows_growfs(type),
143 };
144
145 return 1; /* found! */
146 }
147
148 int find_suitable_partition(
149 const char *device,
150 uint64_t space,
151 sd_id128_t *partition_type,
152 PartitionInfo *ret) {
153
154 _cleanup_(partition_info_destroy) PartitionInfo smallest = PARTITION_INFO_NULL;
155 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
156 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
157 size_t n_partitions;
158 int r;
159
160 assert(device);
161 POINTER_MAY_BE_NULL(partition_type);
162 assert(ret);
163
164 r = DLOPEN_FDISK(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
165 if (r < 0)
166 return r;
167
168 r = fdisk_new_context_at(AT_FDCWD, device, /* read_only= */ true, /* sector_size= */ UINT32_MAX, &c);
169 if (r < 0)
170 return log_error_errno(r, "Failed to create fdisk context from '%s': %m", device);
171
172 if (!sym_fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
173 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", device);
174
175 r = sym_fdisk_get_partitions(c, &t);
176 if (r < 0)
177 return log_error_errno(r, "Failed to acquire partition table: %m");
178
179 n_partitions = sym_fdisk_table_get_nents(t);
180 for (size_t i = 0; i < n_partitions; i++) {
181 _cleanup_(partition_info_destroy) PartitionInfo pinfo = PARTITION_INFO_NULL;
182
183 r = read_partition_info(c, t, i, &pinfo);
184 if (r < 0)
185 return r;
186 if (r == 0) /* not assigned */
187 continue;
188
189 /* Filter out non-matching partition types */
190 if (partition_type && !sd_id128_equal(pinfo.type, *partition_type))
191 continue;
192
193 if (!streq_ptr(pinfo.label, "_empty")) /* used */
194 continue;
195
196 if (space != UINT64_MAX && pinfo.size < space) /* too small */
197 continue;
198
199 if (smallest.partno != SIZE_MAX && smallest.size <= pinfo.size) /* already found smaller */
200 continue;
201
202 smallest = pinfo;
203 pinfo = (PartitionInfo) PARTITION_INFO_NULL;
204 }
205
206 if (smallest.partno == SIZE_MAX)
207 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC), "No available partition of a suitable size found.");
208
209 *ret = smallest;
210 smallest = (PartitionInfo) PARTITION_INFO_NULL;
211
212 return 0;
213 }
214
215 int patch_partition(
216 const char *device,
217 const PartitionInfo *info,
218 PartitionChange change) {
219
220 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *pa = NULL;
221 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
222 bool tweak_no_auto, tweak_read_only, tweak_growfs;
223 GptPartitionType type;
224 int r, fd;
225
226 assert(device);
227 assert(info);
228 assert(change <= _PARTITION_CHANGE_MAX);
229
230 if (change == 0) /* Nothing to do */
231 return 0;
232
233 r = DLOPEN_FDISK(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
234 if (r < 0)
235 return r;
236
237 r = fdisk_new_context_at(AT_FDCWD, device, /* read_only= */ false, /* sector_size= */ UINT32_MAX, &c);
238 if (r < 0)
239 return log_error_errno(r, "Failed to create fdisk context from '%s': %m", device);
240
241 assert_se((fd = sym_fdisk_get_devfd(c)) >= 0);
242
243 /* Make sure udev doesn't read the device while we make changes (this lock is released automatically
244 * by the kernel when the fd is closed, i.e. when the fdisk context is freed, hence no explicit
245 * unlock by us here anywhere.) */
246 if (flock(fd, LOCK_EX) < 0)
247 return log_error_errno(errno, "Failed to lock block device '%s': %m", device);
248
249 if (!sym_fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
250 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not suitable.", device);
251
252 r = sym_fdisk_get_partition(c, info->partno, &pa);
253 if (r < 0)
254 return log_error_errno(r, "Failed to read partition %zu of GPT label of '%s': %m", info->partno, device);
255
256 if (change & PARTITION_LABEL) {
257 r = sym_fdisk_partition_set_name(pa, info->label);
258 if (r < 0)
259 return log_error_errno(r, "Failed to update partition label: %m");
260 }
261
262 if (change & PARTITION_UUID) {
263 r = sym_fdisk_partition_set_uuid(pa, SD_ID128_TO_UUID_STRING(info->uuid));
264 if (r < 0)
265 return log_error_errno(r, "Failed to update partition UUID: %m");
266 }
267
268 if (change & PARTITION_TYPE) {
269 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *pt = NULL;
270
271 pt = sym_fdisk_new_parttype();
272 if (!pt)
273 return log_oom();
274
275 r = sym_fdisk_parttype_set_typestr(pt, SD_ID128_TO_UUID_STRING(info->type));
276 if (r < 0)
277 return log_error_errno(r, "Failed to initialize partition type: %m");
278
279 r = sym_fdisk_partition_set_type(pa, pt);
280 if (r < 0)
281 return log_error_errno(r, "Failed to update partition type: %m");
282 }
283
284 type = gpt_partition_type_from_uuid(info->type);
285
286 /* Tweak the read-only flag, but only if supported by the partition type */
287 tweak_no_auto =
288 FLAGS_SET(change, PARTITION_NO_AUTO) &&
289 gpt_partition_type_knows_no_auto(type);
290 tweak_read_only =
291 FLAGS_SET(change, PARTITION_READ_ONLY) &&
292 gpt_partition_type_knows_read_only(type);
293 tweak_growfs =
294 FLAGS_SET(change, PARTITION_GROWFS) &&
295 gpt_partition_type_knows_growfs(type);
296
297 if (change & PARTITION_FLAGS) {
298 uint64_t flags;
299
300 /* Update the full flags parameter, and import the read-only flag into it */
301
302 flags = info->flags;
303 if (tweak_no_auto)
304 SET_FLAG(flags, SD_GPT_FLAG_NO_AUTO, info->no_auto);
305 if (tweak_read_only)
306 SET_FLAG(flags, SD_GPT_FLAG_READ_ONLY, info->read_only);
307 if (tweak_growfs)
308 SET_FLAG(flags, SD_GPT_FLAG_GROWFS, info->growfs);
309
310 r = fdisk_partition_set_attrs_as_uint64(pa, flags);
311 if (r < 0)
312 return log_error_errno(r, "Failed to update partition flags: %m");
313
314 } else if (tweak_no_auto || tweak_read_only || tweak_growfs) {
315 uint64_t old_flags, new_flags;
316
317 /* So we aren't supposed to update the full flags parameter, but we are supposed to update
318 * the RO flag of it. */
319
320 r = fdisk_partition_get_attrs_as_uint64(pa, &old_flags);
321 if (r < 0)
322 return log_error_errno(r, "Failed to get old partition flags: %m");
323
324 new_flags = old_flags;
325 if (tweak_no_auto)
326 SET_FLAG(new_flags, SD_GPT_FLAG_NO_AUTO, info->no_auto);
327 if (tweak_read_only)
328 SET_FLAG(new_flags, SD_GPT_FLAG_READ_ONLY, info->read_only);
329 if (tweak_growfs)
330 SET_FLAG(new_flags, SD_GPT_FLAG_GROWFS, info->growfs);
331
332 if (new_flags != old_flags) {
333 r = fdisk_partition_set_attrs_as_uint64(pa, new_flags);
334 if (r < 0)
335 return log_error_errno(r, "Failed to update partition flags: %m");
336 }
337 }
338
339 r = sym_fdisk_set_partition(c, info->partno, pa);
340 if (r < 0)
341 return log_error_errno(r, "Failed to update partition: %m");
342
343 r = sym_fdisk_write_disklabel(c);
344 if (r < 0)
345 return log_error_errno(r, "Failed to write updated partition table: %m");
346
347 return 0;
348 }