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 /* Split "/dev/VG/LV" into "VG" and "LV". This function should
31 * probably do more checks.
34 vg_lv_parse (const char *device, char **vg, char **lv)
36 if (STRPREFIX (device, "/dev/"))
39 const char *p = strchr (device, '/');
44 *vg = strndup (device, p - device);
63 prep_prelaunch_lv (const char *filename, prep_data *data)
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'"));
68 if (alloc_disk (filename, data->params[1], 0, 1) == -1)
69 prep_error (data, filename, _("failed to allocate disk"));
73 prep_postlaunch_lv (const char *filename, prep_data *data, const char *device)
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));
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'"));
85 if (asprintf (&part, "%s1", device) == -1) {
90 if (guestfs_pvcreate (g, part) == -1)
91 prep_error (data, filename, _("failed to create PV: %s: %s"),
92 part, guestfs_last_error (g));
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));
99 /* Create the smallest possible LV, then resize it to fill
100 * all available space.
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));
116 prep_prelaunch_lvfs (const char *filename, prep_data *data)
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'"));
121 if (alloc_disk (filename, data->params[2], 0, 1) == -1)
122 prep_error (data, filename, _("failed to allocate disk"));
126 prep_postlaunch_lvfs (const char *filename, prep_data *data, const char *device)
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));
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'"));
138 if (asprintf (&part, "%s1", device) == -1) {
143 if (guestfs_pvcreate (g, part) == -1)
144 prep_error (data, filename, _("failed to create PV: %s: %s"),
145 part, guestfs_last_error (g));
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));
152 /* Create the smallest possible LV, then resize it to fill
153 * all available space.
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));
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));