From d901cc916102f1aaccfb73396b48aa303e5b8cd7 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 14 May 2009 23:47:17 +0100 Subject: Add support for zerofree command. --- src/generator.ml | 23 +++++++++++++ src/guestfs-actions.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/guestfs-actions.h | 1 + src/guestfs_protocol.c | 10 ++++++ src/guestfs_protocol.h | 10 +++++- src/guestfs_protocol.x | 5 +++ 6 files changed, 135 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/generator.ml b/src/generator.ml index 82bfb880..652564d8 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -1996,6 +1996,29 @@ The returned strings are transcoded to UTF-8."); This runs C on the given C. The result is the human-readable, canonical hex dump of the file."); + ("zerofree", (RErr, [String "device"]), 97, [], + [InitNone, Always, TestOutput ( + [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","]; + ["mkfs"; "ext3"; "/dev/sda1"]; + ["mount"; "/dev/sda1"; "/"]; + ["write_file"; "/new"; "test file"; "0"]; + ["umount"; "/dev/sda1"]; + ["zerofree"; "/dev/sda1"]; + ["mount"; "/dev/sda1"; "/"]; + ["cat"; "/new"]], "test file")], + "zero unused inodes and disk blocks on ext2/3 filesystem", + "\ +This runs the I program on C. This program +claims to zero unused inodes and disk blocks on an ext2/3 +filesystem, thus making it possible to compress the filesystem +more effectively. + +You should B run this program if the filesystem is +mounted. + +It is possible that using this program can damage the filesystem +or data on the filesystem."); + ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index 12960510..d27ef012 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -8809,3 +8809,90 @@ char *guestfs_hexdump (guestfs_h *g, return ctx.ret.dump; /* caller will free */ } +struct zerofree_ctx { + /* This flag is set by the callbacks, so we know we've done + * the callbacks as expected, and in the right sequence. + * 0 = not called, 1 = reply_cb called. + */ + int cb_sequence; + struct guestfs_message_header hdr; + struct guestfs_message_error err; +}; + +static void zerofree_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct zerofree_ctx *ctx = (struct zerofree_ctx *) data; + + /* This should definitely not happen. */ + if (ctx->cb_sequence != 0) { + ctx->cb_sequence = 9999; + error (g, "%s: internal error: reply callback called twice", "guestfs_zerofree"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_zerofree"); + return; + } + if (ctx->hdr.status == GUESTFS_STATUS_ERROR) { + if (!xdr_guestfs_message_error (xdr, &ctx->err)) { + error (g, "%s: failed to parse reply error", "guestfs_zerofree"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_zerofree (guestfs_h *g, + const char *device) +{ + struct guestfs_zerofree_args args; + struct zerofree_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_zerofree") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.device = (char *) device; + serial = guestfs__send_sync (g, GUESTFS_PROC_ZEROFREE, + (xdrproc_t) xdr_guestfs_zerofree_args, (char *) &args); + if (serial == -1) { + guestfs_end_busy (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, zerofree_reply_cb, &ctx); + (void) ml->main_loop_run (ml, g); + guestfs_set_reply_callback (g, NULL, NULL); + if (ctx.cb_sequence != 1) { + error (g, "%s reply failed, see earlier error messages", "guestfs_zerofree"); + guestfs_end_busy (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_ZEROFREE, serial) == -1) { + guestfs_end_busy (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + free (ctx.err.error_message); + guestfs_end_busy (g); + return -1; + } + + guestfs_end_busy (g); + return 0; +} + diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h index 92c1a932..9a4e9232 100644 --- a/src/guestfs-actions.h +++ b/src/guestfs-actions.h @@ -139,3 +139,4 @@ extern int guestfs_equal (guestfs_h *handle, const char *file1, const char *file extern char **guestfs_strings (guestfs_h *handle, const char *path); extern char **guestfs_strings_e (guestfs_h *handle, const char *encoding, const char *path); extern char *guestfs_hexdump (guestfs_h *handle, const char *path); +extern int guestfs_zerofree (guestfs_h *handle, const char *device); diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c index d53bcffc..c505b251 100644 --- a/src/guestfs_protocol.c +++ b/src/guestfs_protocol.c @@ -1613,6 +1613,16 @@ xdr_guestfs_hexdump_ret (XDR *xdrs, guestfs_hexdump_ret *objp) return TRUE; } +bool_t +xdr_guestfs_zerofree_args (XDR *xdrs, guestfs_zerofree_args *objp) +{ + register int32_t *buf; + + if (!xdr_string (xdrs, &objp->device, ~0)) + return FALSE; + return TRUE; +} + bool_t xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp) { diff --git a/src/guestfs_protocol.h b/src/guestfs_protocol.h index 87db39b3..a855307c 100644 --- a/src/guestfs_protocol.h +++ b/src/guestfs_protocol.h @@ -837,6 +837,11 @@ struct guestfs_hexdump_ret { }; typedef struct guestfs_hexdump_ret guestfs_hexdump_ret; +struct guestfs_zerofree_args { + char *device; +}; +typedef struct guestfs_zerofree_args guestfs_zerofree_args; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -934,7 +939,8 @@ enum guestfs_procedure { GUESTFS_PROC_STRINGS = 94, GUESTFS_PROC_STRINGS_E = 95, GUESTFS_PROC_HEXDUMP = 96, - GUESTFS_PROC_NR_PROCS = 96 + 1, + GUESTFS_PROC_ZEROFREE = 97, + GUESTFS_PROC_NR_PROCS = 97 + 1, }; typedef enum guestfs_procedure guestfs_procedure; #define GUESTFS_MESSAGE_MAX 4194304 @@ -1116,6 +1122,7 @@ extern bool_t xdr_guestfs_strings_e_args (XDR *, guestfs_strings_e_args*); extern bool_t xdr_guestfs_strings_e_ret (XDR *, guestfs_strings_e_ret*); extern bool_t xdr_guestfs_hexdump_args (XDR *, guestfs_hexdump_args*); extern bool_t xdr_guestfs_hexdump_ret (XDR *, guestfs_hexdump_ret*); +extern bool_t xdr_guestfs_zerofree_args (XDR *, guestfs_zerofree_args*); extern bool_t xdr_guestfs_procedure (XDR *, guestfs_procedure*); extern bool_t xdr_guestfs_message_direction (XDR *, guestfs_message_direction*); extern bool_t xdr_guestfs_message_status (XDR *, guestfs_message_status*); @@ -1256,6 +1263,7 @@ extern bool_t xdr_guestfs_strings_e_args (); extern bool_t xdr_guestfs_strings_e_ret (); extern bool_t xdr_guestfs_hexdump_args (); extern bool_t xdr_guestfs_hexdump_ret (); +extern bool_t xdr_guestfs_zerofree_args (); extern bool_t xdr_guestfs_procedure (); extern bool_t xdr_guestfs_message_direction (); extern bool_t xdr_guestfs_message_status (); diff --git a/src/guestfs_protocol.x b/src/guestfs_protocol.x index 42264244..f09e5b5b 100644 --- a/src/guestfs_protocol.x +++ b/src/guestfs_protocol.x @@ -648,6 +648,10 @@ struct guestfs_hexdump_ret { string dump<>; }; +struct guestfs_zerofree_args { + string device<>; +}; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -745,6 +749,7 @@ enum guestfs_procedure { GUESTFS_PROC_STRINGS = 94, GUESTFS_PROC_STRINGS_E = 95, GUESTFS_PROC_HEXDUMP = 96, + GUESTFS_PROC_ZEROFREE = 97, GUESTFS_PROC_NR_PROCS }; -- cgit