summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2010-09-08 11:00:00 +0100
committerRichard Jones <rjones@redhat.com>2010-09-08 11:00:00 +0100
commitd5c8d3b45b9646a4ae9d4ea033e1cc13a6e25c1f (patch)
tree5a98c63031a12a13647019e2eca5190ce473febf
parent574df371a0421b4b83386f43e5095ccea5069e5b (diff)
downloadlibguestfs-d5c8d3b45b9646a4ae9d4ea033e1cc13a6e25c1f.tar.gz
libguestfs-d5c8d3b45b9646a4ae9d4ea033e1cc13a6e25c1f.tar.xz
libguestfs-d5c8d3b45b9646a4ae9d4ea033e1cc13a6e25c1f.zip
fish: Add guestfish -N bootroot and -N bootrootlv for creating boot+root disks.
-rw-r--r--fish/Makefile.am1
-rw-r--r--fish/alloc.c6
-rw-r--r--fish/fish.h4
-rw-r--r--fish/prep_boot.c163
-rw-r--r--fish/prep_lv.c2
-rw-r--r--po/POTFILES.in1
-rwxr-xr-xsrc/generator.ml23
7 files changed, 195 insertions, 5 deletions
diff --git a/fish/Makefile.am b/fish/Makefile.am
index d5c1dab9..4c5d76f0 100644
--- a/fish/Makefile.am
+++ b/fish/Makefile.am
@@ -55,6 +55,7 @@ guestfish_SOURCES = \
prep_part.c \
prep_fs.c \
prep_lv.c \
+ prep_boot.c \
progress.c \
rc.c \
reopen.c \
diff --git a/fish/alloc.c b/fish/alloc.c
index 75337414..29d9845b 100644
--- a/fish/alloc.c
+++ b/fish/alloc.c
@@ -58,8 +58,6 @@ do_sparse (const char *cmd, int argc, char *argv[])
return 0;
}
-static int parse_size (const char *str, off_t *size_rtn);
-
/* This is the underlying allocation function. It's called from
* a few other places in guestfish.
*/
@@ -144,7 +142,7 @@ alloc_disk (const char *filename, const char *size_str, int add, int sparse)
return 0;
}
-static int
+int
parse_size (const char *str, off_t *size_rtn)
{
unsigned long long size;
@@ -154,7 +152,7 @@ parse_size (const char *str, off_t *size_rtn)
if (xerr != LONGINT_OK) {
fprintf (stderr,
_("%s: invalid integer parameter (%s returned %d)\n"),
- "alloc_disk", "xstrtoull", xerr);
+ "parse_size", "xstrtoull", xerr);
return -1;
}
diff --git a/fish/fish.h b/fish/fish.h
index 7c9c9555..ec1c82f3 100644
--- a/fish/fish.h
+++ b/fish/fish.h
@@ -92,6 +92,7 @@ extern int do_alloc (const char *cmd, int argc, char *argv[]);
extern int do_sparse (const char *cmd, int argc, char *argv[]);
extern int alloc_disk (const char *filename, const char *size,
int add, int sparse);
+extern int parse_size (const char *str, off_t *size_rtn);
/* in echo.c */
extern int do_echo (const char *cmd, int argc, char *argv[]);
@@ -129,6 +130,9 @@ extern void prepare_drive (const char *filename, prep_data *data,
const char *device);
extern void prep_error (prep_data *data, const char *filename, const char *fs, ...) __attribute__((noreturn, format (printf,3,4)));
+/* in prep_lv.c */
+extern int vg_lv_parse (const char *device, char **vg, char **lv);
+
/* in progress.c */
extern void reset_progress_bar (void);
extern void progress_callback (guestfs_h *g, void *data, int proc_nr, int serial, uint64_t position, uint64_t total);
diff --git a/fish/prep_boot.c b/fish/prep_boot.c
new file mode 100644
index 00000000..e80f1ee5
--- /dev/null
+++ b/fish/prep_boot.c
@@ -0,0 +1,163 @@
+/* guestfish - the filesystem interactive shell
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "fish.h"
+#include "prepopts.h"
+
+void
+prep_prelaunch_bootroot (const char *filename, prep_data *data)
+{
+ if (alloc_disk (filename, data->params[2], 0, 1) == -1)
+ prep_error (data, filename, _("failed to allocate disk"));
+}
+
+void
+prep_postlaunch_bootroot (const char *filename, prep_data *data, const char *device)
+{
+ off_t bootsize;
+ if (parse_size (data->params[3], &bootsize) == -1)
+ prep_error (data, filename, _("could not parse boot size"));
+
+ int sector = guestfs_blockdev_getss (g, device);
+ if (sector == -1)
+ prep_error (data, filename, _("failed to get sector size of disk: %s"),
+ guestfs_last_error (g));
+
+ if (guestfs_part_init (g, device, data->params[4]) == -1)
+ prep_error (data, filename, _("failed to partition disk: %s"),
+ guestfs_last_error (g));
+
+ off_t lastbootsect = 64 + bootsize/sector - 1;
+ if (guestfs_part_add (g, device, "primary", 64, lastbootsect) == -1)
+ prep_error (data, filename, _("failed to add boot partition: %s"),
+ guestfs_last_error (g));
+
+ if (guestfs_part_add (g, device, "primary", lastbootsect+1, -64) == -1)
+ prep_error (data, filename, _("failed to add root partition: %s"),
+ guestfs_last_error (g));
+
+ char *part;
+ if (asprintf (&part, "%s1", device) == -1) {
+ perror ("asprintf");
+ exit (EXIT_FAILURE);
+ }
+ if (guestfs_mkfs (g, data->params[0], part) == -1)
+ prep_error (data, filename, _("failed to create boot filesystem: %s"),
+ guestfs_last_error (g));
+ free (part);
+
+ if (asprintf (&part, "%s2", device) == -1) {
+ perror ("asprintf");
+ exit (EXIT_FAILURE);
+ }
+ if (guestfs_mkfs (g, data->params[1], part) == -1)
+ prep_error (data, filename, _("failed to create root filesystem: %s"),
+ guestfs_last_error (g));
+ free (part);
+}
+
+void
+prep_prelaunch_bootrootlv (const char *filename, prep_data *data)
+{
+ if (vg_lv_parse (data->params[0], NULL, NULL) == -1)
+ prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
+
+ if (alloc_disk (filename, data->params[3], 0, 1) == -1)
+ prep_error (data, filename, _("failed to allocate disk"));
+}
+
+void
+prep_postlaunch_bootrootlv (const char *filename, prep_data *data, const char *device)
+{
+ off_t bootsize;
+ if (parse_size (data->params[4], &bootsize) == -1)
+ prep_error (data, filename, _("could not parse boot size"));
+
+ int sector = guestfs_blockdev_getss (g, device);
+ if (sector == -1)
+ prep_error (data, filename, _("failed to get sector size of disk: %s"),
+ guestfs_last_error (g));
+
+ if (guestfs_part_init (g, device, data->params[5]) == -1)
+ prep_error (data, filename, _("failed to partition disk: %s"),
+ guestfs_last_error (g));
+
+ off_t lastbootsect = 64 + bootsize/sector - 1;
+ if (guestfs_part_add (g, device, "primary", 64, lastbootsect) == -1)
+ prep_error (data, filename, _("failed to add boot partition: %s"),
+ guestfs_last_error (g));
+
+ if (guestfs_part_add (g, device, "primary", lastbootsect+1, -64) == -1)
+ prep_error (data, filename, _("failed to add root partition: %s"),
+ guestfs_last_error (g));
+
+ char *vg;
+ char *lv;
+ if (vg_lv_parse (data->params[0], &vg, &lv) == -1)
+ prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
+
+ char *part;
+ if (asprintf (&part, "%s1", device) == -1) {
+ perror ("asprintf");
+ exit (EXIT_FAILURE);
+ }
+ if (guestfs_mkfs (g, data->params[1], part) == -1)
+ prep_error (data, filename, _("failed to create boot filesystem: %s"),
+ guestfs_last_error (g));
+ free (part);
+
+ if (asprintf (&part, "%s2", device) == -1) {
+ perror ("asprintf");
+ exit (EXIT_FAILURE);
+ }
+ if (guestfs_pvcreate (g, part) == -1)
+ prep_error (data, filename, _("failed to create PV: %s: %s"),
+ part, guestfs_last_error (g));
+
+ char *parts[] = { part, NULL };
+ if (guestfs_vgcreate (g, vg, parts) == -1)
+ prep_error (data, filename, _("failed to create VG: %s: %s"),
+ vg, guestfs_last_error (g));
+
+ /* Create the smallest possible LV, then resize it to fill
+ * all available space.
+ */
+ if (guestfs_lvcreate (g, lv, vg, 1) == -1)
+ prep_error (data, filename, _("failed to create LV: /dev/%s/%s: %s"),
+ vg, lv, guestfs_last_error (g));
+ if (guestfs_lvresize_free (g, data->params[0], 100) == -1)
+ prep_error (data, filename,
+ _("failed to resize LV to full size: %s: %s"),
+ data->params[0], guestfs_last_error (g));
+
+ if (guestfs_mkfs (g, data->params[2], data->params[0]) == -1)
+ prep_error (data, filename, _("failed to create root filesystem: %s"),
+ guestfs_last_error (g));
+
+ free (part);
+ free (vg);
+ free (lv);
+}
diff --git a/fish/prep_lv.c b/fish/prep_lv.c
index 1e726dbd..627ed0af 100644
--- a/fish/prep_lv.c
+++ b/fish/prep_lv.c
@@ -30,7 +30,7 @@
/* Split "/dev/VG/LV" into "VG" and "LV". This function should
* probably do more checks.
*/
-static int
+int
vg_lv_parse (const char *device, char **vg, char **lv)
{
if (STRPREFIX (device, "/dev/"))
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 9e0e49cf..8eab9f16 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -81,6 +81,7 @@ fish/lcd.c
fish/man.c
fish/more.c
fish/prep.c
+fish/prep_boot.c
fish/prep_disk.c
fish/prep_fs.c
fish/prep_lv.c
diff --git a/src/generator.ml b/src/generator.ml
index 5c2ffebe..03638deb 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -5677,6 +5677,29 @@ let prepopts = [
creating a 100MB disk with the VG and LV called /dev/VG/LV, with an
ext2 filesystem.");
+ ("bootroot",
+ "create a boot and root filesystem",
+ [ "bootfs", "ext2", "the type of filesystem to use for boot";
+ "rootfs", "ext2", "the type of filesystem to use for root";
+ "size", "100M", "the size of the disk image";
+ "bootsize", "32M", "the size of the boot filesystem";
+ "partition", "mbr", "partition table type" ],
+ " Create a disk with two partitions, for boot and root filesystem.
+ Format the two filesystems independently. There are several optional
+ parameters which control the exact layout and filesystem types.");
+
+ ("bootrootlv",
+ "create a boot and root filesystem using LVM",
+ [ "name", "/dev/VG/LV", "the name of the VG and LV for root";
+ "bootfs", "ext2", "the type of filesystem to use for boot";
+ "rootfs", "ext2", "the type of filesystem to use for root";
+ "size", "100M", "the size of the disk image";
+ "bootsize", "32M", "the size of the boot filesystem";
+ "partition", "mbr", "partition table type" ],
+ " This is the same as 'bootroot' but the root filesystem (only) is
+ placed on a logical volume, named by default '/dev/VG/LV'. There are
+ several optional parameters which control the exact layout.");
+
]
(* Used to memoize the result of pod2text. *)