extern char *do_get_e2uuid (const char *device);
extern int do_fsck (const char *fstype, const char *device);
extern int do_zero (const char *device);
+extern int do_grub_install (const char *root, const char *device);
xdr_free ((xdrproc_t) xdr_guestfs_zero_args, (char *) &args);
}
+static void grub_install_stub (XDR *xdr_in)
+{
+ int r;
+ struct guestfs_grub_install_args args;
+ const char *root;
+ const char *device;
+
+ memset (&args, 0, sizeof args);
+
+ if (!xdr_guestfs_grub_install_args (xdr_in, &args)) {
+ reply_with_error ("%s: daemon failed to decode procedure arguments", "grub_install");
+ return;
+ }
+ root = args.root;
+ device = args.device;
+
+ r = do_grub_install (root, device);
+ if (r == -1)
+ /* do_grub_install has already called reply_with_error */
+ goto done;
+
+ reply (NULL, NULL);
+done:
+ xdr_free ((xdrproc_t) xdr_guestfs_grub_install_args, (char *) &args);
+}
+
void dispatch_incoming_message (XDR *xdr_in)
{
switch (proc_nr) {
case GUESTFS_PROC_ZERO:
zero_stub (xdr_in);
break;
+ case GUESTFS_PROC_GRUB_INSTALL:
+ grub_install_stub (xdr_in);
+ break;
default:
reply_with_error ("dispatch_incoming_message: unknown procedure number %d", proc_nr);
}
printf ("%-20s %s\n", "get-qemu", "get the qemu binary");
printf ("%-20s %s\n", "get-state", "get the current state");
printf ("%-20s %s\n", "get-verbose", "get verbose mode");
+ printf ("%-20s %s\n", "grub-install", "install GRUB");
printf ("%-20s %s\n", "is-busy", "is busy processing a command");
printf ("%-20s %s\n", "is-config", "is in configuration state");
printf ("%-20s %s\n", "is-dir", "test if file exists");
if (strcasecmp (cmd, "zero") == 0)
pod2text ("zero - write zeroes to the device", " zero <device>\n\nThis command writes zeroes over the first few blocks of C<device>.\n\nHow many blocks are zeroed isn't specified (but it's I<not> enough\nto securely wipe the device). It should be sufficient to remove\nany partition tables, filesystem superblocks and so on.");
else
+ if (strcasecmp (cmd, "grub_install") == 0 || strcasecmp (cmd, "grub-install") == 0)
+ pod2text ("grub-install - install GRUB", " grub-install <root> <device>\n\nThis command installs GRUB (the Grand Unified Bootloader) on\nC<device>, with the root directory being C<root>.");
+ else
display_builtin_command (cmd);
}
return r;
}
+static int run_grub_install (const char *cmd, int argc, char *argv[])
+{
+ int r;
+ const char *root;
+ const char *device;
+ if (argc != 2) {
+ fprintf (stderr, "%s should have 2 parameter(s)\n", cmd);
+ fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
+ return -1;
+ }
+ root = argv[0];
+ device = argv[1];
+ r = guestfs_grub_install (g, root, device);
+ return r;
+}
+
int run_action (const char *cmd, int argc, char *argv[])
{
if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0)
if (strcasecmp (cmd, "zero") == 0)
return run_zero (cmd, argc, argv);
else
+ if (strcasecmp (cmd, "grub_install") == 0 || strcasecmp (cmd, "grub-install") == 0)
+ return run_grub_install (cmd, argc, argv);
+ else
{
fprintf (stderr, "%s: unknown command\n", cmd);
return -1;
"get-qemu",
"get-state",
"get-verbose",
+ "grub-install",
"is-busy",
"is-config",
"is-dir",
This returns the verbose messages flag.
+=head2 grub-install
+
+ grub-install root device
+
+This command installs GRUB (the Grand Unified Bootloader) on
+C<device>, with the root directory being C<root>.
+
=head2 is-busy
is-busy
This function returns a C truth value on success or -1 on error.
+=head2 guestfs_grub_install
+
+ int guestfs_grub_install (guestfs_h *handle,
+ const char *root,
+ const char *device);
+
+This command installs GRUB (the Grand Unified Bootloader) on
+C<device>, with the root directory being C<root>.
+
+This function returns 0 on success or -1 on error.
+
=head2 guestfs_is_busy
int guestfs_is_busy (guestfs_h *handle);
private native void _zero (long g, String device)
throws LibGuestFSException;
+ /**
+ * install GRUB
+ *
+ * This command installs GRUB (the Grand Unified
+ * Bootloader) on "device", with the root directory being
+ * "root".
+ *
+ * @throws LibGuestFSException
+ */
+ public void grub_install (String root, String device)
+ throws LibGuestFSException
+ {
+ if (g == 0)
+ throw new LibGuestFSException ("grub_install: handle is closed");
+ _grub_install (g, root, device);
+ }
+ private native void _grub_install (long g, String root, String device)
+ throws LibGuestFSException;
+
}
}
}
+JNIEXPORT void JNICALL
+Java_com_redhat_et_libguestfs_GuestFS__1grub_1install
+ (JNIEnv *env, jobject obj, jlong jg, jstring jroot, jstring jdevice)
+{
+ guestfs_h *g = (guestfs_h *) (long) jg;
+ int r;
+ const char *root;
+ const char *device;
+
+ root = (*env)->GetStringUTFChars (env, jroot, NULL);
+ device = (*env)->GetStringUTFChars (env, jdevice, NULL);
+ r = guestfs_grub_install (g, root, device);
+ (*env)->ReleaseStringUTFChars (env, jroot, root);
+ (*env)->ReleaseStringUTFChars (env, jdevice, device);
+ if (r == -1) {
+ throw_exception (env, guestfs_last_error (g));
+ return ;
+ }
+}
+
external get_e2uuid : t -> string -> string = "ocaml_guestfs_get_e2uuid"
external fsck : t -> string -> string -> int = "ocaml_guestfs_fsck"
external zero : t -> string -> unit = "ocaml_guestfs_zero"
+external grub_install : t -> string -> string -> unit = "ocaml_guestfs_grub_install"
val zero : t -> string -> unit
(** write zeroes to the device *)
+val grub_install : t -> string -> string -> unit
+(** install GRUB *)
+
CAMLreturn (rv);
}
+CAMLprim value
+ocaml_guestfs_grub_install (value gv, value rootv, value devicev)
+{
+ CAMLparam3 (gv, rootv, devicev);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("grub_install: used handle after closing it");
+
+ const char *root = String_val (rootv);
+ const char *device = String_val (devicev);
+ int r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_grub_install (g, root, device);
+ caml_leave_blocking_section ();
+ if (r == -1)
+ ocaml_guestfs_raise_error (g, "grub_install");
+
+ rv = Val_unit;
+ CAMLreturn (rv);
+}
+
if (r == -1)
croak ("zero: %s", guestfs_last_error (g));
+void
+grub_install (g, root, device)
+ guestfs_h *g;
+ char *root;
+ char *device;
+PREINIT:
+ int r;
+ PPCODE:
+ r = guestfs_grub_install (g, root, device);
+ if (r == -1)
+ croak ("grub_install: %s", guestfs_last_error (g));
+
This returns the verbose messages flag.
+=item $h->grub_install ($root, $device);
+
+This command installs GRUB (the Grand Unified Bootloader) on
+C<device>, with the root directory being C<root>.
+
=item $busy = $h->is_busy ();
This returns true iff this handle is busy processing a command
return py_r;
}
+static PyObject *
+py_guestfs_grub_install (PyObject *self, PyObject *args)
+{
+ PyObject *py_g;
+ guestfs_h *g;
+ PyObject *py_r;
+ int r;
+ const char *root;
+ const char *device;
+
+ if (!PyArg_ParseTuple (args, (char *) "Oss:guestfs_grub_install",
+ &py_g, &root, &device))
+ return NULL;
+ g = get_handle (py_g);
+
+ r = guestfs_grub_install (g, root, device);
+ if (r == -1) {
+ PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
+ return NULL;
+ }
+
+ Py_INCREF (Py_None);
+ py_r = Py_None;
+ return py_r;
+}
+
static PyMethodDef methods[] = {
{ (char *) "create", py_guestfs_create, METH_VARARGS, NULL },
{ (char *) "close", py_guestfs_close, METH_VARARGS, NULL },
{ (char *) "get_e2uuid", py_guestfs_get_e2uuid, METH_VARARGS, NULL },
{ (char *) "fsck", py_guestfs_fsck, METH_VARARGS, NULL },
{ (char *) "zero", py_guestfs_zero, METH_VARARGS, NULL },
+ { (char *) "grub_install", py_guestfs_grub_install, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
"""
return libguestfsmod.zero (self._o, device)
+ def grub_install (self, root, device):
+ u"""This command installs GRUB (the Grand Unified
+ Bootloader) on "device", with the root directory being
+ "root".
+ """
+ return libguestfsmod.grub_install (self._o, root, device)
+
return Qnil;
}
+static VALUE ruby_guestfs_grub_install (VALUE gv, VALUE rootv, VALUE devicev)
+{
+ guestfs_h *g;
+ Data_Get_Struct (gv, guestfs_h, g);
+ if (!g)
+ rb_raise (rb_eArgError, "%s: used handle after closing it", "grub_install");
+
+ const char *root = StringValueCStr (rootv);
+ if (!root)
+ rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+ "root", "grub_install");
+ const char *device = StringValueCStr (devicev);
+ if (!device)
+ rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+ "device", "grub_install");
+
+ int r;
+
+ r = guestfs_grub_install (g, root, device);
+ if (r == -1)
+ rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+ return Qnil;
+}
+
/* Initialize the module. */
void Init__guestfs ()
{
ruby_guestfs_fsck, 2);
rb_define_method (c_guestfs, "zero",
ruby_guestfs_zero, 1);
+ rb_define_method (c_guestfs, "grub_install",
+ ruby_guestfs_grub_install, 2);
}
return 0;
}
+struct grub_install_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 grub_install_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ struct grub_install_ctx *ctx = (struct grub_install_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_grub_install");
+ return;
+ }
+
+ ml->main_loop_quit (ml, g);
+
+ if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+ error (g, "%s: failed to parse reply header", "guestfs_grub_install");
+ 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_grub_install");
+ return;
+ }
+ goto done;
+ }
+ done:
+ ctx->cb_sequence = 1;
+}
+
+int guestfs_grub_install (guestfs_h *g,
+ const char *root,
+ const char *device)
+{
+ struct guestfs_grub_install_args args;
+ struct grub_install_ctx ctx;
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ int serial;
+
+ if (check_state (g, "guestfs_grub_install") == -1) return -1;
+ guestfs_set_busy (g);
+
+ memset (&ctx, 0, sizeof ctx);
+
+ args.root = (char *) root;
+ args.device = (char *) device;
+ serial = guestfs__send_sync (g, GUESTFS_PROC_GRUB_INSTALL,
+ (xdrproc_t) xdr_guestfs_grub_install_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, grub_install_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_grub_install");
+ guestfs_set_ready (g);
+ return -1;
+ }
+
+ if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_GRUB_INSTALL, 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;
+}
+
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);
+extern int guestfs_grub_install (guestfs_h *handle, const char *root, const char *device);
}
bool_t
+xdr_guestfs_grub_install_args (XDR *xdrs, guestfs_grub_install_args *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->root, ~0))
+ return FALSE;
+ 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;
};
typedef struct guestfs_zero_args guestfs_zero_args;
+struct guestfs_grub_install_args {
+ char *root;
+ char *device;
+};
+typedef struct guestfs_grub_install_args guestfs_grub_install_args;
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
GUESTFS_PROC_GET_E2UUID = 83,
GUESTFS_PROC_FSCK = 84,
GUESTFS_PROC_ZERO = 85,
- GUESTFS_PROC_NR_PROCS = 85 + 1,
+ GUESTFS_PROC_GRUB_INSTALL = 86,
+ GUESTFS_PROC_NR_PROCS = 86 + 1,
};
typedef enum guestfs_procedure guestfs_procedure;
#define GUESTFS_MESSAGE_MAX 4194304
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_grub_install_args (XDR *, guestfs_grub_install_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*);
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_grub_install_args ();
extern bool_t xdr_guestfs_procedure ();
extern bool_t xdr_guestfs_message_direction ();
extern bool_t xdr_guestfs_message_status ();
string device<>;
};
+struct guestfs_grub_install_args {
+ string root<>;
+ string device<>;
+};
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
GUESTFS_PROC_GET_E2UUID = 83,
GUESTFS_PROC_FSCK = 84,
GUESTFS_PROC_ZERO = 85,
+ GUESTFS_PROC_GRUB_INSTALL = 86,
GUESTFS_PROC_NR_PROCS
};
fprintf (stderr, "warning: \"guestfs_get_e2uuid\" has no tests\n");
}
+static int test_grub_install_0 (void)
+{
+ /* InitBasicFS for grub_install (0): create ext2 on /dev/sda1 */
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_umount_all (g);
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_lvm_remove_all (g);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char *lines[] = {
+ ",",
+ NULL
+ };
+ int r;
+ suppress_error = 0;
+ r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines);
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_mkfs (g, "ext2", "/dev/sda1");
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_mount (g, "/dev/sda1", "/");
+ if (r == -1)
+ return -1;
+ }
+ /* TestOutputTrue for grub_install (0) */
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_grub_install (g, "/", "/dev/sda1");
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_is_dir (g, "/boot");
+ if (r == -1)
+ return -1;
+ if (!r) {
+ fprintf (stderr, "test_grub_install_0: expected true, got false\n");
+ return -1;
+ }
+ }
+ return 0;
+}
+
static int test_zero_0 (void)
{
/* InitBasicFS for zero (0): create ext2 on /dev/sda1 */
exit (1);
}
- nr_tests = 94;
+ nr_tests = 95;
test_num++;
+ printf ("%3d/%3d test_grub_install_0\n", test_num, nr_tests);
+ if (test_grub_install_0 () == -1) {
+ printf ("test_grub_install_0 FAILED\n");
+ failed++;
+ }
+ test_num++;
printf ("%3d/%3d test_zero_0\n", test_num, nr_tests);
if (test_zero_0 () == -1) {
printf ("test_zero_0 FAILED\n");