1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 static prep_data *parse_type_string (const char *type_string);
33 list_prepared_drives (void)
37 printf (_("List of available prepared disk images:\n\n"));
39 for (i = 0; i < NR_PREPS; ++i) {
41 guestfish -N %-8s - %s\n\
44 preps[i].name, preps[i].shortdesc, preps[i].longdesc);
46 if (preps[i].nr_params > 0) {
48 printf (_(" Optional parameters:\n"));
49 printf (" -N %s", preps[i].name);
50 for (j = 0; j < preps[i].nr_params; ++j)
51 printf (":<%s>", preps[i].params[j].pname);
53 for (j = 0; j < preps[i].nr_params; ++j) {
55 printf (_("<%s> %s (default: %s)\n"),
56 preps[i].params[j].pname,
57 preps[i].params[j].pdesc,
58 preps[i].params[j].pdefault);
66 Prepared disk images are written to file \"test1.img\" in the local\n\
67 directory. (\"test2.img\" etc if -N option is given multiple times).\n\
68 For more information see the guestfish(1) manual.\n"));
71 /* Parse the type string (from the command line) and create the output
72 * file 'filename'. This is called before launch. Return the opaque
73 * prep_data which will be passed back to us in prepare_drive below.
76 create_prepared_file (const char *type_string, const char *filename)
78 prep_data *data = parse_type_string (type_string);
79 if (data->prep->prelaunch)
80 data->prep->prelaunch (filename, data);
85 parse_type_string (const char *type_string)
89 /* Match on the type part (without parameters). */
90 size_t len = strcspn (type_string, ":");
91 for (i = 0; i < NR_PREPS; ++i)
92 if (STRCASEEQLEN (type_string, preps[i].name, len))
97 guestfish: -N parameter '%s': no such prepared disk image known.\n\
98 Use 'guestfish -N help' to list possible values for the -N parameter.\n"),
103 prep_data *data = malloc (sizeof *data);
108 data->prep = &preps[i];
109 data->orig_type_string = type_string;
111 /* Set up the optional parameters to all-defaults. */
112 data->params = malloc (data->prep->nr_params * sizeof (char *));
113 if (data->params == NULL) {
118 for (i = 0; i < data->prep->nr_params; ++i)
119 data->params[i] = bad_cast (data->prep->params[i].pdefault);
121 /* Parse the optional parameters. */
122 const char *p = type_string + len;
123 if (*p) p++; /* skip colon char */
127 len = strcspn (p, ":");
128 data->params[i] = strndup (p, len);
129 if (data->params[i] == NULL) {
135 if (*p) p++; /* skip colon char */
142 /* Prepare a drive. The appliance has been launched, and 'device' is
143 * the libguestfs device. 'data' is the requested type. 'filename'
144 * is just used for error messages.
147 prepare_drive (const char *filename, prep_data *data,
150 if (data->prep->postlaunch)
151 data->prep->postlaunch (filename, data, device);
155 prep_error (prep_data *data, const char *filename, const char *fs, ...)
158 _("guestfish: error creating prepared disk image '%s' on '%s': "),
159 data->orig_type_string, filename);
163 vfprintf (stderr, fs, args);
166 fprintf (stderr, "\n");
172 free_prep_data (void *vp)
174 prep_data *data = vp;
177 for (i = 0; i < data->prep->nr_params; ++i)
178 if (data->params[i] != data->prep->params[i].pdefault)
179 free (data->params[i]);