summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2009-04-30 19:28:54 +0100
committerRichard Jones <rjones@redhat.com>2009-04-30 19:28:54 +0100
commit62df226f26bd6ac3c481a7790eb89d760d2f0386 (patch)
tree617a5a436598902b2239d67535925520c20a6e6e /src
parent2a42bec2c5ee521f29179a5aab713f5a9ca2c3b8 (diff)
downloadlibguestfs-62df226f26bd6ac3c481a7790eb89d760d2f0386.tar.gz
libguestfs-62df226f26bd6ac3c481a7790eb89d760d2f0386.tar.xz
libguestfs-62df226f26bd6ac3c481a7790eb89d760d2f0386.zip
Added 'zero' command to wipe partition tables and superblocks.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/generator.ml24
-rw-r--r--src/guestfs-actions.c86
-rw-r--r--src/guestfs-actions.h1
-rw-r--r--src/guestfs_protocol.c10
-rw-r--r--src/guestfs_protocol.h10
-rw-r--r--src/guestfs_protocol.x5
6 files changed, 133 insertions, 3 deletions
diff --git a/src/generator.ml b/src/generator.ml
index 615d32ec..8d5450d1 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -1633,8 +1633,13 @@ This returns the ext2/3/4 filesystem UUID of the filesystem on
C<device>.");
("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [],
- [InitBasicFS, TestRun (
- [["fsck"; "ext2"; "/dev/sda1"]])],
+ [InitBasicFS, TestOutputInt (
+ [["umount"; "/dev/sda1"];
+ ["fsck"; "ext2"; "/dev/sda1"]], 0);
+ InitBasicFS, TestOutputInt (
+ [["umount"; "/dev/sda1"];
+ ["zero"; "/dev/sda1"];
+ ["fsck"; "ext2"; "/dev/sda1"]], 8)],
"run the filesystem checker",
"\
This runs the filesystem checker (fsck) on C<device> which
@@ -1647,6 +1652,21 @@ status codes can be summed together.
It is entirely equivalent to running C<fsck -a -t fstype device>.
Note that checking or repairing NTFS volumes is not supported
(by linux-ntfs).");
+
+ ("zero", (RErr, [String "device"]), 85, [],
+ [InitBasicFS, TestOutput (
+ [["umount"; "/dev/sda1"];
+ ["zero"; "/dev/sda1"];
+ ["file"; "/dev/sda1"]], "data")],
+ "write zeroes to the device",
+ "\
+This command writes zeroes over the first few blocks of C<device>.
+
+How many blocks are zeroed isn't specified (but it's I<not> enough
+to securely wipe the device). It should be sufficient to remove
+any partition tables, filesystem superblocks and so on.");
+
+
]
let all_functions = non_daemon_functions @ daemon_functions
diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c
index a075b802..e2da1c3a 100644
--- a/src/guestfs-actions.c
+++ b/src/guestfs-actions.c
@@ -7642,3 +7642,89 @@ int guestfs_fsck (guestfs_h *g,
return ctx.ret.status;
}
+struct zero_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 zero_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ struct zero_ctx *ctx = (struct zero_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_zero");
+ return;
+ }
+
+ ml->main_loop_quit (ml, g);
+
+ if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+ error (g, "%s: failed to parse reply header", "guestfs_zero");
+ 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_zero");
+ return;
+ }
+ goto done;
+ }
+ done:
+ ctx->cb_sequence = 1;
+}
+
+int guestfs_zero (guestfs_h *g,
+ const char *device)
+{
+ struct guestfs_zero_args args;
+ struct zero_ctx ctx;
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ int serial;
+
+ if (check_state (g, "guestfs_zero") == -1) return -1;
+ guestfs_set_busy (g);
+
+ memset (&ctx, 0, sizeof ctx);
+
+ args.device = (char *) device;
+ serial = guestfs__send_sync (g, GUESTFS_PROC_ZERO,
+ (xdrproc_t) xdr_guestfs_zero_args, (char *) &args);
+ if (serial == -1) {
+ guestfs_set_ready (g);
+ return -1;
+ }
+
+ guestfs__switch_to_receiving (g);
+ ctx.cb_sequence = 0;
+ guestfs_set_reply_callback (g, zero_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_zero");
+ guestfs_set_ready (g);
+ return -1;
+ }
+
+ if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_ZERO, serial) == -1) {
+ guestfs_set_ready (g);
+ return -1;
+ }
+
+ if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+ error (g, "%s", ctx.err.error_message);
+ guestfs_set_ready (g);
+ return -1;
+ }
+
+ guestfs_set_ready (g);
+ return 0;
+}
+
diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h
index 6e56099a..0fc62a18 100644
--- a/src/guestfs-actions.h
+++ b/src/guestfs-actions.h
@@ -124,3 +124,4 @@ extern char *guestfs_get_e2label (guestfs_h *handle, const char *device);
extern int guestfs_set_e2uuid (guestfs_h *handle, const char *device, const char *uuid);
extern char *guestfs_get_e2uuid (guestfs_h *handle, const char *device);
extern int guestfs_fsck (guestfs_h *handle, const char *fstype, const char *device);
+extern int guestfs_zero (guestfs_h *handle, const char *device);
diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c
index 16e80f17..b12133a0 100644
--- a/src/guestfs_protocol.c
+++ b/src/guestfs_protocol.c
@@ -1450,6 +1450,16 @@ xdr_guestfs_fsck_ret (XDR *xdrs, guestfs_fsck_ret *objp)
}
bool_t
+xdr_guestfs_zero_args (XDR *xdrs, guestfs_zero_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)
{
register int32_t *buf;
diff --git a/src/guestfs_protocol.h b/src/guestfs_protocol.h
index 64e4b4db..2e79669c 100644
--- a/src/guestfs_protocol.h
+++ b/src/guestfs_protocol.h
@@ -750,6 +750,11 @@ struct guestfs_fsck_ret {
};
typedef struct guestfs_fsck_ret guestfs_fsck_ret;
+struct guestfs_zero_args {
+ char *device;
+};
+typedef struct guestfs_zero_args guestfs_zero_args;
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
@@ -835,7 +840,8 @@ enum guestfs_procedure {
GUESTFS_PROC_SET_E2UUID = 82,
GUESTFS_PROC_GET_E2UUID = 83,
GUESTFS_PROC_FSCK = 84,
- GUESTFS_PROC_NR_PROCS = 84 + 1,
+ GUESTFS_PROC_ZERO = 85,
+ GUESTFS_PROC_NR_PROCS = 85 + 1,
};
typedef enum guestfs_procedure guestfs_procedure;
#define GUESTFS_MESSAGE_MAX 4194304
@@ -1002,6 +1008,7 @@ extern bool_t xdr_guestfs_get_e2uuid_args (XDR *, guestfs_get_e2uuid_args*);
extern bool_t xdr_guestfs_get_e2uuid_ret (XDR *, guestfs_get_e2uuid_ret*);
extern bool_t xdr_guestfs_fsck_args (XDR *, guestfs_fsck_args*);
extern bool_t xdr_guestfs_fsck_ret (XDR *, guestfs_fsck_ret*);
+extern bool_t xdr_guestfs_zero_args (XDR *, guestfs_zero_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*);
@@ -1127,6 +1134,7 @@ extern bool_t xdr_guestfs_get_e2uuid_args ();
extern bool_t xdr_guestfs_get_e2uuid_ret ();
extern bool_t xdr_guestfs_fsck_args ();
extern bool_t xdr_guestfs_fsck_ret ();
+extern bool_t xdr_guestfs_zero_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 92dc8eac..c940bf44 100644
--- a/src/guestfs_protocol.x
+++ b/src/guestfs_protocol.x
@@ -582,6 +582,10 @@ struct guestfs_fsck_ret {
int status;
};
+struct guestfs_zero_args {
+ string device<>;
+};
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
@@ -667,6 +671,7 @@ enum guestfs_procedure {
GUESTFS_PROC_SET_E2UUID = 82,
GUESTFS_PROC_GET_E2UUID = 83,
GUESTFS_PROC_FSCK = 84,
+ GUESTFS_PROC_ZERO = 85,
GUESTFS_PROC_NR_PROCS
};