Skip to contentSkip to conversation

kvm tools: Introduce 'kvm setup' command

RFC/PATCH Pekka Enberg 1 participant 1 message Aug 24, 2011 KVM

lore
Share & tools
lore
#0 lore raw

This patch implements 'kvm setup' command that can be used to setup a guest filesystem that shares system libraries and binaries from host filesystem in read-only mode.

You can setup a new shared rootfs guest with:

./kvm setup -n default

and launch it with:

./kvm run --9p /,hostfs -p "init=virt/init" -d ~/.kvm-tools/default/

We want to teach 'kvm run' to be able to launch guest filesystems by name in the future. Furthermore, 'kvm run' should setup a 'default' filesystem and use it by default unless the user specifies otherwise.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Sasha Levin <[email protected]>
---
 tools/kvm/.gitignore                  |    1 +
 tools/kvm/Documentation/kvm-setup.txt |   15 +++
 tools/kvm/Makefile                    |   11 ++-
 tools/kvm/builtin-setup.c             |  184 +++++++++++++++++++++++++++++++++
 tools/kvm/command-list.txt            |    1 +
 tools/kvm/guest/init.c                |   40 +++++++
 tools/kvm/include/kvm/builtin-setup.h |    7 ++
 tools/kvm/kvm-cmd.c                   |    2 +
 8 files changed, 259 insertions(+), 2 deletions(-)
 create mode 100644 tools/kvm/Documentation/kvm-setup.txt
 create mode 100644 tools/kvm/builtin-setup.c
 create mode 100644 tools/kvm/guest/init.c
 create mode 100644 tools/kvm/include/kvm/builtin-setup.h
tools/kvm/.gitignore +1 -0
index 852d052..6ace4ec 100644
@@ -6,4 +6,5 @@ tags
 include/common-cmds.h
 tests/boot/boot_test.iso
 tests/boot/rootfs/
+guest/init
 KVMTOOLS-VERSION-FILE
tools/kvm/Documentation/kvm-setup.txt new file +15 -0
new file mode 100644
index 0000000..c845d17
@@ -0,0 +1,15 @@
+kvm-setup(1)
+================
+
+NAME
+----
+kvm-setup - Setup a new virtual machine
+
+SYNOPSIS
+--------
+[verse]
+'kvm setup <name>'
+
+DESCRIPTION
+-----------
+The command setups a virtual machine.
tools/kvm/Makefile +9 -2
index 669386f..25cbd7e 100644
@@ -20,6 +20,8 @@ TAGS	:= ctags
 
 PROGRAM	:= kvm
 
+GUEST_INIT := guest/init
+
 OBJS	+= builtin-balloon.o
 OBJS	+= builtin-debug.o
 OBJS	+= builtin-help.o
@@ -28,6 +30,7 @@ OBJS	+= builtin-stat.o
 OBJS	+= builtin-pause.o
 OBJS	+= builtin-resume.o
 OBJS	+= builtin-run.o
+OBJS	+= builtin-setup.o
 OBJS	+= builtin-stop.o
 OBJS	+= builtin-version.o
 OBJS	+= cpuid.o
@@ -159,7 +162,7 @@ WARNINGS += -Wunused-result
 
 CFLAGS	+= $(WARNINGS)
 
-all: $(PROGRAM)
+all: $(PROGRAM) $(GUEST_INIT)
 
 KVMTOOLS-VERSION-FILE:
 	@$(SHELL_PATH) util/KVMTOOLS-VERSION-GEN $(OUTPUT)
@@ -169,6 +172,10 @@ $(PROGRAM): $(DEPS) $(OBJS)
 	$(E) "  LINK    " $@
 	$(Q) $(CC) $(OBJS) $(LIBS) -o $@
 
+$(GUEST_INIT): guest/init.c
+	$(E) "  LINK    " $@
+	$(Q) $(CC) -static guest/init.c -o $@
+
 $(DEPS):
 
 %.d: %.c
@@ -240,7 +247,7 @@ clean:
 	$(Q) rm -f bios/bios-rom.h
 	$(Q) rm -f tests/boot/boot_test.iso
 	$(Q) rm -rf tests/boot/rootfs/
-	$(Q) rm -f $(DEPS) $(OBJS) $(PROGRAM)
+	$(Q) rm -f $(DEPS) $(OBJS) $(PROGRAM) $(GUEST_INIT)
 	$(Q) rm -f cscope.*
 	$(Q) rm -f $(KVM_INCLUDE)/common-cmds.h
 	$(Q) rm -f KVMTOOLS-VERSION-FILE
tools/kvm/builtin-setup.c new file +184 -0

184 lines not on this page · raw message

tools/kvm/command-list.txt +1 -0
index f201d75..0d16c62 100644
@@ -3,6 +3,7 @@
 # command name			category [deprecated] [common]
 #
 kvm-run				mainporcelain common
+kvm-setup			mainporcelain common
 kvm-pause			common
 kvm-resume			common
 kvm-version			common
tools/kvm/guest/init.c new file +40 -0
new file mode 100644
index 0000000..837acfb
@@ -0,0 +1,40 @@
+/*
+ * This is a simple init for shared rootfs guests. It brings up critical
+ * mountpoints and then launches /bin/sh.
+ */
+#include <sys/mount.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+
+static int run_process(char *filename)
+{
+	char *new_argv[] = { filename, NULL };
+	char *new_env[] = { NULL };
+
+	return execve(filename, new_argv, new_env);
+}
+
+static void do_mounts(void)
+{
+	mount("hostfs", "/host", "9p", MS_RDONLY, "trans=virtio,version=9p2000.L");
+	mount("", "/sys", "sysfs", 0, NULL);
+	mount("proc", "/proc", "proc", 0, NULL);
+	mount("devtmpfs", "/dev", "devtmpfs", 0, NULL);
+}
+
+int main(int argc, char *argv[])
+{
+	puts("Mounting...");
+
+	do_mounts();
+
+	puts("Starting '/bin/sh'...");
+
+	run_process("/bin/sh");
+
+	printf("Init failed: %s\n", strerror(errno));
+
+	return 0;
+}
tools/kvm/include/kvm/builtin-setup.h new file +7 -0
new file mode 100644
index 0000000..b0eb345
@@ -0,0 +1,7 @@
+#ifndef KVM__SETUP_H
+#define KVM__SETUP_H
+
+int kvm_cmd_setup(int argc, const char **argv, const char *prefix);
+void kvm_setup_help(void);
+
+#endif
tools/kvm/kvm-cmd.c +2 -0
index 8fc6d22..96108a8 100644
@@ -11,6 +11,7 @@
 #include "kvm/builtin-balloon.h"
 #include "kvm/builtin-list.h"
 #include "kvm/builtin-version.h"
+#include "kvm/builtin-setup.h"
 #include "kvm/builtin-stop.h"
 #include "kvm/builtin-stat.h"
 #include "kvm/builtin-help.h"
@@ -29,6 +30,7 @@ struct cmd_struct kvm_commands[] = {
 	{ "stop",	kvm_cmd_stop,		kvm_stop_help,		0 },
 	{ "stat",	kvm_cmd_stat,		kvm_stat_help,		0 },
 	{ "help",	kvm_cmd_help,		NULL,			0 },
+	{ "setup",	kvm_cmd_setup,		kvm_setup_help,		0 },
 	{ "run",	kvm_cmd_run,		kvm_run_help,		0 },
 	{ NULL,		NULL,			NULL,			0 },
 };
Signature · 1 line
1.7.1