fish: Document test1.img, test2.img etc used by -N option in FILES section.
[libguestfs.git] / fish / prep.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "fish.h"
28
29 static prep_data *parse_type_string (const char *type_string);
30 static void prep_error (prep_data *data, const char *filename, const char *fs, ...) __attribute__((noreturn, format (printf,3,4)));
31
32 struct prep {
33   const char *name;             /* eg. "fs" */
34
35   size_t nr_params;             /* optional parameters */
36   struct param *params;
37
38   const char *shortdesc;        /* short description */
39   const char *longdesc;         /* long description */
40
41                                 /* functions to implement it */
42   void (*prelaunch) (const char *filename, prep_data *);
43   void (*postlaunch) (const char *filename, prep_data *, const char *device);
44 };
45
46 struct param {
47   const char *pname;            /* parameter name */
48   const char *pdefault;         /* parameter default */
49   const char *pdesc;            /* parameter description */
50 };
51
52 static void prelaunch_disk (const char *filename, prep_data *data);
53 static struct param disk_params[] = {
54   { "size", "100M", "the size of the disk image" },
55 };
56
57 static void prelaunch_part (const char *filename, prep_data *data);
58 static void postlaunch_part (const char *filename, prep_data *data, const char *device);
59 static struct param part_params[] = {
60   { "size", "100M", "the size of the disk image" },
61   { "partition", "mbr", "partition table type" },
62 };
63
64 static void prelaunch_fs (const char *filename, prep_data *data);
65 static void postlaunch_fs (const char *filename, prep_data *data, const char *device);
66 static struct param fs_params[] = {
67   { "filesystem", "ext2", "the type of filesystem to use" },
68   { "size", "100M", "the size of the disk image" },
69   { "partition", "mbr", "partition table type" },
70 };
71
72 static const struct prep preps[] = {
73   { "disk",
74     1, disk_params,
75     "create a blank disk",
76     "\
77   Create a blank disk, size 100MB (by default).\n\
78 \n\
79   The default size can be changed by supplying an optional parameter.",
80     prelaunch_disk, NULL
81   },
82   { "part",
83     2, part_params,
84     "create a partitioned disk",
85     "\
86   Create a disk with a single partition.  By default the size of the disk\n\
87   is 100MB (the available space in the partition will be a tiny bit smaller)\n\
88   and the partition table will be MBR (old DOS-style).\n\
89 \n\
90   These defaults can be changed by supplying optional parameters.",
91     prelaunch_part, postlaunch_part
92   },
93   { "fs",
94     3, fs_params,
95     "create a filesystem",
96     "\
97   Create a disk with a single partition, with the partition containing\n\
98   an empty filesystem.  This defaults to creating a 100MB disk (the available\n\
99   space in the filesystem will be a tiny bit smaller) with an MBR (old\n\
100   DOS-style) partition table and an ext2 filesystem.\n\
101 \n\
102   These defaults can be changed by supplying optional parameters.",
103     prelaunch_fs, postlaunch_fs
104   },
105 };
106
107 #define nr_preps (sizeof preps / sizeof preps[0])
108
109 void
110 list_prepared_drives (void)
111 {
112   size_t i, j;
113
114   printf (_("List of available prepared disk images:\n\n"));
115
116   for (i = 0; i < nr_preps; ++i) {
117     printf (_("\
118 guestfish -N %-16s %s\n\
119 \n\
120 %s\n"),
121             preps[i].name, preps[i].shortdesc, preps[i].longdesc);
122
123     if (preps[i].nr_params > 0) {
124       printf ("\n");
125       printf (_("  Optional parameters:\n"));
126       printf ("    -N %s", preps[i].name);
127       for (j = 0; j < preps[i].nr_params; ++j)
128         printf (":<%s>", preps[i].params[j].pname);
129       printf ("\n");
130       for (j = 0; j < preps[i].nr_params; ++j) {
131         printf ("      ");
132         printf (_("<%s> %s (default: %s)\n"),
133                 preps[i].params[j].pname,
134                 preps[i].params[j].pdesc,
135                 preps[i].params[j].pdefault);
136       }
137     }
138
139     printf ("\n");
140   }
141
142   printf (_("\
143 Prepared disk images are written to file \"test1.img\" in the local\n\
144 directory.  (\"test2.img\" etc if -N option is given multiple times).\n\
145 For more information see the guestfish(1) manual.\n"));
146 }
147
148 struct prep_data {
149   const struct prep *prep;
150   const char *orig_type_string;
151   const char **params;
152 };
153
154 /* Parse the type string (from the command line) and create the output
155  * file 'filename'.  This is called before launch.  Return the opaque
156  * prep_data which will be passed back to us in prepare_drive below.
157  */
158 prep_data *
159 create_prepared_file (const char *type_string, const char *filename)
160 {
161   prep_data *data = parse_type_string (type_string);
162   if (data->prep->prelaunch)
163     data->prep->prelaunch (filename, data);
164   return data;
165 }
166
167 static prep_data *
168 parse_type_string (const char *type_string)
169 {
170   size_t i;
171
172   /* Match on the type part (without parameters). */
173   size_t len = strcspn (type_string, ":");
174   for (i = 0; i < nr_preps; ++i)
175     if (STRCASEEQLEN (type_string, preps[i].name, len))
176       break;
177
178   if (i == nr_preps) {
179     fprintf (stderr, _("\
180 guestfish: -N parameter '%s': no such prepared disk image known.\n\
181 Use 'guestfish -N list' to list possible values for the -N parameter.\n"),
182              type_string);
183     exit (EXIT_FAILURE);
184   }
185
186   prep_data *data = malloc (sizeof *data);
187   if (data == NULL) {
188     perror ("malloc");
189     exit (EXIT_FAILURE);
190   }
191   data->prep = &preps[i];
192   data->orig_type_string = type_string;
193
194   /* Set up the optional parameters to all-defaults. */
195   data->params = malloc (data->prep->nr_params * sizeof (char *));
196   if (data->params == NULL) {
197     perror ("malloc");
198     exit (EXIT_FAILURE);
199   }
200
201   for (i = 0; i < data->prep->nr_params; ++i)
202     data->params[i] = data->prep->params[i].pdefault;
203
204   /* Parse the optional parameters. */
205   const char *p = type_string + len;
206   if (*p) p++; /* skip colon char */
207
208   i = 0;
209   while (*p) {
210     len = strcspn (p, ":");
211     data->params[i] = strndup (p, len);
212     if (data->params[i] == NULL) {
213       perror ("strndup");
214       exit (EXIT_FAILURE);
215     }
216
217     p += len;
218     if (*p) p++; /* skip colon char */
219     i++;
220   }
221
222   return data;
223 }
224
225 /* Prepare a drive.  The appliance has been launched, and 'device' is
226  * the libguestfs device.  'data' is the requested type.  'filename'
227  * is just used for error messages.
228  */
229 void
230 prepare_drive (const char *filename, prep_data *data,
231                const char *device)
232 {
233   if (data->prep->postlaunch)
234     data->prep->postlaunch (filename, data, device);
235 }
236
237 static void
238 prep_error (prep_data *data, const char *filename, const char *fs, ...)
239 {
240   fprintf (stderr,
241            _("guestfish: error creating prepared disk image '%s' on '%s': "),
242            data->orig_type_string, filename);
243
244   va_list args;
245   va_start (args, fs);
246   vfprintf (stderr, fs, args);
247   va_end (args);
248
249   fprintf (stderr, "\n");
250
251   exit (EXIT_FAILURE);
252 }
253
254 static void
255 prelaunch_disk (const char *filename, prep_data *data)
256 {
257   if (alloc_disk (filename, data->params[0], 0, 1) == -1)
258     prep_error (data, filename, _("failed to allocate disk"));
259 }
260
261 static void
262 prelaunch_part (const char *filename, prep_data *data)
263 {
264   if (alloc_disk (filename, data->params[0], 0, 1) == -1)
265     prep_error (data, filename, _("failed to allocate disk"));
266 }
267
268 static void
269 postlaunch_part (const char *filename, prep_data *data, const char *device)
270 {
271   if (guestfs_part_disk (g, device, data->params[1]) == -1)
272     prep_error (data, filename, _("failed to partition disk: %s"),
273                 guestfs_last_error (g));
274 }
275
276 static void
277 prelaunch_fs (const char *filename, prep_data *data)
278 {
279   if (alloc_disk (filename, data->params[1], 0, 1) == -1)
280     prep_error (data, filename, _("failed to allocate disk"));
281 }
282
283 static void
284 postlaunch_fs (const char *filename, prep_data *data, const char *device)
285 {
286   if (guestfs_part_disk (g, device, data->params[2]) == -1)
287     prep_error (data, filename, _("failed to partition disk: %s"),
288                 guestfs_last_error (g));
289
290   char *part;
291   if (asprintf (&part, "%s1", device) == -1) {
292     perror ("asprintf");
293     exit (EXIT_FAILURE);
294   }
295
296   if (guestfs_mkfs (g, data->params[0], part) == -1)
297     prep_error (data, filename, _("failed to create filesystem (%s): %s"),
298                 data->params[0], guestfs_last_error (g));
299
300   free (part);
301 }