2 * Copyright (c) 2013 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
40 static struct gprovider *
41 provider_for_name(struct gmesh *mesh, const char *name)
43 struct gclass *classp;
44 struct gprovider *pp = NULL;
47 LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
48 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
49 if (LIST_EMPTY(&gp->lg_provider))
52 LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
53 if (strcmp(pp->lg_name, name) == 0)
56 if (pp != NULL) break;
59 if (pp != NULL) break;
66 part_config(char *disk, const char *scheme, char *config)
68 char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;
69 struct gclass *classp;
71 struct ggeom *gpart = NULL;
75 scheme = default_scheme();
77 error = geom_gettree(&mesh);
78 if (provider_for_name(&mesh, disk) == NULL) {
79 fprintf(stderr, "GEOM provider %s not found\n", disk);
80 geom_deletetree(&mesh);
84 /* Remove any existing partitioning and create new scheme */
85 LIST_FOREACH(classp, &mesh.lg_class, lg_class)
86 if (strcmp(classp->lg_name, "PART") == 0)
89 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
90 if (strcmp(gpart->lg_name, disk) == 0)
95 gpart_partition(disk, scheme);
97 if (strcmp(scheme, "PC98") == 0 || strcmp(scheme, "MBR") == 0) {
99 geom_gettree(&submesh);
100 gpart_create(provider_for_name(&submesh, disk),
101 "freebsd", NULL, NULL, &disk, 0);
102 geom_deletetree(&submesh);
107 geom_deletetree(&mesh);
108 error = geom_gettree(&mesh);
110 /* Create partitions */
111 if (config == NULL) {
112 wizard_makeparts(&mesh, disk, "ufs", 0);
116 while ((partition = strsep(&config, ",")) != NULL) {
117 while ((ap = strsep(&partition, " \t\n")) != NULL) {
122 else if (type == NULL)
124 else if (mount == NULL)
129 if (strcmp(size, "auto") == 0)
131 gpart_create(provider_for_name(&mesh, disk), type, size, mount,
133 geom_deletetree(&mesh);
134 error = geom_gettree(&mesh);
135 size = type = mount = NULL;
139 geom_deletetree(&mesh);
146 int parse_disk_config(char *input)
149 char *disk = NULL, *scheme = NULL, *partconfig = NULL;
151 while (input != NULL && *input != 0) {
152 if (isspace(*input)) {
160 partconfig = strchr(input, '}');
161 if (partconfig == NULL) {
162 fprintf(stderr, "Malformed partition setup "
163 "string: %s\n", input);
173 disk = strsep(&input, " \t\n");
174 else if (scheme == NULL)
175 scheme = strsep(&input, " \t\n");
177 fprintf(stderr, "Unknown directive: %s\n",
178 strsep(&input, " \t\n"));
182 } while (input != NULL && *input != 0);
185 return (part_config(disk, scheme, partconfig));
191 scripted_editor(int argc, const char **argv)
194 int i, error = 0, len = 0;
196 for (i = 1; i < argc; i++)
197 len += strlen(argv[i]) + 1;
198 char inputbuf[len], *input = inputbuf;
199 strcpy(input, argv[1]);
200 for (i = 2; i < argc; i++) {
202 strcat(input, argv[i]);
205 while ((token = strsep(&input, ";")) != NULL) {
206 error = parse_disk_config(token);