Path: blob/main/usr.sbin/bsdinstall/partedit/scripted.c
101998 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2013 Nathan Whitehorn4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>2930#include <ctype.h>31#include <libgeom.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>3536#include "partedit.h"3738static struct gprovider *39provider_for_name(struct gmesh *mesh, const char *name)40{41struct gclass *classp;42struct gprovider *pp = NULL;43struct ggeom *gp;4445LIST_FOREACH(classp, &mesh->lg_class, lg_class) {46LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {47if (LIST_EMPTY(&gp->lg_provider))48continue;4950LIST_FOREACH(pp, &gp->lg_provider, lg_provider)51if (strcmp(pp->lg_name, name) == 0)52break;5354if (pp != NULL) break;55}5657if (pp != NULL) break;58}5960return (pp);61}6263static int64part_config(char *disk, const char *scheme, char *config)65{66char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;67struct gclass *classp;68struct gmesh mesh;69struct ggeom *gpart = NULL;70int error;7172if (scheme == NULL)73scheme = default_scheme();7475error = geom_gettree(&mesh);76if (error != 0)77return (-1);78if (provider_for_name(&mesh, disk) == NULL) {79fprintf(stderr, "GEOM provider %s not found\n", disk);80geom_deletetree(&mesh);81return (-1);82}8384/* Remove any existing partitioning and create new scheme */85LIST_FOREACH(classp, &mesh.lg_class, lg_class)86if (strcmp(classp->lg_name, "PART") == 0)87break;88if (classp != NULL) {89LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)90if (strcmp(gpart->lg_name, disk) == 0)91break;92}93if (gpart != NULL)94gpart_destroy(gpart);95gpart_partition(disk, scheme);9697if (strcmp(scheme, "MBR") == 0) {98struct gmesh submesh;99100if (geom_gettree(&submesh) == 0) {101gpart_create(provider_for_name(&submesh, disk),102"freebsd", NULL, NULL, &disk, 0);103geom_deletetree(&submesh);104}105} else {106disk = strdup(disk);107}108109geom_deletetree(&mesh);110error = geom_gettree(&mesh);111if (error != 0) {112free(disk);113return (-1);114}115116/* Create partitions */117if (config == NULL) {118wizard_makeparts(&mesh, disk, "ufs", 0);119goto finished;120}121122while ((partition = strsep(&config, ",")) != NULL) {123while ((ap = strsep(&partition, " \t\n")) != NULL) {124if (*ap == '\0')125continue;126if (size == NULL)127size = ap;128else if (type == NULL)129type = ap;130else if (mount == NULL)131mount = ap;132}133if (size == NULL)134continue;135if (strcmp(size, "auto") == 0)136size = NULL;137gpart_create(provider_for_name(&mesh, disk), type, size, mount,138NULL, 0);139geom_deletetree(&mesh);140error = geom_gettree(&mesh);141if (error != 0) {142free(disk);143return (-1);144}145size = type = mount = NULL;146}147148finished:149geom_deletetree(&mesh);150free(disk);151152return (0);153}154155static int156parse_disk_config(char *input)157{158char *ap;159char *disk = NULL, *scheme = NULL, *partconfig = NULL;160161while (input != NULL && *input != 0) {162if (isspace(*input)) {163input++;164continue;165}166167switch(*input) {168case '{':169input++;170partconfig = strchr(input, '}');171if (partconfig == NULL) {172fprintf(stderr, "Malformed partition setup "173"string: %s\n", input);174return (1);175}176*partconfig = '\0';177ap = partconfig+1;178partconfig = input;179input = ap;180break;181default:182if (disk == NULL)183disk = strsep(&input, " \t\n");184else if (scheme == NULL)185scheme = strsep(&input, " \t\n");186else {187fprintf(stderr, "Unknown directive: %s\n",188strsep(&input, " \t\n"));189return (1);190}191}192} while (input != NULL && *input != 0);193194if (disk == NULL || strcmp(disk, "DEFAULT") == 0) {195struct gmesh mesh;196197if (geom_gettree(&mesh) == 0) {198disk = boot_disk_select(&mesh);199geom_deletetree(&mesh);200}201}202203return (part_config(disk, scheme, partconfig));204}205206int207scripted_editor(int argc, const char **argv)208{209FILE *fp;210char *input, *token;211size_t len;212int i, error = 0;213214fp = open_memstream(&input, &len);215fputs(argv[1], fp);216for (i = 2; i < argc; i++) {217fprintf(fp, " %s", argv[i]);218}219fclose(fp);220221while ((token = strsep(&input, ";")) != NULL) {222error = parse_disk_config(token);223if (error != 0) {224free(input);225return (error);226}227}228free(input);229230return (0);231}232233234235