fish: Add guestfish -N lvfs for creating formatted LVs.
[libguestfs.git] / fish / prep_lv.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 #include "prepopts.h"
29
30 /* Split "/dev/VG/LV" into "VG" and "LV".  This function should
31  * probably do more checks.
32  */
33 static int
34 vg_lv_parse (const char *device, char **vg, char **lv)
35 {
36   if (STRPREFIX (device, "/dev/"))
37     device += 5;
38
39   const char *p = strchr (device, '/');
40   if (p == NULL)
41     return -1;
42
43   if (vg) {
44     *vg = strndup (device, p - device);
45     if (*vg == NULL) {
46       perror ("strndup");
47       exit (EXIT_FAILURE);
48     }
49   }
50
51   if (lv) {
52     *lv = strdup (p+1);
53     if (*lv == NULL) {
54       perror ("strndup");
55       exit (EXIT_FAILURE);
56     }
57   }
58
59   return 0;
60 }
61
62 void
63 prep_prelaunch_lv (const char *filename, prep_data *data)
64 {
65   if (vg_lv_parse (data->params[0], NULL, NULL) == -1)
66     prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
67
68   if (alloc_disk (filename, data->params[1], 0, 1) == -1)
69     prep_error (data, filename, _("failed to allocate disk"));
70 }
71
72 void
73 prep_postlaunch_lv (const char *filename, prep_data *data, const char *device)
74 {
75   if (guestfs_part_disk (g, device, data->params[2]) == -1)
76     prep_error (data, filename, _("failed to partition disk: %s"),
77                 guestfs_last_error (g));
78
79   char *vg;
80   char *lv;
81   if (vg_lv_parse (data->params[0], &vg, &lv) == -1)
82     prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
83
84   char *part;
85   if (asprintf (&part, "%s1", device) == -1) {
86     perror ("asprintf");
87     exit (EXIT_FAILURE);
88   }
89
90   if (guestfs_pvcreate (g, part) == -1)
91     prep_error (data, filename, _("failed to create PV: %s: %s"),
92                 part, guestfs_last_error (g));
93
94   char *parts[] = { part, NULL };
95   if (guestfs_vgcreate (g, vg, parts) == -1)
96     prep_error (data, filename, _("failed to create VG: %s: %s"),
97                 vg, guestfs_last_error (g));
98
99   /* Create the smallest possible LV, then resize it to fill
100    * all available space.
101    */
102   if (guestfs_lvcreate (g, lv, vg, 1) == -1)
103     prep_error (data, filename, _("failed to create LV: /dev/%s/%s: %s"),
104                 vg, lv, guestfs_last_error (g));
105   if (guestfs_lvresize_free (g, data->params[0], 100) == -1)
106     prep_error (data, filename,
107                 _("failed to resize LV to full size: %s: %s"),
108                 data->params[0], guestfs_last_error (g));
109
110   free (part);
111   free (vg);
112   free (lv);
113 }
114
115 void
116 prep_prelaunch_lvfs (const char *filename, prep_data *data)
117 {
118   if (vg_lv_parse (data->params[0], NULL, NULL) == -1)
119     prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
120
121   if (alloc_disk (filename, data->params[2], 0, 1) == -1)
122     prep_error (data, filename, _("failed to allocate disk"));
123 }
124
125 void
126 prep_postlaunch_lvfs (const char *filename, prep_data *data, const char *device)
127 {
128   if (guestfs_part_disk (g, device, data->params[3]) == -1)
129     prep_error (data, filename, _("failed to partition disk: %s"),
130                 guestfs_last_error (g));
131
132   char *vg;
133   char *lv;
134   if (vg_lv_parse (data->params[0], &vg, &lv) == -1)
135     prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
136
137   char *part;
138   if (asprintf (&part, "%s1", device) == -1) {
139     perror ("asprintf");
140     exit (EXIT_FAILURE);
141   }
142
143   if (guestfs_pvcreate (g, part) == -1)
144     prep_error (data, filename, _("failed to create PV: %s: %s"),
145                 part, guestfs_last_error (g));
146
147   char *parts[] = { part, NULL };
148   if (guestfs_vgcreate (g, vg, parts) == -1)
149     prep_error (data, filename, _("failed to create VG: %s: %s"),
150                 vg, guestfs_last_error (g));
151
152   /* Create the smallest possible LV, then resize it to fill
153    * all available space.
154    */
155   if (guestfs_lvcreate (g, lv, vg, 1) == -1)
156     prep_error (data, filename, _("failed to create LV: /dev/%s/%s: %s"),
157                 vg, lv, guestfs_last_error (g));
158   if (guestfs_lvresize_free (g, data->params[0], 100) == -1)
159     prep_error (data, filename,
160                 _("failed to resize LV to full size: %s: %s"),
161                 data->params[0], guestfs_last_error (g));
162
163   /* Create the filesystem. */
164   if (guestfs_mkfs (g, data->params[1], data->params[0]) == -1)
165     prep_error (data, filename, _("failed to create filesystem (%s): %s"),
166                 data->params[1], guestfs_last_error (g));
167
168   free (part);
169   free (vg);
170   free (lv);
171 }