Wanlong Gao
2012-Jul-21 07:27 UTC
[Libguestfs] [PATCH 1/5] mount: add a macro to resolve path or device
Add a macro DUP_RESOLVE_DEVICE_OR_PATH to resolve path or device. Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> --- daemon/daemon.h | 18 ++++++++++++++++++ daemon/mount.c | 13 ++----------- po/POTFILES | 8 ++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/daemon/daemon.h b/daemon/daemon.h index 85eec45..39cc3f3 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -336,6 +336,24 @@ is_zero (const char *buffer, size_t size) } \ } while (0) +/* Helper for functions which need either a root(/sysroot) path, + * OR a /dev/ device which exists. + */ +#define DUP_RESOLVE_DEVICE_OR_PATH(path,buf,cancel_stmt,fail_stmt) \ + do { \ + int is_dev; \ + is_dev = STREQLEN (path, "/dev/", 5); \ + buf = is_dev ? strdup (path) \ + : sysroot_path (path); \ + if (buf == NULL) { \ + cancel_stmt; \ + reply_with_perror ("malloc"); \ + fail_stmt; \ + } \ + if (is_dev) \ + RESOLVE_DEVICE (buf, cancel_stmt, fail_stmt); \ + } while (0) + /* NB: * (1) You must match CHROOT_IN and CHROOT_OUT even along error paths. * (2) You must not change directory! cwd must always be "/", otherwise diff --git a/daemon/mount.c b/daemon/mount.c index 0661eb8..1bac1a7 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -192,18 +192,9 @@ do_umount (const char *pathordevice) int r; char *err; char *buf; - int is_dev; - is_dev = STREQLEN (pathordevice, "/dev/", 5); - buf = is_dev ? strdup (pathordevice) - : sysroot_path (pathordevice); - if (buf == NULL) { - reply_with_perror ("malloc"); - return -1; - } - - if (is_dev) - RESOLVE_DEVICE (buf, , { free (buf); return -1; }); + DUP_RESOLVE_DEVICE_OR_PATH (pathordevice, buf, , + { free (buf); return -1;}); r = command (NULL, &err, "umount", buf, NULL); free (buf); diff --git a/po/POTFILES b/po/POTFILES index 60f8d95..69291fa 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -27,6 +27,7 @@ daemon/du.c daemon/echo-daemon.c daemon/errnostring-gperf.c daemon/errnostring.c +daemon/errnostring_gperf.c daemon/ext2.c daemon/fallocate.c daemon/file.c @@ -97,6 +98,7 @@ erlang/erl-guestfs.c fish/alloc.c fish/cmds-gperf.c fish/cmds.c +fish/cmds_gperf.c fish/completion.c fish/config.c fish/copy.c @@ -135,6 +137,7 @@ format/format.c fuse/guestmount.c gobject/src/optargs-add_domain.c gobject/src/optargs-add_drive.c +gobject/src/optargs-add_drive_opts.c gobject/src/optargs-btrfs_filesystem_resize.c gobject/src/optargs-btrfs_fsck.c gobject/src/optargs-compress_device_out.c @@ -150,12 +153,15 @@ gobject/src/optargs-internal_test.c gobject/src/optargs-md_create.c gobject/src/optargs-mkfs.c gobject/src/optargs-mkfs_btrfs.c +gobject/src/optargs-mkfs_opts.c gobject/src/optargs-mount_9p.c gobject/src/optargs-mount_local.c gobject/src/optargs-ntfsclone_out.c gobject/src/optargs-ntfsfix.c gobject/src/optargs-ntfsresize.c +gobject/src/optargs-ntfsresize_opts.c gobject/src/optargs-set_e2attrs.c +gobject/src/optargs-test0.c gobject/src/optargs-tune2fs.c gobject/src/optargs-umount_local.c gobject/src/session.c @@ -180,6 +186,7 @@ inspector/virt-inspector.c java/com_redhat_et_libguestfs_GuestFS.c ocaml/guestfs-c-actions.c ocaml/guestfs-c.c +ocaml/guestfs_c_actions.c perl/Guestfs.c perl/bindtests.pl perl/lib/Sys/Guestfs.pm @@ -197,6 +204,7 @@ src/bindtests.c src/dbdump.c src/errnostring-gperf.c src/errnostring.c +src/errnostring_gperf.c src/events.c src/filearch.c src/fuse.c -- 1.7.11.2.249.g31c7954
Wanlong Gao
2012-Jul-21 07:27 UTC
[Libguestfs] [PATCH 2/5] xfs: use the added macro DUP_RESOLVE_DEVICE_OR_PATH
Use this macro to simplify the code. Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> --- daemon/xfs.c | 17 +++-------------- generator/generator_actions.ml | 6 +++--- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/daemon/xfs.c b/daemon/xfs.c index 3efc14f..5d267e6 100644 --- a/daemon/xfs.c +++ b/daemon/xfs.c @@ -311,7 +311,7 @@ error: } guestfs_int_xfsinfo * -do_xfs_info (const char *path) +do_xfs_info (const char *pathordevice) { int r; char *buf; @@ -319,19 +319,8 @@ do_xfs_info (const char *path) char **lines = NULL; guestfs_int_xfsinfo *ret = NULL; - if (do_is_dir (path)) { - buf = sysroot_path (path); - if (!buf) { - reply_with_perror ("malloc"); - return NULL; - } - } else { - buf = strdup(path); - if (!buf) { - reply_with_perror ("strdup"); - return NULL; - } - } + DUP_RESOLVE_DEVICE_OR_PATH (pathordevice, buf, , + {free (buf); return NULL;}); r = command (&out, &err, "xfs_info", buf, NULL); free (buf); diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index c25bda1..1ce4026 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -8923,7 +8923,7 @@ call C<guestfs_max_disks>." }; { defaults with name = "xfs_info"; - style = RStruct ("info", "xfsinfo"), [Pathname "path"], []; + style = RStruct ("info", "xfsinfo"), [String "pathordevice"], []; proc_nr = Some 337; optional = Some "xfs"; tests = [ @@ -8937,8 +8937,8 @@ call C<guestfs_max_disks>." }; ]; shortdesc = "get geometry of XFS filesystem"; longdesc = "\ -C<path> is a mounted XFS filesystem. This command returns the -geometry of the filesystem. +C<pathordevice> is a mounted XFS filesystem. This command returns +the geometry of the filesystem. The returned struct contains geometry information. Missing fields are returned as C<-1> (for numeric fields) or empty -- 1.7.11.2.249.g31c7954
Wanlong Gao
2012-Jul-21 07:27 UTC
[Libguestfs] [PATCH 3/5] umount: add force umount and lazy umount
Add the option force and lazy for force and lazy umount. Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> --- daemon/mount.c | 25 +++++++++++++++++++++++-- generator/generator_actions.ml | 22 +++++++++++----------- gobject/Makefile.inc | 2 ++ po/POTFILES | 1 + 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/daemon/mount.c b/daemon/mount.c index 1bac1a7..0b91433 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -29,6 +29,8 @@ #include "daemon.h" #include "actions.h" +#define MAX_ARGS 64 + /* You must mount something on "/" first before many operations. * Hence we have an internal function which can test if something is * mounted on *or under* the sysroot directory. (It has to be *or @@ -187,16 +189,35 @@ do_mount_options (const char *options, const char *device, * is kept updated. */ int -do_umount (const char *pathordevice) +do_umount (const char *pathordevice, + int force, int lazy) { int r; char *err; char *buf; + char prog[] = "umount"; + const char *argv[MAX_ARGS]; + size_t i = 0; DUP_RESOLVE_DEVICE_OR_PATH (pathordevice, buf, , { free (buf); return -1;}); - r = command (NULL, &err, "umount", buf, NULL); + if (!(optargs_bitmask & GUESTFS_UMOUNT_FORCE_BITMASK)) + force = 0; + if (!(optargs_bitmask & GUESTFS_UMOUNT_LAZE_BITMASK)) + lazy = 0; + + ADD_ARG (argv, i, prog); + + if (force) + ADD_ARG (argv, i, "-f"); + if (lazy) + ADD_ARG (argv, i, "-l"); + + ADD_ARG (argv, i, buf); + ADD_ARG (argv, i, NULL); + + r = commandv (NULL, &err, argv); free (buf); if (r == -1) { diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index 1ce4026..67116b5 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -2742,7 +2742,7 @@ characters does I<not> work, even if the length is specified." }; { defaults with name = "umount"; - style = RErr, [String "pathordevice"], []; + style = RErr, [String "pathordevice"], [OBool "force"; OBool "laze"]; proc_nr = Some 45; fish_alias = ["unmount"]; tests = [ @@ -2755,7 +2755,7 @@ characters does I<not> work, even if the length is specified." }; [["part_disk"; "/dev/sda"; "mbr"]; ["mkfs"; "ext2"; "/dev/sda1"; ""; "NOARG"; ""; ""]; ["mount_options"; ""; "/dev/sda1"; "/"]; - ["umount"; "/"]; + ["umount"; "/"; "false"; "false"]; ["mounts"]], []) ]; shortdesc = "unmount a filesystem"; @@ -3437,12 +3437,12 @@ To download an uncompressed tarball, use C<guestfs_tar_out>." }; proc_nr = Some 73; tests = [ InitBasicFS, Always, TestLastFail ( - [["umount"; "/"]; + [["umount"; "/"; "false"; "false"]; ["mount_ro"; "/dev/sda1"; "/"]; ["touch"; "/new"]]); InitBasicFS, Always, TestOutput ( [["write"; "/new"; "data"]; - ["umount"; "/"]; + ["umount"; "/"; "false"; "false"]; ["mount_ro"; "/dev/sda1"; "/"]; ["cat"; "/new"]], "data") ]; @@ -3685,10 +3685,10 @@ C<device>." }; fish_output = Some FishOutputHexadecimal; tests = [ InitBasicFS, Always, TestOutputInt ( - [["umount"; "/dev/sda1"]; + [["umount"; "/dev/sda1"; "false"; "false"]; ["fsck"; "ext2"; "/dev/sda1"]], 0); InitBasicFS, Always, TestOutputInt ( - [["umount"; "/dev/sda1"]; + [["umount"; "/dev/sda1"; "false"; "false"]; ["zero"; "/dev/sda1"]; ["fsck"; "ext2"; "/dev/sda1"]], 8) ]; @@ -3729,7 +3729,7 @@ This command is entirely equivalent to running C<fsck -a -t fstype device>." }; progress = true; tests = [ InitBasicFS, Always, TestRun ( - [["umount"; "/dev/sda1"]; + [["umount"; "/dev/sda1"; "false"; "false"]; ["zero"; "/dev/sda1"]]) ]; shortdesc = "write zeroes to the device"; @@ -4051,7 +4051,7 @@ the human-readable, canonical hex dump of the file." }; ["mkfs"; "ext3"; "/dev/sda1"; ""; "NOARG"; ""; ""]; ["mount_options"; ""; "/dev/sda1"; "/"]; ["write"; "/new"; "test file"]; - ["umount"; "/dev/sda1"]; + ["umount"; "/dev/sda1"; "false"; "false"]; ["zerofree"; "/dev/sda1"]; ["mount_options"; ""; "/dev/sda1"; "/"]; ["cat"; "/new"]], "test file") @@ -4175,7 +4175,7 @@ are activated or deactivated." }; ["mkfs"; "ext2"; "/dev/VG/LV"; ""; "NOARG"; ""; ""]; ["mount_options"; ""; "/dev/VG/LV"; "/"]; ["write"; "/new"; "test content"]; - ["umount"; "/"]; + ["umount"; "/"; "false"; "false"]; ["lvresize"; "/dev/VG/LV"; "20"]; ["e2fsck_f"; "/dev/VG/LV"]; ["e2fsck"; "/dev/VG/LV"; "true"; "false"]; @@ -6534,7 +6534,7 @@ Rename a logical volume C<logvol> with the new name C<newlogvol>." }; proc_nr = Some 220; tests = [ InitBasicFSonLVM, Always, TestOutputList ( - [["umount"; "/"]; + [["umount"; "/"; "false"; "false"]; ["vg_activate"; "false"; "VG"]; ["vgrename"; "VG"; "VG2"]; ["vg_activate"; "true"; "VG2"]; @@ -7655,7 +7655,7 @@ it contains all zero bytes." }; proc_nr = Some 284; tests = [ InitBasicFS, Always, TestOutputTrue ( - [["umount"; "/dev/sda1"]; + [["umount"; "/dev/sda1"; "false"; "false"]; ["zero_device"; "/dev/sda1"]; ["is_zero_device"; "/dev/sda1"]]); InitBasicFS, Always, TestOutputFalse ( diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc index c73e5df..230d114 100644 --- a/gobject/Makefile.inc +++ b/gobject/Makefile.inc @@ -45,6 +45,7 @@ guestfs_gobject_headers= \ include/guestfs-gobject/optargs-inspect_get_icon.h \ include/guestfs-gobject/optargs-mount_local.h \ include/guestfs-gobject/optargs-umount_local.h \ + include/guestfs-gobject/optargs-umount.h \ include/guestfs-gobject/optargs-mkfs.h \ include/guestfs-gobject/optargs-mount_9p.h \ include/guestfs-gobject/optargs-ntfsresize.h \ @@ -90,6 +91,7 @@ guestfs_gobject_sources= \ src/optargs-inspect_get_icon.c \ src/optargs-mount_local.c \ src/optargs-umount_local.c \ + src/optargs-umount.c \ src/optargs-mkfs.c \ src/optargs-mount_9p.c \ src/optargs-ntfsresize.c \ diff --git a/po/POTFILES b/po/POTFILES index 69291fa..700c199 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -163,6 +163,7 @@ gobject/src/optargs-ntfsresize_opts.c gobject/src/optargs-set_e2attrs.c gobject/src/optargs-test0.c gobject/src/optargs-tune2fs.c +gobject/src/optargs-umount.c gobject/src/optargs-umount_local.c gobject/src/session.c gobject/src/struct-application.c -- 1.7.11.2.249.g31c7954
Wanlong Gao
2012-Jul-21 07:27 UTC
[Libguestfs] [PATCH 4/5] xfs_info: tidy up the log path message
Before patch:><fs> xfs-info /badpathlibguestfs: error: xfs_info: xfs_info: /sysroot/badpath is not a mounted XFS filesystem After patch:><fs> xfs-info /badpathlibguestfs: error: xfs_info: xfs_info: /badpath is not a mounted XFS filesystem Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> --- daemon/daemon.h | 2 ++ daemon/guestfsd.c | 18 ++++++++++++++++++ daemon/xfs.c | 7 +++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/daemon/daemon.h b/daemon/daemon.h index 39cc3f3..635ce86 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -114,6 +114,8 @@ extern void udev_settle (void); extern int random_name (char *template); +extern char *tidy_log_path (char *log); + /* This just stops gcc from giving a warning about our custom printf * formatters %Q and %R. See guestfs(3)/EXTENDING LIBGUESTFS for more * info about these. diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 2b0acf9..8fc40bf 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -1256,3 +1256,21 @@ udev_settle (void) (void) command (NULL, NULL, "udevadm", "settle", NULL); (void) command (NULL, NULL, "udevsettle", NULL); } + +char * +tidy_log_path (char *log) +{ + char *p, *buf; + size_t len = strlen (log); + size_t off; + p = strstr (log, "/sysroot"); + if (p) { + off = p - log; + buf = malloc (len + 1); + strncpy (buf, log, off); + strcpy (buf + off, log + off + 8); + return buf; + } else { + return log; + } +} diff --git a/daemon/xfs.c b/daemon/xfs.c index 5d267e6..1299a47 100644 --- a/daemon/xfs.c +++ b/daemon/xfs.c @@ -325,7 +325,9 @@ do_xfs_info (const char *pathordevice) r = command (&out, &err, "xfs_info", buf, NULL); free (buf); if (r == -1) { - reply_with_error ("%s", err); + buf = tidy_log_path (err); + reply_with_error ("%s", buf); + free (buf); goto error; } @@ -336,7 +338,8 @@ do_xfs_info (const char *pathordevice) ret = parse_xfs_info (lines); error: - free (err); + if (err) + free (err); free (out); if (lines) free_strings (lines); -- 1.7.11.2.249.g31c7954
Wanlong Gao
2012-Jul-21 07:27 UTC
[Libguestfs] [PATCH 5/5] umount: tidy up the path in log message
Before patch:><fs> xfs-info /badpathlibguestfs: error: xfs_info: xfs_info: /sysroot/badpath is not a mounted XFS filesystem After patch:><fs> umount /badpathlibguestfs: error: umount: /badpath: umount: /badpath: not found Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> --- daemon/mount.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/daemon/mount.c b/daemon/mount.c index 0b91433..aa8df51 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -221,8 +221,11 @@ do_umount (const char *pathordevice, free (buf); if (r == -1) { - reply_with_error ("%s: %s", pathordevice, err); - free (err); + buf = tidy_log_path (err); + reply_with_error ("%s: %s", pathordevice, buf); + free (buf); + if (err) + free (err); return -1; } -- 1.7.11.2.249.g31c7954
Richard W.M. Jones
2012-Jul-21 10:59 UTC
[Libguestfs] [PATCH 1/5] mount: add a macro to resolve path or device
On Sat, Jul 21, 2012 at 03:27:13PM +0800, Wanlong Gao wrote:> Add a macro DUP_RESOLVE_DEVICE_OR_PATH to resolve path or device. > > Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com> > --- > daemon/daemon.h | 18 ++++++++++++++++++ > daemon/mount.c | 13 ++----------- > po/POTFILES | 8 ++++++++ > 3 files changed, 28 insertions(+), 11 deletions(-) > > diff --git a/daemon/daemon.h b/daemon/daemon.h > index 85eec45..39cc3f3 100644 > --- a/daemon/daemon.h > +++ b/daemon/daemon.h > @@ -336,6 +336,24 @@ is_zero (const char *buffer, size_t size) > } \ > } while (0) > > +/* Helper for functions which need either a root(/sysroot) path, > + * OR a /dev/ device which exists. > + */ > +#define DUP_RESOLVE_DEVICE_OR_PATH(path,buf,cancel_stmt,fail_stmt) \Can we call this macro: STRDUP_RESOLVE_DEVICE_OR_PATH I think that would be more descriptive. Also this comment would be better: /* Same as REQUIRE_ROOT_OR_RESOLVE_DEVICE but this strdup's the result. */ Otherwise the patch seems OK. You can delete the following files from your local repository, and that will stop them from polluting these patches ...> +daemon/errnostring_gperf.c > +fish/cmds_gperf.c > +gobject/src/optargs-add_drive_opts.c > +gobject/src/optargs-mkfs_opts.c > +gobject/src/optargs-ntfsresize_opts.c > +gobject/src/optargs-test0.c > +ocaml/guestfs_c_actions.c > +src/errnostring_gperf.cRich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming blog: http://rwmj.wordpress.com Fedora now supports 80 OCaml packages (the OPEN alternative to F#) http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora