summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2012-11-23 12:30:21 +0000
committerRichard W.M. Jones <rjones@redhat.com>2012-11-23 13:17:05 +0000
commit2272cccfd4d78c37ae7fa16369a9893f29533a1a (patch)
tree32de180e3ff7bfa9652da7678ffd3b9c6dd68f6d
parenta6a0b9ba164f68dd79782f054604102017bc9c15 (diff)
downloadlibguestfs-2272cccfd4d78c37ae7fa16369a9893f29533a1a.tar.gz
libguestfs-2272cccfd4d78c37ae7fa16369a9893f29533a1a.tar.xz
libguestfs-2272cccfd4d78c37ae7fa16369a9893f29533a1a.zip
launch: Refactor duplicate code which constructs the appliance command line.
Move this into a common file.
-rw-r--r--src/guestfs-internal.h1
-rw-r--r--src/launch-appliance.c41
-rw-r--r--src/launch-libvirt.c40
-rw-r--r--src/launch.c47
4 files changed, 62 insertions, 67 deletions
diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h
index 6e0b3685..07689ffa 100644
--- a/src/guestfs-internal.h
+++ b/src/guestfs-internal.h
@@ -567,6 +567,7 @@ extern void guestfs___rollback_drives (guestfs_h *g, size_t);
extern void guestfs___launch_failed_error (guestfs_h *g);
extern void guestfs___add_dummy_appliance_drive (guestfs_h *g);
extern void guestfs___free_drives (guestfs_h *g);
+extern char *guestfs___appliance_command_line (guestfs_h *g, const char *appliance_dev);
/* launch-appliance.c */
extern char *guestfs___drive_name (size_t index, char *ret);
diff --git a/src/launch-appliance.c b/src/launch-appliance.c
index 8b2fda07..f5647cfb 100644
--- a/src/launch-appliance.c
+++ b/src/launch-appliance.c
@@ -281,7 +281,7 @@ launch_appliance (guestfs_h *g, const char *arg)
}
}
- char appliance_root[64] = "";
+ char appliance_dev[64] = "/dev/Xd";
/* Add the ext2 appliance drive (after all the drives). */
if (appliance) {
@@ -305,9 +305,8 @@ launch_appliance (guestfs_h *g, const char *arg)
add_cmdline (g, "scsi-hd,drive=appliance");
}
- snprintf (appliance_root, sizeof appliance_root, "root=/dev/%cd",
- virtio_scsi ? 's' : 'v');
- guestfs___drive_name (g->nr_drives, &appliance_root[12]);
+ appliance_dev[5] = virtio_scsi ? 's' : 'v';
+ guestfs___drive_name (g->nr_drives, &appliance_dev[7]);
}
if (STRNEQ (QEMU_OPTIONS, "")) {
@@ -422,41 +421,15 @@ launch_appliance (guestfs_h *g, const char *arg)
add_cmdline (g, "virtio-net-pci,netdev=usernet");
}
-#if defined(__arm__)
-#define SERIAL_CONSOLE "ttyAMA0"
-#else
-#define SERIAL_CONSOLE "ttyS0"
-#endif
-
-#define LINUX_CMDLINE \
- "panic=1 " /* force kernel to panic if daemon exits */ \
- "console=" SERIAL_CONSOLE " " /* serial console */ \
- "udevtimeout=600 " /* good for very slow systems (RHBZ#480319) */ \
- "no_timer_check " /* fix for RHBZ#502058 */ \
- "acpi=off " /* we don't need ACPI, turn it off */ \
- "printk.time=1 " /* display timestamp before kernel messages */ \
- "cgroup_disable=memory " /* saves us about 5 MB of RAM */
-
- /* Linux kernel command line. */
- snprintf (buf, sizeof buf,
- LINUX_CMDLINE
- "%s " /* (root) */
- "%s " /* (selinux) */
- "%s " /* (verbose) */
- "TERM=%s " /* (TERM environment variable) */
- "%s", /* (append) */
- appliance_root,
- g->selinux ? "selinux=1 enforcing=0" : "selinux=0",
- g->verbose ? "guestfs_verbose=1" : "",
- getenv ("TERM") ? : "linux",
- g->append ? g->append : "");
-
add_cmdline (g, "-kernel");
add_cmdline (g, kernel);
add_cmdline (g, "-initrd");
add_cmdline (g, initrd);
+
add_cmdline (g, "-append");
- add_cmdline (g, buf);
+ char *cmdline = guestfs___appliance_command_line (g, appliance_dev);
+ add_cmdline (g, cmdline);
+ free (cmdline);
/* Finish off the command line. */
incr_cmdline_size (g);
diff --git a/src/launch-libvirt.c b/src/launch-libvirt.c
index 3ee4c0f8..8e9b5ad1 100644
--- a/src/launch-libvirt.c
+++ b/src/launch-libvirt.c
@@ -623,40 +623,12 @@ construct_libvirt_xml_boot (guestfs_h *g, xmlTextWriterPtr xo,
const char *kernel, const char *initrd,
size_t appliance_index)
{
- char buf[256];
- char appliance_root[64] = "";
-
- /* XXX Lots of common code shared with src/launch-appliance.c */
-#if defined(__arm__)
-#define SERIAL_CONSOLE "ttyAMA0"
-#else
-#define SERIAL_CONSOLE "ttyS0"
-#endif
-
-#define LINUX_CMDLINE \
- "panic=1 " /* force kernel to panic if daemon exits */ \
- "console=" SERIAL_CONSOLE " " /* serial console */ \
- "udevtimeout=600 " /* good for very slow systems (RHBZ#480319) */ \
- "no_timer_check " /* fix for RHBZ#502058 */ \
- "acpi=off " /* we don't need ACPI, turn it off */ \
- "printk.time=1 " /* display timestamp before kernel messages */ \
- "cgroup_disable=memory " /* saves us about 5 MB of RAM */
+ char appliance_dev[64] = "/dev/sd";
+ char *cmdline;
/* Linux kernel command line. */
- guestfs___drive_name (appliance_index, appliance_root);
-
- snprintf (buf, sizeof buf,
- LINUX_CMDLINE
- "root=/dev/sd%s " /* (root) */
- "%s " /* (selinux) */
- "%s " /* (verbose) */
- "TERM=%s " /* (TERM environment variable) */
- "%s", /* (append) */
- appliance_root,
- g->selinux ? "selinux=1 enforcing=0" : "selinux=0",
- g->verbose ? "guestfs_verbose=1" : "",
- getenv ("TERM") ? : "linux",
- g->append ? g->append : "");
+ guestfs___drive_name (appliance_index, &appliance_dev[7]);
+ cmdline = guestfs___appliance_command_line (g, appliance_dev);
XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "os"));
@@ -673,14 +645,16 @@ construct_libvirt_xml_boot (guestfs_h *g, xmlTextWriterPtr xo,
XMLERROR (-1, xmlTextWriterEndElement (xo));
XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "cmdline"));
- XMLERROR (-1, xmlTextWriterWriteString (xo, BAD_CAST buf));
+ XMLERROR (-1, xmlTextWriterWriteString (xo, BAD_CAST cmdline));
XMLERROR (-1, xmlTextWriterEndElement (xo));
XMLERROR (-1, xmlTextWriterEndElement (xo));
+ free (cmdline);
return 0;
err:
+ free (cmdline);
return -1;
}
diff --git a/src/launch.c b/src/launch.c
index d2c1a42a..cb1d80b6 100644
--- a/src/launch.c
+++ b/src/launch.c
@@ -784,3 +784,50 @@ guestfs__get_state (guestfs_h *g)
{
return g->state;
}
+
+/* Construct the Linux command line passed to the appliance. This is
+ * used by the 'appliance' and 'libvirt' attach-methods, and is simply
+ * located in this file because it's a convenient place for this
+ * common code.
+ *
+ * The 'appliance_dev' parameter must be the full device name of the
+ * appliance disk and must have already been adjusted to take into
+ * account virtio-blk or virtio-scsi; eg "/dev/sdb".
+ *
+ * Note that this returns a newly allocated buffer which must be freed
+ * by the caller.
+ */
+#if defined(__arm__)
+#define SERIAL_CONSOLE "ttyAMA0"
+#else
+#define SERIAL_CONSOLE "ttyS0"
+#endif
+
+char *
+guestfs___appliance_command_line (guestfs_h *g, const char *appliance_dev)
+{
+ char *term = getenv ("TERM");
+ char *ret;
+
+ ret = safe_asprintf
+ (g,
+ "panic=1" /* force kernel to panic if daemon exits */
+ " console=" SERIAL_CONSOLE /* serial console */
+ " udevtimeout=600" /* good for very slow systems (RHBZ#480319) */
+ " no_timer_check" /* fix for RHBZ#502058 */
+ " acpi=off" /* we don't need ACPI, turn it off */
+ " printk.time=1" /* display timestamp before kernel messages */
+ " cgroup_disable=memory" /* saves us about 5 MB of RAM */
+ " root=%s" /* root (appliance_dev) */
+ " %s" /* selinux */
+ "%s" /* verbose */
+ " TERM=%s" /* TERM environment variable */
+ "%s%s", /* append */
+ appliance_dev,
+ g->selinux ? "selinux=1 enforcing=0" : "selinux=0",
+ g->verbose ? " guestfs_verbose=1" : "",
+ term ? term : "linux",
+ g->append ? " " : "", g->append ? g->append : "");
+
+ return ret;
+}