partitions.h revision 1.18 1 /* $NetBSD: partitions.h,v 1.18 2020/10/12 16:14:32 martin Exp $ */
2
3 /*
4 * Copyright 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 /*
31 * Abstract interface to access arbitrary disk partitioning schemes and
32 * keep Sysinst proper independent of the implementation / on-disk
33 * details.
34 *
35 * NOTE:
36 * - all sector numbers, alignement and sizes are in units of the
37 * disks physical sector size (not necessarily 512 bytes)!
38 * - some interfaces pass the disks sector size (when it is easily
39 * available at typical callers), but the backends can always
40 * assume it to be equal to the real physical sector size. If
41 * no value is passed, the backend can query the disk data
42 * via get_disk_geom().
43 * - single exception: disk_partitioning_scheme::size_limit is in 512
44 * byte sectors (as it is not associated with a concrete disk)
45 */
46
47 #include <sys/types.h>
48 #include <stdbool.h>
49 #include "msg_defs.h"
50
51 /*
52 * Import all the file system types, as enum fs_type.
53 */
54 #define FSTYPE_ENUMNAME fs_type
55 #define FSTYPENAMES
56 #include <sys/disklabel.h>
57 #undef FSTYPE_ENUMNAME
58
59 #ifndef FS_TMPFS
60 #define FS_TMPFS 256 /* random value (outside uint8_t range) */
61 #endif
62 #ifndef FS_MFS
63 #define FS_MFS 257 /* another random (out of range) value */
64 #endif
65
66 #define MAX_LABEL_LEN 128 /* max. length of a partition label */
67 #define MAX_SHORTCUT_LEN 8 /* max. length of a shortcut ("a:") */
68
69 /*
70 * A partition index / handle, identifies a singlepartition within
71 * a struct disk_partitions. This is just an iterator/index - whenever
72 * changes to the set of partitions are done, partitions may get a new
73 * part_id.
74 * We assume that partitioning schemes keep partitions sorted (with
75 * key = start address, some schemes will have overlapping partitions,
76 * like MBR extended partitions).
77 */
78 typedef size_t part_id;
79
80 /*
81 * An invalid value for a partition index / handle
82 */
83 #define NO_PART ((part_id)~0U)
84
85 /*
86 * Intended usage for a partition
87 */
88 enum part_type {
89 PT_undef, /* invalid value */
90 PT_unknown, /* anything we can not map to one of these */
91 PT_root, /* the NetBSD / partition (bootable) */
92 PT_swap, /* the NetBSD swap partition */
93 PT_FAT, /* boot partition (e.g. for u-boot) */
94 PT_EXT2, /* boot partition (for Linux appliances) */
95 PT_SYSVBFS, /* boot partition (for some SYSV machines) */
96 PT_EFI_SYSTEM, /* (U)EFI boot partition */
97 };
98
99 /*
100 * A generic structure describing partition types for menu/user interface
101 * purposes. The internal details may be richer and the *pointer* value
102 * is the unique token - that is: the partitioning scheme will hand out
103 * pointers to internal data and recognize the exact partition type details
104 * by pointer comparision.
105 */
106 struct part_type_desc {
107 enum part_type generic_ptype; /* what this maps to in generic terms */
108 const char *short_desc; /* short type description */
109 const char *description; /* full description */
110 };
111
112 /* Bits for disk_part_info.flags: */
113 #define PTI_SEC_CONTAINER 1 /* this covers our secondary
114 partitions */
115 #define PTI_WHOLE_DISK 2 /* all of the NetBSD disk */
116 #define PTI_BOOT 4 /* required for booting */
117 #define PTI_PSCHEME_INTERNAL 8 /* no user partition, e.g.
118 MBRs extend partition */
119 #define PTI_RAW_PART 16 /* total disk */
120 #define PTI_INSTALL_TARGET 32 /* marks the target partition
121 * assumed to become / after
122 * reboot; may not be
123 * persistent; may only be
124 * set for a single partition!
125 */
126
127 /* A single partition */
128 struct disk_part_info {
129 daddr_t start, size; /* start and size on disk */
130 uint32_t flags; /* active PTI_ flags */
131 const struct part_type_desc *nat_type; /* native partition type */
132 /*
133 * The following will only be available
134 * a) for a small subset of file system types
135 * b) if the partition (in this state) has already been
136 * used before
137 * It is OK to leave all these zeroed / NULL when setting
138 * partition data - or leave them at the last values a get operation
139 * returned. Backends can not rely on them to be valid.
140 */
141 const char *last_mounted; /* last mount point or NULL */
142 unsigned int fs_type, fs_sub_type; /* FS_* type of filesystem
143 * and for some FS a sub
144 * type (e.g. FFSv1 vs. FFSv2)
145 */
146 };
147
148 /* An unused area that may be used for new partitions */
149 struct disk_part_free_space {
150 daddr_t start, size;
151 };
152
153 /*
154 * Some partition schemes define additional data that needs to be edited.
155 * These attributes are described in this structure and referenced by
156 * their index into the fixed list of available attributes.
157 */
158 enum custom_attr_type { pet_bool, pet_cardinal, pet_str };
159 struct disk_part_custom_attribute {
160 msg label; /* Name, like "active partition" */
161 enum custom_attr_type type; /* bool, long, char* */
162 size_t strlen; /* maximum length if pet_str */
163 };
164
165 /*
166 * When displaying a partition editor, we have standard colums, but
167 * partitioning schemes add custom columns to the table as well.
168 * There is a fixed number of columns and they are described by this
169 * structure:
170 */
171 struct disk_part_edit_column_desc {
172 msg title;
173 unsigned int width;
174 };
175
176 struct disk_partitions; /* in-memory represenation of a set of partitions */
177
178 /*
179 * When querying partition "device" names, we may ask for:
180 */
181 enum dev_name_usage {
182 parent_device_only, /* wd0 instead of wd0i, no path */
183 logical_name, /* NAME=my-root instead of dk7 */
184 plain_name, /* e.g. /dev/wd0i or /dev/dk7 */
185 raw_dev_name, /* e.g. /dev/rwd0i or /dev/rdk7 */
186 };
187
188 /*
189 * A scheme how to store partitions on-disk, and methods to read/write
190 * them to/from our abstract internal presentation.
191 */
192 struct disk_partitioning_scheme {
193 /* name of the on-disk scheme, retrieved via msg_string */
194 msg name, short_name;
195
196 /* prompt shown when creating custom partition types */
197 msg new_type_prompt;
198
199 /* description of scheme specific partition flags */
200 msg part_flag_desc;
201
202 /*
203 * size restrictions for this partitioning scheme (number
204 * of 512 byte sectors max)
205 */
206 daddr_t size_limit; /* 0 if not limited */
207
208 /*
209 * If this scheme allows sub-partitions (i.e. MBR -> disklabel),
210 * this is a pointer to the (potential/optional) secondary
211 * scheme. Depending on partitioning details it may not be
212 * used in the end.
213 * This link is only here for better help messages.
214 * See *secondary_partitions further below for actually accesing
215 * secondary partitions.
216 */
217 const struct disk_partitioning_scheme *secondary_scheme;
218
219 /*
220 * Partition editor colum descriptions for whatever the scheme
221 * needs to display (see format_partition_table_str below).
222 */
223 size_t edit_columns_count;
224 const struct disk_part_edit_column_desc *edit_columns;
225
226 /*
227 * Custom attributes editable by the partitioning scheme (but of
228 * no particular meaning for sysinst)
229 */
230 size_t custom_attribute_count;
231 const struct disk_part_custom_attribute *custom_attributes;
232
233 /*
234 * Partition types supported by this scheme,
235 * first function gets the number, second queries single elements
236 */
237 size_t (*get_part_types_count)(void);
238 const struct part_type_desc * (*get_part_type)(size_t ndx);
239 /*
240 * Get the prefered native representation for a generic partition type
241 */
242 const struct part_type_desc * (*get_generic_part_type)(enum part_type);
243 /*
244 * Get the prefered native partition type for a specific file system
245 * type (FS_*) and subtype (fs specific value)
246 */
247 const struct part_type_desc * (*get_fs_part_type)(
248 enum part_type, unsigned, unsigned);
249 /*
250 * Optional: inverse to above: given a part_type_desc, set default
251 * fstype and subtype.
252 */
253 bool (*get_default_fstype)(const struct part_type_desc *,
254 unsigned *fstype, unsigned *fs_sub_type);
255 /*
256 * Create a custom partition type. If the type already exists
257 * (or there is a collision), the old existing type will be
258 * returned and no new type created. This is not considered
259 * an error (to keep the user interface simple).
260 * On failure NULL is returned and (if passed != NULL)
261 * *err_msg is set to a message describing the error.
262 */
263 const struct part_type_desc * (*create_custom_part_type)
264 (const char *custom, const char **err_msg);
265 /*
266 * Return a usable internal partition type representation
267 * for types that are not otherwise mappable.
268 * This could be FS_OTHER for disklabel, or a randomly
269 * created type guid for GPT. This type may or may not be
270 * in the regular type list. If not, it needs to behave like a
271 * custom type.
272 */
273 const struct part_type_desc * (*create_unknown_part_type)(void);
274
275 /*
276 * Global attributes
277 */
278 /*
279 * Get partition alignment suggestion. The schemen may enforce
280 * additional/different alignment for some partitions.
281 */
282 daddr_t (*get_part_alignment)(const struct disk_partitions*);
283
284 /*
285 * Methods to manipulate the in-memory abstract representation
286 */
287
288 /* Retrieve data about a single partition, identified by the part_id.
289 * Fill the disk_part_info structure
290 */
291 bool (*get_part_info)(const struct disk_partitions*, part_id,
292 struct disk_part_info*);
293
294 /* Optional: fill a atribute string describing the given partition */
295 bool (*get_part_attr_str)(const struct disk_partitions*, part_id,
296 char *str, size_t avail_space);
297 /* Format a partition editor element for the "col" column in
298 * edit_columns. Used e.g. with MBR to set "active" flags.
299 */
300 bool (*format_partition_table_str)(const struct disk_partitions*,
301 part_id, size_t col, char *outstr, size_t outspace);
302
303 /* is the type of this partition changable? */
304 bool (*part_type_can_change)(const struct disk_partitions*,
305 part_id);
306
307 /* can we add further partitions? */
308 bool (*can_add_partition)(const struct disk_partitions*);
309
310 /* is the custom attribut changable? */
311 bool (*custom_attribute_writable)(const struct disk_partitions*,
312 part_id, size_t attr_no);
313 /*
314 * Output formatting for custom attributes.
315 * If "info" is != NULL, use (where it makes sense)
316 * values from that structure, as if a call to set_part_info
317 * would have been done before this call.
318 */
319 bool (*format_custom_attribute)(const struct disk_partitions*,
320 part_id, size_t attr_no, const struct disk_part_info *info,
321 char *out, size_t out_space);
322 /* value setter functions for custom attributes */
323 /* pet_bool: */
324 bool (*custom_attribute_toggle)(struct disk_partitions*,
325 part_id, size_t attr_no);
326 /* pet_cardinal: */
327 bool (*custom_attribute_set_card)(struct disk_partitions*,
328 part_id, size_t attr_no, long new_val);
329 /* pet_str or pet_cardinal: */
330 bool (*custom_attribute_set_str)(struct disk_partitions*,
331 part_id, size_t attr_no, const char *new_val);
332
333 /*
334 * Optional: additional user information when showing the size
335 * editor (especially for existing unknown partitions)
336 */
337 const char * (*other_partition_identifier)(const struct
338 disk_partitions*, part_id);
339
340
341 /* Retrieve device and partition names, e.g. for checking
342 * against kern.root_device or invoking newfs.
343 * For disklabel partitions, "part" will be set to the partition
344 * index (a = 0, b = 1, ...), for others it will get set to -1.
345 * If dev_name_usage is parent_device_only, the device name will
346 * not include a partition letter - obviously this only makes a
347 * difference with disklabel partitions.
348 * If dev_name_usage is logical_name instead of a device name
349 * a given name may be returned in NAME= syntax.
350 * If with_path is true (and the returned value is a device
351 * node), include the /dev/ prefix in the result string
352 * (this is ignored when returning NAME= syntax for /etc/fstab).
353 * If life is true, the device must be made available under
354 * that name (only makes a difference for NAME=syntax if
355 * no wedge has been created yet,) - implied for all variants
356 * where dev_name_usage != logical_name.
357 */
358 bool (*get_part_device)(const struct disk_partitions*,
359 part_id, char *devname, size_t max_devname_len, int *part,
360 enum dev_name_usage, bool with_path, bool life);
361
362 /*
363 * How big could we resize the given position (start of existing
364 * partition or free space)
365 */
366 daddr_t (*max_free_space_at)(const struct disk_partitions*, daddr_t);
367
368 /*
369 * Provide a list of free spaces usable for further partitioning,
370 * assuming the given partition alignment.
371 * If start is > 0 no space with lower sector numbers will
372 * be found.
373 * If ignore is > 0, any partition starting at that sector will
374 * be considered "free", this is used e.g. when moving an existing
375 * partition around.
376 */
377 size_t (*get_free_spaces)(const struct disk_partitions*,
378 struct disk_part_free_space *result, size_t max_num_result,
379 daddr_t min_space_size, daddr_t align, daddr_t start,
380 daddr_t ignore /* -1 */);
381
382 /*
383 * Translate a partition description from a foreign partitioning
384 * scheme as close as possible to what we can handle in add_partition.
385 * This mostly adjusts flags and partition type pointers (using
386 * more lose matching than add_partition would do).
387 */
388 bool (*adapt_foreign_part_info)(
389 const struct disk_partitions *myself, struct disk_part_info *dest,
390 const struct disk_partitioning_scheme *src_scheme,
391 const struct disk_part_info *src);
392
393 /*
394 * Update data for an existing partition
395 */
396 bool (*set_part_info)(struct disk_partitions*, part_id,
397 const struct disk_part_info*, const char **err_msg);
398
399 /* Add a new partition and return its part_id. */
400 part_id (*add_partition)(struct disk_partitions*,
401 const struct disk_part_info*, const char **err_msg);
402
403 /*
404 * Optional: add a partition from an outer scheme, accept all
405 * details w/o verification as best as possible.
406 */
407 part_id (*add_outer_partition)(struct disk_partitions*,
408 const struct disk_part_info*, const char **err_msg);
409
410 /* Delete all partitions */
411 bool (*delete_all_partitions)(struct disk_partitions*);
412
413 /* Optional: delete any partitions inside the given range */
414 bool (*delete_partitions_in_range)(struct disk_partitions*,
415 daddr_t start, daddr_t size);
416
417 /* Delete the specified partition */
418 bool (*delete_partition)(struct disk_partitions*, part_id,
419 const char **err_msg);
420
421 /*
422 * Methods for the whole set of partitions
423 */
424 /*
425 * If this scheme only creates a singly NetBSD partition, which
426 * then is sub-partitioned (usually by disklabel), this returns a
427 * pointer to the secondary partition set.
428 * Otherwise NULL is returned, e.g. when there is no
429 * NetBSD partition defined (so this might change over time).
430 * Schemes that NEVER use a secondary scheme set this
431 * function pointer to NULL.
432 *
433 * If force_empty = true, ignore all on-disk contents and just
434 * create a new disk_partitons structure for the secondary scheme
435 * (this is used after deleting all partitions and setting up
436 * things for "use whole disk").
437 *
438 * The returned pointer is always owned by the primary partitions,
439 * caller MUST never free it, but otherwise can manipulate it
440 * arbitrarily.
441 */
442 struct disk_partitions *
443 (*secondary_partitions)(struct disk_partitions *, daddr_t start,
444 bool force_empty);
445
446 /*
447 * Write the whole set (in new_state) back to disk.
448 */
449 bool (*write_to_disk)(struct disk_partitions *new_state);
450
451 /*
452 * Try to read partitions from a disk, return NULL if this is not
453 * the partitioning scheme in use on that device.
454 * Usually start and len are 0 (and ignored).
455 * If this is about a part of a disk (like only the NetBSD
456 * MBR partition, start and len are the valid part of the
457 * disk.
458 */
459 struct disk_partitions * (*read_from_disk)(const char *,
460 daddr_t start, daddr_t len, size_t bytes_per_sec,
461 const struct disk_partitioning_scheme *);
462
463 /*
464 * Set up all internal data for a new disk.
465 */
466 struct disk_partitions * (*create_new_for_disk)(const char *,
467 daddr_t start, daddr_t len, bool is_boot_drive,
468 struct disk_partitions *parent);
469
470 /*
471 * Optional: this scheme may be used to boot from the given disk
472 */
473 bool (*have_boot_support)(const char *disk);
474
475 /*
476 * Optional: try to guess disk geometry from the partition information
477 */
478 int (*guess_disk_geom)(struct disk_partitions *,
479 int *cyl, int *head, int *sec);
480
481 /*
482 * Return a "cylinder size" (in number of blocks) - whatever that
483 * means to a particular partitioning scheme.
484 */
485 size_t (*get_cylinder_size)(const struct disk_partitions *);
486
487 /*
488 * Optional: change used geometry info and update internal state
489 */
490 bool (*change_disk_geom)(struct disk_partitions *,
491 int cyl, int head, int sec);
492
493 /*
494 * Optional:
495 * Get or set a name for the whole disk (most partitioning
496 * schemes do not provide this). Used for disklabel "pack names",
497 * which then may be used for aut-discovery of wedges, so it
498 * makes sense for the user to edit them.
499 */
500 bool (*get_disk_pack_name)(const struct disk_partitions *,
501 char *, size_t);
502 bool (*set_disk_pack_name)(struct disk_partitions *, const char *);
503
504 /*
505 * Optional:
506 * Find a partition by name (as used in /etc/fstab NAME= entries)
507 */
508 part_id (*find_by_name)(struct disk_partitions *, const char *name);
509
510 /*
511 * Optional:
512 * Try to guess install target partition from internal data,
513 * returns true if a safe match was found and sets start/size
514 * to the target partition.
515 */
516 bool (*guess_install_target)(const struct disk_partitions *,
517 daddr_t *start, daddr_t *size);
518
519 /*
520 * Optional: verify that the whole set of partitions would be bootable,
521 * fix up any issues (with user interaction) where needed.
522 * If "quiet" is true, fix up everything silently if possible
523 * and never return 1.
524 * Returns:
525 * 0: abort install
526 * 1: re-edit partitions
527 * 2: use anyway (continue)
528 */
529 int (*post_edit_verify)(struct disk_partitions *, bool quiet);
530
531 /*
532 * Optional: called during updates, before mounting the target disk(s),
533 * before md_pre_update() is called. Can be used to fixup
534 * partition info for historic errors (e.g. i386 changing MBR
535 * partition type from 165 to 169), similar to post_edit_verify.
536 * Returns:
537 * true if the partition info has changed (write back required)
538 * false if nothing further needs to be done.
539 */
540 bool (*pre_update_verify)(struct disk_partitions *);
541
542 /* Free all the data */
543 void (*free)(struct disk_partitions*);
544
545 /* Wipe all on-disk state, leave blank disk - and free data */
546 void (*destroy_part_scheme)(struct disk_partitions*);
547
548 /* Scheme global cleanup */
549 void (*cleanup)(void);
550 };
551
552 /*
553 * The in-memory representation of all partitions on a concrete disk,
554 * tied to the partitioning scheme in use.
555 *
556 * Concrete schemes will derive from the abstract disk_partitions
557 * structure (by aggregation), but consumers of the API will only
558 * ever see this public part.
559 */
560 struct disk_partitions {
561 /* which partitioning scheme is in use */
562 const struct disk_partitioning_scheme *pscheme;
563
564 /* the disk device this came from (or should go to) */
565 const char *disk;
566
567 /* global/public disk data */
568
569 /*
570 * The basic unit of size used for this disk (all "start",
571 * "size" and "align" values are in this unit).
572 */
573 size_t bytes_per_sector; /* must be 2^n and >= 512 */
574
575 /*
576 * Valid partitions may have IDs in the range 0 .. num_part (excl.)
577 */
578 part_id num_part;
579
580 /*
581 * If this is a sub-partitioning, the start of the "disk" is
582 * some arbitrary partition in the parent. Sometimes we need
583 * to be able to calculate absoluted offsets.
584 */
585 daddr_t disk_start;
586 /*
587 * Total size of the disk (usable for partitioning)
588 */
589 daddr_t disk_size;
590
591 /*
592 * Space not yet allocated
593 */
594 daddr_t free_space;
595
596 /*
597 * If this is the secondary partitioning scheme, pointer to
598 * the outer one. Otherwise NULL.
599 */
600 struct disk_partitions *parent;
601 };
602
603 /*
604 * A list of partitioning schemes, so we can iterate over everything
605 * supported (e.g. when partitioning a new disk). NULL terminated.
606 */
607 extern const struct disk_partitioning_scheme **available_part_schemes;
608 extern size_t num_available_part_schemes;
609
610 /*
611 * Generic reader - query a disk device and read all partitions from it
612 */
613 struct disk_partitions *
614 partitions_read_disk(const char *, daddr_t disk_size,
615 size_t bytes_per_sector, bool no_mbr);
616
617 /*
618 * Generic part info adaption, may be overriden by individual partitionin
619 * schemes
620 */
621 bool generic_adapt_foreign_part_info(
622 const struct disk_partitions *myself, struct disk_part_info *dest,
623 const struct disk_partitioning_scheme *src_scheme,
624 const struct disk_part_info *src);
625
626 /*
627 * One time initialization and clenaup
628 */
629 void partitions_init(void);
630 void partitions_cleanup(void);
631
632