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_mfs_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_mfsbacked(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);
168 d->cylinders = -1; /* -1 indicates "we don't know" */
172 d->slice_head = NULL;
173 d->slice_tail = NULL;
176 if (s->disk_head == NULL)
179 s->disk_tail->next = d;
181 d->prev = s->disk_tail;
188 disk_description_is_better(const char *existing, const char *new_desc __unused)
190 if (existing == NULL)
196 disk_get_desc(const struct disk *d)
202 disk_set_desc(struct disk *d, const char *desc)
206 if (!disk_description_is_better(d->desc, desc))
210 d->desc = aura_strdup(desc);
213 * Get the disk's total capacity.
214 * XXX we should do this with C/H/S ?
217 while (*c != ':' && *c != '\0')
222 d->capacity = atoi(c + 1);
226 * Returns the name of the device node used to represent the disk.
227 * Note that the storage used for the returned string is static,
228 * and the string is overwritten each time this function is called.
231 disk_get_device_name(const struct disk *d)
233 static char tmp_dev_name[256];
235 snprintf(tmp_dev_name, 256, "%s", d->device);
236 return(tmp_dev_name);
240 * Returns the name of the device node used to represent the
242 * Note that the storage used for the returned string is static,
243 * and the string is overwritten each time this function is called.
246 disk_get_raw_device_name(const struct disk *d)
248 static char tmp_dev_name[256];
250 snprintf(tmp_dev_name, 256, "%s", d->device);
251 return(tmp_dev_name);
255 * Find the first disk description structure in the given
256 * storage description which matches the given device name
257 * prefix. Note that this means that if a storage
258 * description s contains disks named "ad0" and "ad1",
259 * disk_find(s, "ad0s1c") will return a pointer to the disk
260 * structure for "ad0".
263 disk_find(const struct storage *s, const char *device)
265 struct disk *d = s->disk_head;
268 if (strncmp(device, d->device, strlen(d->device)) == 0)
277 disk_next(const struct disk *d)
283 disk_slice_first(const struct disk *d)
285 return(d->slice_head);
289 disk_set_formatted(struct disk *d, int formatted)
291 d->we_formatted = formatted;
295 disk_get_formatted(const struct disk *d)
297 return(d->we_formatted);
301 disk_set_geometry(struct disk *d, int cyl, int hd, int sec)
309 disk_get_geometry(const struct disk *d, int *cyl, int *hd, int *sec)
317 * Free the memory allocated to hold the set of disk descriptions.
320 disks_free(struct storage *s)
322 struct disk *d = s->disk_head, *next;
326 slices_free(d->slice_head);
338 * Create a new slice description and add it to a disk description.
341 slice_new(struct disk *d, int number, int type, int flags,
342 unsigned long start, unsigned long size)
345 const char *sysid_desc = NULL;
349 dfui_debug("** adding slice %d (start %ld, size %ld, sysid %d) "
350 "to disk %s\n", number, start, size, type, d->device);
352 AURA_MALLOC(s, slice);
356 s->subpartition_head = NULL;
357 s->subpartition_tail = NULL;
366 if (part_types[i].type == type) {
367 sysid_desc = part_types[i].name;
370 if (part_types[i].type == 255)
373 if (sysid_desc == NULL) {
374 snprintf(unknown, 256, "??? Unknown, sysid = %d", type);
375 sysid_desc = unknown;
378 asprintf(&s->desc, "%ldM - %ldM: %s",
379 start / 2048, (start + size) / 2048, sysid_desc);
380 s->capacity = size / 2048;
383 if (d->slice_head == NULL)
386 d->slice_tail->next = s;
388 s->prev = d->slice_tail;
395 * Find a slice description on a given disk description given the
399 slice_find(const struct disk *d, int number)
401 struct slice *s = d->slice_head;
404 if (s->number == number)
413 slice_next(const struct slice *s)
419 * Returns the name of the device node used to represent the slice.
420 * Note that the storage used for the returned string is static,
421 * and the string is overwritten each time this function is called.
424 slice_get_device_name(const struct slice *s)
426 static char tmp_dev_name[256];
428 snprintf(tmp_dev_name, 256, "%ss%d", s->parent->device, s->number);
429 return(tmp_dev_name);
433 * Returns the name of the device node used to represent
435 * Note that the storage used for the returned string is static,
436 * and the string is overwritten each time this function is called.
439 slice_get_raw_device_name(const struct slice *s)
441 static char tmp_dev_name[256];
443 snprintf(tmp_dev_name, 256, "%ss%d", s->parent->device, s->number);
444 return(tmp_dev_name);
448 slice_get_number(const struct slice *s)
454 slice_get_desc(const struct slice *s)
460 slice_get_capacity(const struct slice *s)
466 slice_get_start(const struct slice *s)
472 slice_get_size(const struct slice *s)
478 slice_get_type(const struct slice *s)
484 slice_get_flags(const struct slice *s)
489 struct subpartition *
490 slice_subpartition_first(const struct slice *s)
492 return(s->subpartition_head);
496 * Free all memory for a list of slice descriptions.
499 slices_free(struct slice *head)
503 while (head != NULL) {
505 subpartitions_free(head);
507 AURA_FREE(head, slice);
512 struct subpartition *
513 subpartition_new_hammer(struct slice *s, const char *mountpoint, long capacity)
515 struct subpartition *sp;
517 AURA_MALLOC(sp, subpartition);
521 struct subpartition *last = s->subpartition_tail;
524 } else if (last->letter == 'b') {
527 sp->letter = (char)(last->letter + 1);
530 sp->mountpoint = aura_strdup(mountpoint);
531 sp->capacity = capacity;
532 sp->type = FS_HAMMER;
535 * We need this here, because a UFS /boot needs valid values
537 if (sp->capacity < 1024)
542 if (sp->capacity < 1024)
549 if (strcasecmp(mountpoint, "swap") == 0)
551 if (strcmp(mountpoint, "/") != 0 && strcmp(mountpoint, "/boot") != 0 &&
552 strcmp(mountpoint, "swap") != 0)
556 if (s->subpartition_head == NULL)
557 s->subpartition_head = sp;
559 s->subpartition_tail->next = sp;
561 sp->prev = s->subpartition_tail;
562 s->subpartition_tail = sp;
568 * NOTE: arguments to this function are not checked for sanity.
570 * fsize and/or bsize may both be -1, indicating
571 * "choose a reasonable default."
573 struct subpartition *
574 subpartition_new(struct slice *s, const char *mountpoint, long capacity,
575 int softupdates, long fsize, long bsize, int mfsbacked)
577 struct subpartition *sp, *sptmp;
580 AURA_MALLOC(sp, subpartition);
584 sp->mountpoint = aura_strdup(mountpoint);
585 sp->capacity = capacity;
589 if (sp->capacity < 1024)
598 if (sp->capacity < 1024)
606 if (softupdates == -1) {
607 if (strcmp(mountpoint, "/") == 0)
612 sp->softupdates = softupdates;
615 sp->mfsbacked = mfsbacked;
618 if (strcasecmp(mountpoint, "swap") == 0)
621 if (s->subpartition_head == NULL) {
622 s->subpartition_head = sp;
623 s->subpartition_tail = sp;
625 for (sptmp = s->subpartition_head; sptmp != NULL;
626 sptmp = sptmp->next) {
627 if (strcmp(sptmp->mountpoint, sp->mountpoint) > 0)
631 if (s->subpartition_head == sptmp)
632 s->subpartition_head = sp;
634 sptmp->prev->next = sp;
636 sp->prev = sptmp->prev;
639 sp->prev = s->subpartition_tail;
640 s->subpartition_tail->next = sp;
641 s->subpartition_tail = sp;
645 for (sptmp = s->subpartition_head; sptmp != NULL;
646 sptmp = sptmp->next) {
647 if (sptmp->mfsbacked)
649 else if (strcmp(sptmp->mountpoint, "/") == 0 ||
650 strcmp(sptmp->mountpoint, "/dummy") == 0)
652 else if (strcasecmp(sptmp->mountpoint, "swap") == 0)
655 sptmp->letter = letter++;
662 * Find the subpartition description in the given storage
663 * description whose mountpoint matches the given string exactly.
665 struct subpartition *
666 subpartition_find(const struct slice *s, const char *fmt, ...)
668 struct subpartition *sp = s->subpartition_head;
673 vasprintf(&mountpoint, fmt, args);
677 if (strcmp(mountpoint, sp->mountpoint) == 0) {
689 * Find the subpartition description in the given storage
690 * description where the given filename would presumably
691 * reside. This is the subpartition whose mountpoint is
692 * the longest match for the given filename.
694 struct subpartition *
695 subpartition_of(const struct slice *s, const char *fmt, ...)
697 struct subpartition *sp = s->subpartition_head;
698 struct subpartition *csp = NULL;
704 vasprintf(&filename, fmt, args);
708 if (strlen(sp->mountpoint) > len &&
709 strlen(sp->mountpoint) <= strlen(filename) &&
710 strncmp(filename, sp->mountpoint, strlen(sp->mountpoint)) == 0) {
712 len = strlen(csp->mountpoint);
721 struct subpartition *
722 subpartition_find_capacity(const struct slice *s, long capacity)
724 struct subpartition *sp = s->subpartition_head;
727 if (sp->capacity == capacity)
735 struct subpartition *
736 subpartition_next(const struct subpartition *sp)
742 subpartition_get_pfs(const struct subpartition *sp)
748 * Returns the name of the device node used to represent
750 * Note that the storage used for the returned string is static,
751 * and the string is overwritten each time this function is called.
754 subpartition_get_device_name(const struct subpartition *sp)
756 static char tmp_dev_name[256];
758 snprintf(tmp_dev_name, 256, "%ss%d%c", sp->parent->parent->device,
759 sp->parent->number, sp->letter);
760 return(tmp_dev_name);
764 * Returns the name of the device node used to represent
765 * the raw subpartition.
766 * Note that the storage used for the returned string is static,
767 * and the string is overwritten each time this function is called.
770 subpartition_get_raw_device_name(const struct subpartition *sp)
772 static char tmp_dev_name[256];
774 snprintf(tmp_dev_name, 256, "r%ss%d%c", sp->parent->parent->device,
775 sp->parent->number, sp->letter);
776 return(tmp_dev_name);
780 subpartition_get_mountpoint(const struct subpartition *sp)
782 return(sp->mountpoint);
786 subpartition_get_letter(const struct subpartition *sp)
792 subpartition_get_fsize(const struct subpartition *sp)
798 subpartition_get_bsize(const struct subpartition *sp)
804 subpartition_get_capacity(const struct subpartition *sp)
806 return(sp->capacity);
810 subpartition_is_swap(const struct subpartition *sp)
816 subpartition_is_softupdated(const struct subpartition *sp)
818 return(sp->softupdates);
821 subpartition_is_mfsbacked(const struct subpartition *sp)
823 return(sp->mfsbacked);
827 subpartition_count(const struct slice *s)
829 struct subpartition *sp = s->subpartition_head;
841 subpartitions_free(struct slice *s)
843 struct subpartition *sp = s->subpartition_head, *next;
847 free(sp->mountpoint);
848 AURA_FREE(sp, subpartition);
852 s->subpartition_head = NULL;
853 s->subpartition_tail = NULL;
857 measure_activated_swap(const struct i_fn_args *a)
864 if ((p = aura_popen("%s%s -k", "r", a->os_root, cmd_name(a, "SWAPINFO"))) == NULL)
866 while (fgets(line, 255, p) != NULL) {
867 if ((word = strtok(line, " \t")) == NULL)
869 if (strcmp(word, "Device") == 0)
871 if ((word = strtok(NULL, " \t")) == NULL)
881 measure_activated_swap_from_slice(const struct i_fn_args *a,
882 const struct disk *d, const struct slice *s)
889 if ((p = aura_popen("%s%s -k", "r", a->os_root, cmd_name(a, "SWAPINFO"))) == NULL)
892 asprintf(&dev, "/dev/%ss%d", d->device, s->number);
894 while (fgets(line, 255, p) != NULL) {
895 if ((word = strtok(line, " \t")) == NULL)
897 if (strcmp(word, "Device") == 0)
899 if (strstr(word, dev) != word)
901 if ((word = strtok(NULL, " \t")) == NULL)
912 measure_activated_swap_from_disk(const struct i_fn_args *a,
913 const struct disk *d)
918 for (s = d->slice_head; s != NULL; s = s->next)
919 swap += measure_activated_swap_from_slice(a, d, s);