From: Richard Jones Date: Wed, 8 Sep 2010 10:00:00 +0000 (+0100) Subject: fish: Add guestfish -N bootroot and -N bootrootlv for creating boot+root disks. X-Git-Tag: 1.5.9~1 X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=commitdiff_plain;h=d5c8d3b45b9646a4ae9d4ea033e1cc13a6e25c1f fish: Add guestfish -N bootroot and -N bootrootlv for creating boot+root disks. --- diff --git a/fish/Makefile.am b/fish/Makefile.am index d5c1dab..4c5d76f 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 7533741..29d9845 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 7c9c955..ec1c82f 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 0000000..e80f1ee --- /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 + +#include +#include +#include +#include +#include + +#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 1e726db..627ed0a 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 9e0e49c..8eab9f1 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 5c2ffeb..03638de 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. *)