1 /*- 2 * Copyright (c) 2013 Nathan Whitehorn 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <errno.h> 31 #include <libutil.h> 32 #include <inttypes.h> 33 34 #include <libgeom.h> 35 #include <dialog.h> 36 #include <dlg_keys.h> 37 38 #include "partedit.h" 39 40 static struct gprovider * 41 provider_for_name(struct gmesh *mesh, const char *name) 42 { 43 struct gclass *classp; 44 struct gprovider *pp = NULL; 45 struct ggeom *gp; 46 47 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 48 if (strcmp(classp->lg_name, "DISK") != 0 && 49 strcmp(classp->lg_name, "PART") != 0 && 50 strcmp(classp->lg_name, "RAID") != 0 && 51 strcmp(classp->lg_name, "MD") != 0) 52 continue; 53 54 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 55 if (LIST_EMPTY(&gp->lg_provider)) 56 continue; 57 58 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) 59 if (strcmp(pp->lg_name, name) == 0) 60 break; 61 62 if (pp != NULL) break; 63 } 64 65 if (pp != NULL) break; 66 } 67 68 return (pp); 69 } 70 71 static int 72 part_config(char *disk, const char *scheme, char *config) 73 { 74 char *partition, *ap, *size = NULL, *type = NULL, *mount = NULL; 75 struct gclass *classp; 76 struct gmesh mesh; 77 struct ggeom *gpart = NULL; 78 int error; 79 80 if (scheme == NULL) 81 scheme = default_scheme(); 82 83 error = geom_gettree(&mesh); 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, "PC98") == 0 || 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, 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) 186 part_config(disk, scheme, partconfig); 187 188 return (0); 189 } 190 191 int 192 scripted_editor(int argc, const char **argv) 193 { 194 char *token; 195 int i, len = 0; 196 197 for (i = 1; i < argc; i++) 198 len += strlen(argv[i]) + 1; 199 char inputbuf[len], *input = inputbuf; 200 strcpy(input, argv[1]); 201 for (i = 2; i < argc; i++) { 202 strcat(input, " "); 203 strcat(input, argv[i]); 204 } 205 206 while ((token = strsep(&input, ";")) != NULL) 207 parse_disk_config(token); 208 209 return (0); 210 } 211 212