xref: /freebsd/usr.sbin/bsdinstall/partedit/scripted.c (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
33 #include <ctype.h>
34 #include <libgeom.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "partedit.h"
40 
41 static struct gprovider *
42 provider_for_name(struct gmesh *mesh, const char *name)
43 {
44 	struct gclass *classp;
45 	struct gprovider *pp = NULL;
46 	struct ggeom *gp;
47 
48 	LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
49 		LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
50 			if (LIST_EMPTY(&gp->lg_provider))
51 				continue;
52 
53 			LIST_FOREACH(pp, &gp->lg_provider, lg_provider)
54 				if (strcmp(pp->lg_name, name) == 0)
55 					break;
56 
57 			if (pp != NULL) break;
58 		}
59 
60 		if (pp != NULL) break;
61 	}
62 
63 	return (pp);
64 }
65 
66 static int
67 part_config(char *disk, const char *scheme, char *config)
68 {
69 	char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL;
70 	struct gclass *classp;
71 	struct gmesh mesh;
72 	struct ggeom *gpart = NULL;
73 	int error;
74 
75 	if (scheme == NULL)
76 		scheme = default_scheme();
77 
78 	error = geom_gettree(&mesh);
79 	if (provider_for_name(&mesh, disk) == NULL) {
80 		fprintf(stderr, "GEOM provider %s not found\n", disk);
81 		geom_deletetree(&mesh);
82 		return (-1);
83 	}
84 
85 	/* Remove any existing partitioning and create new scheme */
86 	LIST_FOREACH(classp, &mesh.lg_class, lg_class)
87 		if (strcmp(classp->lg_name, "PART") == 0)
88 			break;
89 	if (classp != NULL) {
90 		LIST_FOREACH(gpart, &classp->lg_geom, lg_geom)
91 		if (strcmp(gpart->lg_name, disk) == 0)
92 			break;
93 	}
94 	if (gpart != NULL)
95 		gpart_destroy(gpart);
96 	gpart_partition(disk, scheme);
97 
98 	if (strcmp(scheme, "MBR") == 0) {
99 		struct gmesh submesh;
100 		geom_gettree(&submesh);
101 		gpart_create(provider_for_name(&submesh, disk),
102 		    "freebsd", NULL, NULL, &disk, 0);
103 		geom_deletetree(&submesh);
104 	} else {
105 		disk= strdup(disk);
106 	}
107 
108 	geom_deletetree(&mesh);
109 	error = geom_gettree(&mesh);
110 
111 	/* Create partitions */
112 	if (config == NULL) {
113 		wizard_makeparts(&mesh, disk, "ufs", 0);
114 		goto finished;
115 	}
116 
117 	while ((partition = strsep(&config, ",")) != NULL) {
118 		while ((ap = strsep(&partition, " \t\n")) != NULL) {
119 			if (*ap == '\0')
120 				continue;
121 			if (size == NULL)
122 				size = ap;
123 			else if (type == NULL)
124 				type = ap;
125 			else if (mount == NULL)
126 				mount = ap;
127 		}
128 		if (size == NULL)
129 			continue;
130 		if (strcmp(size, "auto") == 0)
131 			size = NULL;
132 		gpart_create(provider_for_name(&mesh, disk), type, size, mount,
133 		    NULL, 0);
134 		geom_deletetree(&mesh);
135 		error = geom_gettree(&mesh);
136 		size = type = mount = NULL;
137 	}
138 
139 finished:
140 	geom_deletetree(&mesh);
141 	free(disk);
142 
143 	return (0);
144 }
145 
146 static
147 int parse_disk_config(char *input)
148 {
149 	char *ap;
150 	char *disk = NULL, *scheme = NULL, *partconfig = NULL;
151 
152 	while (input != NULL && *input != 0) {
153 		if (isspace(*input)) {
154 			input++;
155 			continue;
156 		}
157 
158 		switch(*input) {
159 		case '{':
160 			input++;
161 			partconfig = strchr(input, '}');
162 			if (partconfig == NULL) {
163 				fprintf(stderr, "Malformed partition setup "
164 				    "string: %s\n", input);
165 				return (1);
166 			}
167 			*partconfig = '\0';
168 			ap = partconfig+1;
169 			partconfig = input;
170 			input = ap;
171 			break;
172 		default:
173 			if (disk == NULL)
174 				disk = strsep(&input, " \t\n");
175 			else if (scheme == NULL)
176 				scheme = strsep(&input, " \t\n");
177 			else {
178 				fprintf(stderr, "Unknown directive: %s\n",
179 				    strsep(&input, " \t\n"));
180 				return (1);
181 			}
182 		}
183 	} while (input != NULL && *input != 0);
184 
185 	if (disk == NULL || strcmp(disk, "DEFAULT") == 0) {
186 		struct gmesh mesh;
187 		geom_gettree(&mesh);
188 		disk = boot_disk_select(&mesh);
189 		geom_deletetree(&mesh);
190 	}
191 
192 	return (part_config(disk, scheme, partconfig));
193 }
194 
195 int
196 scripted_editor(int argc, const char **argv)
197 {
198 	char *token;
199 	int i, error = 0, len = 0;
200 
201 	for (i = 1; i < argc; i++)
202 		len += strlen(argv[i]) + 1;
203 	char inputbuf[len], *input = inputbuf;
204 	strcpy(input, argv[1]);
205 	for (i = 2; i < argc; i++) {
206 		strcat(input, " ");
207 		strcat(input, argv[i]);
208 	}
209 
210 	while ((token = strsep(&input, ";")) != NULL) {
211 		error = parse_disk_config(token);
212 		if (error != 0)
213 			return (error);
214 	}
215 
216 	return (0);
217 }
218 
219