xref: /freebsd/usr.sbin/bsdinstall/partedit/scripted.c (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <errno.h>
33 #include <libutil.h>
34 #include <inttypes.h>
35 
36 #include <libgeom.h>
37 #include <dialog.h>
38 #include <dlg_keys.h>
39 
40 #include "partedit.h"
41 
42 static struct gprovider *
43 provider_for_name(struct gmesh *mesh, const char *name)
44 {
45 	struct gclass *classp;
46 	struct gprovider *pp = NULL;
47 	struct ggeom *gp;
48 
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))
52 				continue;
53 
54 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
55 				if (strcmp(pp->lg_name, name) == 0)
56 					break;
57 
58 			if (pp != NULL) break;
59 		}
60 
61 		if (pp != NULL) break;
62 	}
63 
64 	return (pp);
65 }
66 
67 static int
68 part_config(char *disk, const char *scheme, char *config)
69 {
70 	char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;
71 	struct gclass *classp;
72 	struct gmesh mesh;
73 	struct ggeom *gpart = NULL;
74 	int error;
75 
76 	if (scheme == NULL)
77 		scheme = default_scheme();
78 
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);
83 		return (-1);
84 	}
85 
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)
89 			break;
90 	if (classp != NULL) {
91 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
92 		if (strcmp(gpart->lg_name, disk) == 0)
93 			break;
94 	}
95 	if (gpart != NULL)
96 		gpart_destroy(gpart);
97 	gpart_partition(disk, scheme);
98 
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);
105 	} else {
106 		disk= strdup(disk);
107 	}
108 
109 	geom_deletetree(&mesh);
110 	error = geom_gettree(&mesh);
111 
112 	/* Create partitions */
113 	if (config == NULL) {
114 		wizard_makeparts(&mesh, disk, "ufs", 0);
115 		goto finished;
116 	}
117 
118 	while ((partition = strsep(&config, ",")) != NULL) {
119 		while ((ap = strsep(&partition, " \t\n")) != NULL) {
120 			if (*ap == '\0')
121 				continue;
122 			if (size == NULL)
123 				size = ap;
124 			else if (type == NULL)
125 				type = ap;
126 			else if (mount == NULL)
127 				mount = ap;
128 		}
129 		if (size == NULL)
130 			continue;
131 		if (strcmp(size, "auto") == 0)
132 			size = NULL;
133 		gpart_create(provider_for_name(&mesh, disk), type, size, mount,
134 		    NULL, 0);
135 		geom_deletetree(&mesh);
136 		error = geom_gettree(&mesh);
137 		size = type = mount = NULL;
138 	}
139 
140 finished:
141 	geom_deletetree(&mesh);
142 	free(disk);
143 
144 	return (0);
145 }
146 
147 static
148 int parse_disk_config(char *input)
149 {
150 	char *ap;
151 	char *disk = NULL, *scheme = NULL, *partconfig = NULL;
152 
153 	while (input != NULL && *input != 0) {
154 		if (isspace(*input)) {
155 			input++;
156 			continue;
157 		}
158 
159 		switch(*input) {
160 		case '{':
161 			input++;
162 			partconfig = strchr(input, '}');
163 			if (partconfig == NULL) {
164 				fprintf(stderr, "Malformed partition setup "
165 				    "string: %s\n", input);
166 				return (1);
167 			}
168 			*partconfig = '\0';
169 			ap = partconfig+1;
170 			partconfig = input;
171 			input = ap;
172 			break;
173 		default:
174 			if (disk == NULL)
175 				disk = strsep(&input, " \t\n");
176 			else if (scheme == NULL)
177 				scheme = strsep(&input, " \t\n");
178 			else {
179 				fprintf(stderr, "Unknown directive: %s\n",
180 				    strsep(&input, " \t\n"));
181 				return (1);
182 			}
183 		}
184 	} while (input != NULL && *input != 0);
185 
186 	if (disk == NULL || strcmp(disk, "DEFAULT") == 0) {
187 		struct gmesh mesh;
188 		geom_gettree(&mesh);
189 		disk = boot_disk_select(&mesh);
190 		geom_deletetree(&mesh);
191 	}
192 
193 	return (part_config(disk, scheme, partconfig));
194 }
195 
196 int
197 scripted_editor(int argc, const char **argv)
198 {
199 	char *token;
200 	int i, error = 0, len = 0;
201 
202 	for (i = 1; i < argc; i++)
203 		len += strlen(argv[i]) + 1;
204 	char inputbuf[len], *input = inputbuf;
205 	strcpy(input, argv[1]);
206 	for (i = 2; i < argc; i++) {
207 		strcat(input, " ");
208 		strcat(input, argv[i]);
209 	}
210 
211 	while ((token = strsep(&input, ";")) != NULL) {
212 		error = parse_disk_config(token);
213 		if (error != 0)
214 			return (error);
215 	}
216 
217 	return (0);
218 }
219 
220