2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
42 static struct gprovider *
43 provider_for_name(struct gmesh *mesh, const char *name)
45 struct gclass *classp;
46 struct gprovider *pp = NULL;
49 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
50 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
51 if (LIST_EMPTY(&gp->lg_provider))
54 LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
55 if (strcmp(pp->lg_name, name) == 0)
58 if (pp != NULL) break;
61 if (pp != NULL) break;
68 part_config(char *disk, const char *scheme, char *config)
70 char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;
71 struct gclass *classp;
73 struct ggeom *gpart = NULL;
77 scheme = default_scheme();
79 error = geom_gettree(&mesh);
80 if (provider_for_name(&mesh, disk) == NULL) {
81 fprintf(stderr, "GEOM provider %s not found\n", disk);
82 geom_deletetree(&mesh);
86 /* Remove any existing partitioning and create new scheme */
87 LIST_FOREACH(classp, &mesh.lg_class, lg_class)
88 if (strcmp(classp->lg_name, "PART") == 0)
91 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
92 if (strcmp(gpart->lg_name, disk) == 0)
97 gpart_partition(disk, scheme);
99 if (strcmp(scheme, "MBR") == 0) {
100 struct gmesh submesh;
101 geom_gettree(&submesh);
102 gpart_create(provider_for_name(&submesh, disk),
103 "freebsd", NULL, NULL, &disk, 0);
104 geom_deletetree(&submesh);
109 geom_deletetree(&mesh);
110 error = geom_gettree(&mesh);
112 /* Create partitions */
113 if (config == NULL) {
114 wizard_makeparts(&mesh, disk, "ufs", 0);
118 while ((partition = strsep(&config, ",")) != NULL) {
119 while ((ap = strsep(&partition, " \t\n")) != NULL) {
124 else if (type == NULL)
126 else if (mount == NULL)
131 if (strcmp(size, "auto") == 0)
133 gpart_create(provider_for_name(&mesh, disk), type, size, mount,
135 geom_deletetree(&mesh);
136 error = geom_gettree(&mesh);
137 size = type = mount = NULL;
141 geom_deletetree(&mesh);
148 int parse_disk_config(char *input)
151 char *disk = NULL, *scheme = NULL, *partconfig = NULL;
153 while (input != NULL && *input != 0) {
154 if (isspace(*input)) {
162 partconfig = strchr(input, '}');
163 if (partconfig == NULL) {
164 fprintf(stderr, "Malformed partition setup "
165 "string: %s\n", input);
175 disk = strsep(&input, " \t\n");
176 else if (scheme == NULL)
177 scheme = strsep(&input, " \t\n");
179 fprintf(stderr, "Unknown directive: %s\n",
180 strsep(&input, " \t\n"));
184 } while (input != NULL && *input != 0);
187 return (part_config(disk, scheme, partconfig));
193 scripted_editor(int argc, const char **argv)
196 int i, error = 0, len = 0;
198 for (i = 1; i < argc; i++)
199 len += strlen(argv[i]) + 1;
200 char inputbuf[len], *input = inputbuf;
201 strcpy(input, argv[1]);
202 for (i = 2; i < argc; i++) {
204 strcat(input, argv[i]);
207 while ((token = strsep(&input, ";")) != NULL) {
208 error = parse_disk_config(token);