2 * Copyright (c)2004 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of the DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Disk utility functions for installer.
37 * $Id: diskutil.c,v 1.44 2005/02/07 06:41:42 cpressey Exp $
45 #include "libaura/mem.h"
46 #include "libaura/fspred.h"
47 #include "libaura/popen.h"
49 #include "libdfui/dfui.h"
50 #include "libdfui/dump.h"
52 #define NEEDS_DISKUTIL_STRUCTURE_DEFINITIONS
54 #undef NEEDS_DISKUTIL_STRUCTURE_DEFINITIONS
57 #include "functions.h"
61 static int disk_description_is_better(const char *, const char *);
63 /** STORAGE DESCRIPTORS **/
70 AURA_MALLOC(s, storage);
74 s->selected_disk = NULL;
75 s->selected_slice = NULL;
82 storage_get_tmpfs_status(const char *mountpoint, struct storage *s)
84 struct subpartition *sp;
86 for (sp = slice_subpartition_first(s->selected_slice);
87 sp != NULL; sp = subpartition_next(sp)) {
88 if(strcmp(subpartition_get_mountpoint(sp), mountpoint) == 0) {
89 if(subpartition_is_tmpfsbacked(sp) == 1) {
100 storage_free(struct storage *s)
103 AURA_FREE(s, storage);
107 storage_set_memsize(struct storage *s, unsigned long memsize)
113 storage_get_memsize(const struct storage *s)
119 storage_disk_first(const struct storage *s)
121 return(s->disk_head);
125 storage_set_selected_disk(struct storage *s, struct disk *d)
127 s->selected_disk = d;
131 storage_get_selected_disk(const struct storage *s)
133 return(s->selected_disk);
137 storage_set_selected_slice(struct storage *s, struct slice *sl)
139 s->selected_slice = sl;
143 storage_get_selected_slice(const struct storage *s)
145 return(s->selected_slice);
149 * Create a new disk description structure.
152 disk_new(struct storage *s, const char *dev_name)
156 if (disk_find(s, dev_name) != NULL) {
157 /* Already discovered */
161 AURA_MALLOC(d, disk);
163 d->device = aura_strdup(dev_name);
169 d->cylinders = -1; /* -1 indicates "we don't know" */
173 d->slice_head = NULL;
174 d->slice_tail = NULL;
177 if (s->disk_head == NULL)
180 s->disk_tail->next = d;
182 d->prev = s->disk_tail;
189 disk_description_is_better(const char *existing, const char *new_desc __unused)
191 if (existing == NULL)
197 disk_get_desc(const struct disk *d)
203 disk_set_desc(struct disk *d, const char *desc)
207 if (!disk_description_is_better(d->desc, desc))
211 d->desc = aura_strdup(desc);
214 * Get the disk's total capacity.
215 * XXX we should do this with C/H/S ?
218 while (*c != ':' && *c != '\0')
223 d->capacity = atoi(c + 1);
227 * Returns the name of the device node used to represent the disk.
228 * Note that the storage used for the returned string is static,
229 * and the string is overwritten each time this function is called.
232 disk_get_device_name(const struct disk *d)
234 static char tmp_dev_name[256];
236 snprintf(tmp_dev_name, 256, "%s", d->device);
237 return(tmp_dev_name);
241 disk_get_serno(const struct disk *d)
247 disk_set_serno(struct disk *d, const char *serno)
249 d->serno = aura_strdup(serno);
253 disk_get_number(const struct disk *d)
259 disk_set_number(struct disk *d, const int number)
265 * Find the first disk description structure in the given
266 * storage description which matches the given device name
267 * prefix. Note that this means that if a storage
268 * description s contains disks named "ad0" and "ad1",
269 * disk_find(s, "ad0s1c") will return a pointer to the disk
270 * structure for "ad0".
273 disk_find(const struct storage *s, const char *device)
275 struct disk *d = s->disk_head;
278 if (strncmp(device, d->device, strlen(d->device)) == 0)
287 disk_next(const struct disk *d)
293 disk_slice_first(const struct disk *d)
295 return(d->slice_head);
299 disk_set_formatted(struct disk *d, int formatted)
301 d->we_formatted = formatted;
305 disk_get_formatted(const struct disk *d)
307 return(d->we_formatted);
311 disk_set_geometry(struct disk *d, int cyl, int hd, int sec)
319 disk_get_geometry(const struct disk *d, int *cyl, int *hd, int *sec)
327 * Free the memory allocated to hold the set of disk descriptions.
330 disks_free(struct storage *s)
332 struct disk *d = s->disk_head, *next;
336 slices_free(d->slice_head);
348 * Create a new slice description and add it to a disk description.
351 slice_new(struct disk *d, int number, int type, int flags,
352 unsigned long start, unsigned long size)
355 const char *sysid_desc = NULL;
359 dfui_debug("** adding slice %d (start %ld, size %ld, sysid %d) "
360 "to disk %s\n", number, start, size, type, d->device);
362 AURA_MALLOC(s, slice);
366 s->subpartition_head = NULL;
367 s->subpartition_tail = NULL;
376 if (part_types[i].type == type) {
377 sysid_desc = part_types[i].name;
380 if (part_types[i].type == 255)
383 if (sysid_desc == NULL) {
384 snprintf(unknown, 256, "??? Unknown, sysid = %d", type);
385 sysid_desc = unknown;
388 asprintf(&s->desc, "%ldM - %ldM: %s",
389 start / 2048, (start + size) / 2048, sysid_desc);
390 s->capacity = size / 2048;
393 if (d->slice_head == NULL)
396 d->slice_tail->next = s;
398 s->prev = d->slice_tail;
405 * Find a slice description on a given disk description given the
409 slice_find(const struct disk *d, int number)
411 struct slice *s = d->slice_head;
414 if (s->number == number)
423 slice_next(const struct slice *s)
429 * Returns the name of the device node used to represent the slice.
430 * Note that the storage used for the returned string is static,
431 * and the string is overwritten each time this function is called.
434 slice_get_device_name(const struct slice *s)
436 static char tmp_dev_name[256];
438 snprintf(tmp_dev_name, 256, "%ss%d", s->parent->device, s->number);
439 return(tmp_dev_name);
443 slice_get_number(const struct slice *s)
449 slice_get_desc(const struct slice *s)
455 slice_get_capacity(const struct slice *s)
461 slice_get_start(const struct slice *s)
467 slice_get_size(const struct slice *s)
473 slice_get_type(const struct slice *s)
479 slice_get_flags(const struct slice *s)
484 struct subpartition *
485 slice_subpartition_first(const struct slice *s)
487 return(s->subpartition_head);
491 * Free all memory for a list of slice descriptions.
494 slices_free(struct slice *head)
498 while (head != NULL) {
500 subpartitions_free(head);
502 AURA_FREE(head, slice);
507 struct subpartition *
508 subpartition_new_hammer(struct slice *s, const char *mountpoint, long capacity,
511 struct subpartition *sp;
513 AURA_MALLOC(sp, subpartition);
517 struct subpartition *last = s->subpartition_tail;
520 } else if (last->letter == 'b') {
523 sp->letter = (char)(last->letter + 1);
526 sp->mountpoint = aura_strdup(mountpoint);
527 sp->capacity = capacity;
528 sp->encrypted = encrypted;
529 sp->type = FS_HAMMER;
532 * We need this here, because a UFS /boot needs valid values
534 if (sp->capacity < 1024)
539 if (sp->capacity < 1024)
546 if (strcasecmp(mountpoint, "swap") == 0)
548 if (strcmp(mountpoint, "/") != 0 && strcmp(mountpoint, "/boot") != 0 &&
549 strcmp(mountpoint, "swap") != 0)
553 if (s->subpartition_head == NULL)
554 s->subpartition_head = sp;
556 s->subpartition_tail->next = sp;
558 sp->prev = s->subpartition_tail;
559 s->subpartition_tail = sp;
565 * NOTE: arguments to this function are not checked for sanity.
567 * fsize and/or bsize may both be -1, indicating
568 * "choose a reasonable default."
570 struct subpartition *
571 subpartition_new_ufs(struct slice *s, const char *mountpoint, long capacity,
572 int encrypted, int softupdates, long fsize, long bsize, int tmpfsbacked)
574 struct subpartition *sp, *sptmp;
577 AURA_MALLOC(sp, subpartition);
581 sp->mountpoint = aura_strdup(mountpoint);
582 sp->capacity = capacity;
583 sp->encrypted = encrypted;
587 if (sp->capacity < 1024)
596 if (sp->capacity < 1024)
604 if (softupdates == -1) {
605 if (strcmp(mountpoint, "/") == 0)
610 sp->softupdates = softupdates;
613 sp->tmpfsbacked = tmpfsbacked;
616 if (strcasecmp(mountpoint, "swap") == 0)
619 if (s->subpartition_head == NULL) {
620 s->subpartition_head = sp;
621 s->subpartition_tail = sp;
623 for (sptmp = s->subpartition_head; sptmp != NULL;
624 sptmp = sptmp->next) {
625 if (strcmp(sptmp->mountpoint, sp->mountpoint) > 0)
629 if (s->subpartition_head == sptmp)
630 s->subpartition_head = sp;
632 sptmp->prev->next = sp;
634 sp->prev = sptmp->prev;
637 sp->prev = s->subpartition_tail;
638 s->subpartition_tail->next = sp;
639 s->subpartition_tail = sp;
643 for (sptmp = s->subpartition_head; sptmp != NULL;
644 sptmp = sptmp->next) {
645 if (sptmp->tmpfsbacked)
647 else if (strcmp(sptmp->mountpoint, "/") == 0 ||
648 strcmp(sptmp->mountpoint, "/dummy") == 0)
650 else if (strcasecmp(sptmp->mountpoint, "swap") == 0)
653 sptmp->letter = letter++;
660 * Find the subpartition description in the given storage
661 * description whose mountpoint matches the given string exactly.
663 struct subpartition *
664 subpartition_find(const struct slice *s, const char *fmt, ...)
666 struct subpartition *sp = s->subpartition_head;
671 vasprintf(&mountpoint, fmt, args);
675 if (strcmp(mountpoint, sp->mountpoint) == 0) {
687 * Find the subpartition description in the given storage
688 * description where the given filename would presumably
689 * reside. This is the subpartition whose mountpoint is
690 * the longest match for the given filename.
692 struct subpartition *
693 subpartition_of(const struct slice *s, const char *fmt, ...)
695 struct subpartition *sp = s->subpartition_head;
696 struct subpartition *csp = NULL;
702 vasprintf(&filename, fmt, args);
706 if (strlen(sp->mountpoint) > len &&
707 strlen(sp->mountpoint) <= strlen(filename) &&
708 strncmp(filename, sp->mountpoint, strlen(sp->mountpoint)) == 0) {
710 len = strlen(csp->mountpoint);
719 struct subpartition *
720 subpartition_find_capacity(const struct slice *s, long capacity)
722 struct subpartition *sp = s->subpartition_head;
725 if (sp->capacity == capacity)
733 struct subpartition *
734 subpartition_next(const struct subpartition *sp)
740 subpartition_get_pfs(const struct subpartition *sp)
746 * Returns the name of the device node used to represent
747 * the subpartition, either by serial number or traditional style.
748 * Note that the storage used for the returned string is static,
749 * and the string is overwritten each time this function is called.
752 subpartition_get_device_name(const struct subpartition *sp)
754 static char tmp_dev_name[256];
756 if (sp->parent->parent->serno != NULL)
757 snprintf(tmp_dev_name, 256, "serno/%s.s%d%c",
758 sp->parent->parent->serno, sp->parent->number, sp->letter);
760 snprintf(tmp_dev_name, 256, "%ss%d%c",
761 sp->parent->parent->device, sp->parent->number, sp->letter);
762 return(tmp_dev_name);
766 subpartition_get_mountpoint(const struct subpartition *sp)
768 return(sp->mountpoint);
772 subpartition_get_letter(const struct subpartition *sp)
778 subpartition_get_fsize(const struct subpartition *sp)
784 subpartition_get_bsize(const struct subpartition *sp)
790 subpartition_get_capacity(const struct subpartition *sp)
792 return(sp->capacity);
796 subpartition_clr_encrypted(struct subpartition *sp)
802 subpartition_is_encrypted(const struct subpartition *sp)
804 return(sp->encrypted);
808 subpartition_is_swap(const struct subpartition *sp)
814 subpartition_is_softupdated(const struct subpartition *sp)
816 return(sp->softupdates);
819 subpartition_is_tmpfsbacked(const struct subpartition *sp)
821 return(sp->tmpfsbacked);
825 subpartition_count(const struct slice *s)
827 struct subpartition *sp = s->subpartition_head;
839 subpartitions_free(struct slice *s)
841 struct subpartition *sp = s->subpartition_head, *next;
845 free(sp->mountpoint);
846 AURA_FREE(sp, subpartition);
850 s->subpartition_head = NULL;
851 s->subpartition_tail = NULL;
855 measure_activated_swap(const struct i_fn_args *a)
862 if ((p = aura_popen("%s%s -k", "r", a->os_root, cmd_name(a, "SWAPINFO"))) == NULL)
864 while (fgets(line, 255, p) != NULL) {
865 if ((word = strtok(line, " \t")) == NULL)
867 if (strcmp(word, "Device") == 0)
869 if ((word = strtok(NULL, " \t")) == NULL)
879 measure_activated_swap_from_slice(const struct i_fn_args *a,
880 const struct disk *d, const struct slice *s)
887 if ((p = aura_popen("%s%s -k", "r", a->os_root, cmd_name(a, "SWAPINFO"))) == NULL)
890 asprintf(&dev, "/dev/%ss%d", d->device, s->number);
892 while (fgets(line, 255, p) != NULL) {
893 if ((word = strtok(line, " \t")) == NULL)
895 if (strcmp(word, "Device") == 0)
897 if (strstr(word, dev) != word)
899 if ((word = strtok(NULL, " \t")) == NULL)
910 measure_activated_swap_from_disk(const struct i_fn_args *a,
911 const struct disk *d)
916 for (s = d->slice_head; s != NULL; s = s->next)
917 swap += measure_activated_swap_from_slice(a, d, s);
923 swapoff_all(const struct i_fn_args *a)
927 if ((p = aura_popen("%s%s off; %s%s | %s%s \"^/dev\" | %s%s '{print $1;}' | %s%s %s%s", "r",
928 a->os_root, cmd_name(a, "DUMPON"),
929 a->os_root, cmd_name(a, "SWAPINFO"),
930 a->os_root, cmd_name(a, "GREP"),
931 a->os_root, cmd_name(a, "AWK"),
932 a->os_root, cmd_name(a, "XARGS"),
933 a->os_root, cmd_name(a, "SWAPOFF"))) != NULL)
940 remove_all_mappings(const struct i_fn_args *a)
944 if ((p = aura_popen("%s%s -1 /dev/mapper | %s%s -vw control | %s%s -n 1 %s%s luksClose", "r",
945 a->os_root, cmd_name(a, "LS"),
946 a->os_root, cmd_name(a, "GREP"),
947 a->os_root, cmd_name(a, "XARGS"),
948 a->os_root, cmd_name(a, "CRYPTSETUP"))) != NULL)