plan.h (5455B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef ARUU_LIBDISK_PLAN_H 3 #define ARUU_LIBDISK_PLAN_H 4 5 /* a disk as someone means it to be: its partition table, and for each 6 * partition the filesystem it is to hold, where that is mounted, its label 7 * and whether it is made fresh. what a partitioner edits, an installer is 8 * handed, and a file says (plan_save, plan_load), so each of them is the 9 * same thing in the same words. 10 * 11 * the file is text, a line each: 12 * 13 * disk /dev/vda 14 * label gpt 15 * part 1 start=2048 sectors=2048 type=ef02 name=BIOS 16 * part 2 start=4096 sectors=1048576 type=ef00 name=ESP fs=vfat mount=/boot/efi format=yes 17 * part 3 start=1052672 sectors=0 type=8300 name=ROOT fs=ext4 mount=/ format=yes 18 * 19 * sectors=0 is what is left of the disk. nothing here writes a disk but 20 * plan_write_table, and what it makes of the filesystems is the argv 21 * plan_mkfs_argv gives, for the caller to run */ 22 23 #include "label.h" 24 25 #include <stdio.h> 26 27 #define PLAN_PATH 128 28 #define PLAN_FS 16 29 #define PLAN_MOUNT 64 30 #define PLAN_LABEL 32 31 32 struct plan_part { 33 char fs[PLAN_FS]; /* what it is to hold: a name of plan_filesystems, "" for none */ 34 char mount[PLAN_MOUNT]; /* where it is mounted in the installed system, "" for nowhere */ 35 char label[PLAN_LABEL]; /* the filesystem's label */ 36 int format; /* the filesystem is made, else what is there is kept */ 37 char found[PLAN_FS]; /* what is on it now, "" when nothing is known */ 38 }; 39 40 struct plan { 41 char disk[PLAN_PATH]; 42 struct label label; 43 struct plan_part attr[LABEL_SLOTS]; 44 }; 45 46 /* the filesystems a partition can hold */ 47 struct plan_fs { 48 const char *name; 49 const char *title; /* for a person */ 50 const char *mount_as; /* the type the system mounts it as, NULL for none (swap) */ 51 int bootable; /* a root filesystem the system can start from */ 52 int max_label; 53 }; 54 const struct plan_fs *plan_filesystems(int *n); 55 const struct plan_fs *plan_filesystem(const char *name); 56 57 /* the device of partition slot (0 based) of disk: /dev/vda1, /dev/nvme0n1p1 */ 58 void plan_part_path(const char *disk, int slot, char *buf, size_t n); 59 60 /* disk read: its table, and what is on each partition. a disk with none is an 61 * empty plan of no table. -1 when it cannot be read */ 62 int plan_read_disk(struct plan *p, const char *disk); 63 64 /* the partition's attributes set. false for a filesystem not known or a label too long */ 65 int plan_set( 66 struct plan *p, int slot, const char *fs, const char *mount, const char *label, int format 67 ); 68 /* label_add, with what it is to hold: the slot or a negative errno */ 69 int plan_add( 70 struct plan *p, 71 uint64_t start, 72 uint64_t sectors, 73 const char *type, 74 const char *name, 75 const char *fs, 76 const char *mount, 77 const char *label 78 ); 79 int plan_delete(struct plan *p, int slot); 80 81 /* what the plan needs to be installed to: the checks asked for */ 82 #define PLAN_NEED_ROOT 1 /* a partition mounted at /, of a filesystem to start from */ 83 #define PLAN_NEED_BOOT \ 84 2 /* a way for the machine to start: an EFI system partition at /boot/efi or a BIOS boot one */ 85 /* 0 when the plan holds, else -1 and why, in words for a person */ 86 int plan_check(const struct plan *p, unsigned need, char *why, size_t n); 87 88 /* what applying now would do over before, a line each (fn may be NULL to count), to read before it 89 * is done 90 * ("delete /dev/vda2", "create 512 MiB EFI system", "format /dev/vda3 as ext4"). how many */ 91 int plan_changes( 92 const struct plan *before, 93 const struct plan *now, 94 void (*fn)(const char *line, void *arg), 95 void *arg 96 ); 97 98 /* the layout an installer makes of a whole disk when it is not asked for another: a 1 MiB BIOS boot 99 * partition, a 512 MiB EFI system one at /boot/efi, swap when swap_mib is not 0, and the rest the 100 * root filesystem root_fs. 0, or -1 for a disk that cannot hold it */ 101 #define PLAN_BIOS_MIB 1 102 #define PLAN_ESP_MIB 512 103 int plan_guided(struct plan *p, const char *disk, uint64_t swap_mib, const char *root_fs); 104 105 /* a part of the plan by its role: "esp", "root" and "swap" the device (/dev/vda2), "bios" the 106 * number of the BIOS boot partition, "rootfs" the filesystem at /. false when the plan has none */ 107 int plan_role(const struct plan *p, const char *role, char *buf, size_t n); 108 /* the slots that have a place to be mounted, in the order to mount them: / before /boot before 109 * /boot/efi. how many, at most max */ 110 int plan_mount_order(const struct plan *p, int *slots, int max); 111 /* waits for the devices of the partitions to be there after the table is written, up to tenths of a 112 * second */ 113 int plan_wait_nodes(const struct plan *p, int tenths); 114 115 /* the table written to the disk, the filesystems left for the caller */ 116 int plan_write_table(const struct plan *p); 117 /* the command that makes the filesystem of slot: its argv into argv (at most max words, 118 * NULL ended) whose words are held in store. 0, -1 when the slot has no filesystem to make */ 119 int 120 plan_mkfs_argv(const struct plan *p, int slot, char **argv, int max, char *store, size_t store_n); 121 122 int plan_save(const struct plan *p, FILE *f); 123 /* a plan read: -1 with why for a line that is not one */ 124 int plan_load(struct plan *p, FILE *f, char *why, size_t n); 125 126 /* a size for a person, "512 MiB" */ 127 void plan_size(uint64_t bytes, char *buf, size_t n); 128 129 #endif