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 while ((partition = strsep(&config, ",")) != NULL) { 113 while ((ap = strsep(&partition, " \t\n")) != NULL) { 114 if (*ap == '\0') 115 continue; 116 if (size == NULL) 117 size = ap; 118 else if (type == NULL) 119 type = ap; 120 else if (mount == NULL) 121 mount = ap; 122 } 123 if (size == NULL) 124 continue; 125 if (strcmp(size, "auto") == 0) 126 size = NULL; 127 gpart_create(provider_for_name(&mesh, disk), type, size, mount, 128 NULL, 0); 129 geom_deletetree(&mesh); 130 error = geom_gettree(&mesh); 131 size = type = mount = NULL; 132 } 133 134 geom_deletetree(&mesh); 135 free(disk); 136 137 return (0); 138 } 139 140 static 141 int parse_disk_config(char *input) 142 { 143 char *ap; 144 char *disk = NULL, *scheme = NULL, *partconfig = NULL; 145 146 while (input != NULL && *input != 0) { 147 if (isspace(*input)) { 148 input++; 149 continue; 150 } 151 152 switch(*input) { 153 case '{': 154 input++; 155 partconfig = strchr(input, '}'); 156 if (partconfig == NULL) { 157 fprintf(stderr, "Malformed partition setup " 158 "string: %s\n", input); 159 return (1); 160 } 161 *partconfig = '\0'; 162 ap = partconfig+1; 163 partconfig = input; 164 input = ap; 165 break; 166 default: 167 if (disk == NULL) 168 disk = strsep(&input, " \t\n"); 169 else if (scheme == NULL) 170 scheme = strsep(&input, " \t\n"); 171 else { 172 fprintf(stderr, "Unknown directive: %s\n", 173 strsep(&input, " \t\n")); 174 return (1); 175 } 176 } 177 } while (input != NULL && *input != 0); 178 179 if (disk != NULL) 180 part_config(disk, scheme, partconfig); 181 182 return (0); 183 } 184 185 int 186 scripted_editor(int argc, const char **argv) 187 { 188 char *token; 189 int i, len = 0; 190 191 for (i = 1; i < argc; i++) 192 len += strlen(argv[i]) + 1; 193 char inputbuf[len], *input = inputbuf; 194 strcpy(input, argv[1]); 195 for (i = 2; i < argc; i++) { 196 strcat(input, " "); 197 strcat(input, argv[i]); 198 } 199 200 while ((token = strsep(&input, ";")) != NULL) 201 parse_disk_config(token); 202 203 return (0); 204 } 205 206