Skip to contentSkip to conversation

kvm tools: Embed init and init_stage2 into lkvm binary

PATCH New Asias He 1 participant 4 messages Aug 22, 2012 KVM

lore
Share & tools
lore

Asias He

author
#0 lore raw

PATCH 1/4 kvm tools: Embed init and init_stage2 into lkvm binary

We generate guest init and init_stage2 on the fly and put them in

~/.lkvm/$guest/virt/init
~/.lkvm/$guest/virt/init_stage2
.

This makes 'lkvm run' no longer depending on guest/{int, init_stage2}.

Further, we can merge init and init_stage2 since there is no need to split the init function into two stages any more.

---
 tools/kvm/Makefile      | 13 ++++++++-----
 tools/kvm/builtin-run.c | 50 +++++++++++++++++++++++++++++++++++--------------
 2 files changed, 44 insertions(+), 19 deletions(-)
tools/kvm/Makefile +8 -5
index 2bf931b..1099b76 100644
@@ -286,14 +286,15 @@ KVMTOOLS-VERSION-FILE:
 # $(OTHEROBJS) are things that do not get substituted like this.
 #
 STATIC_OBJS = $(patsubst %.o,%.static.o,$(OBJS) $(OBJS_STATOPT))
+GUEST_OBJS = guest/guest_init.o guest/guest_init_stage2.o
 
-$(PROGRAM)-static:  $(DEPS) $(STATIC_OBJS) $(OTHEROBJS)
+$(PROGRAM)-static:  $(DEPS) $(STATIC_OBJS) $(OTHEROBJS) $(GUEST_INIT) $(GUEST_INIT_S2)
 	$(E) "  LINK    " $@
-	$(Q) $(CC) -static $(CFLAGS) $(STATIC_OBJS) $(OTHEROBJS) $(LIBS) $(LIBS_STATOPT) -o $@
+	$(Q) $(CC) -static $(CFLAGS) $(STATIC_OBJS) $(OTHEROBJS) $(GUEST_OBJS) $(LIBS) $(LIBS_STATOPT) -o $@
 
-$(PROGRAM): $(DEPS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS)
+$(PROGRAM): $(DEPS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(GUEST_INIT) $(GUEST_INIT_S2)
 	$(E) "  LINK    " $@
-	$(Q) $(CC) $(CFLAGS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(LIBS) $(LIBS_DYNOPT) -o $@
+	$(Q) $(CC) $(CFLAGS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(GUEST_OBJS) $(LIBS) $(LIBS_DYNOPT) -o $@
 
 $(PROGRAM_ALIAS): $(PROGRAM)
 	$(E) "  LN      " $@
@@ -302,10 +303,12 @@ $(PROGRAM_ALIAS): $(PROGRAM)
 $(GUEST_INIT): guest/init.c
 	$(E) "  LINK    " $@
 	$(Q) $(CC) -static guest/init.c -o $@
+	$(Q) ld -r -b binary -o guest/guest_init.o $(GUEST_INIT)
 
 $(GUEST_INIT_S2): guest/init_stage2.c
 	$(E) "  LINK    " $@
 	$(Q) $(CC) -static guest/init_stage2.c -o $@
+	$(Q) ld -r -b binary -o guest/guest_init_stage2.o $(GUEST_INIT_S2)
 
 $(DEPS):
 
@@ -403,7 +406,7 @@ clean:
 	$(Q) rm -f x86/bios/bios-rom.h
 	$(Q) rm -f tests/boot/boot_test.iso
 	$(Q) rm -rf tests/boot/rootfs/
-	$(Q) rm -f $(DEPS) $(OBJS) $(OTHEROBJS) $(OBJS_DYNOPT) $(STATIC_OBJS) $(PROGRAM) $(PROGRAM_ALIAS) $(PROGRAM)-static $(GUEST_INIT) $(GUEST_INIT_S2)
+	$(Q) rm -f $(DEPS) $(OBJS) $(OTHEROBJS) $(OBJS_DYNOPT) $(STATIC_OBJS) $(PROGRAM) $(PROGRAM_ALIAS) $(PROGRAM)-static $(GUEST_INIT) $(GUEST_INIT_S2) $(GUEST_OBJS)
 	$(Q) rm -f cscope.*
 	$(Q) rm -f tags
 	$(Q) rm -f TAGS
tools/kvm/builtin-run.c +36 -14

74 lines not on this page · raw message

Signature · 1 line
1.7.11.4
author
#2 lore raw

PATCH 3/4 kvm tools: Merge guest init and init_stage2.c

Two stages init process is introduced in

commit eaf720b285947a6f4e29174d0eba1899de31d8ab kvm tools: Split custom rootfs init into two stages

to address the outdated init binary issue.

Currenly, init binary is embedded in lkvm binary and generated on the fly. So there is no need to split the init process.

This makes
1) the size of lkvm binary smaller becasue only one statically linked init binary.
2) the init process simpler and cleaner.

---
 tools/kvm/Makefile            | 16 ++++---------
 tools/kvm/builtin-run.c       | 15 ------------
 tools/kvm/guest/init.c        | 33 +++++++++++++++++++++++++-
 tools/kvm/guest/init_stage2.c | 55 -------------------------------------------
 4 files changed, 37 insertions(+), 82 deletions(-)
 delete mode 100644 tools/kvm/guest/init_stage2.c
tools/kvm/Makefile +5 -11
index 2bb5713..8aa0125 100644
@@ -38,7 +38,6 @@ PROGRAM	:= lkvm
 PROGRAM_ALIAS := vm
 
 GUEST_INIT := guest/init
-GUEST_INIT_S2 := guest/init_stage2
 
 OBJS	+= builtin-balloon.o
 OBJS	+= builtin-debug.o
@@ -271,7 +270,7 @@ ifneq ($(WERROR),0)
 	CFLAGS += -Werror
 endif
 
-all: arch_support_check $(PROGRAM) $(PROGRAM_ALIAS) $(GUEST_INIT) $(GUEST_INIT_S2)
+all: arch_support_check $(PROGRAM) $(PROGRAM_ALIAS) $(GUEST_INIT)
 
 arch_support_check:
 	$(UNSUPP_ERR)
@@ -287,13 +286,13 @@ KVMTOOLS-VERSION-FILE:
 # $(OTHEROBJS) are things that do not get substituted like this.
 #
 STATIC_OBJS = $(patsubst %.o,%.static.o,$(OBJS) $(OBJS_STATOPT))
-GUEST_OBJS = guest/guest_init.o guest/guest_init_stage2.o
+GUEST_OBJS = guest/guest_init.o
 
-$(PROGRAM)-static:  $(DEPS) $(STATIC_OBJS) $(OTHEROBJS) $(GUEST_INIT) $(GUEST_INIT_S2)
+$(PROGRAM)-static:  $(DEPS) $(STATIC_OBJS) $(OTHEROBJS) $(GUEST_INIT)
 	$(E) "  LINK    " $@
 	$(Q) $(CC) -static $(CFLAGS) $(STATIC_OBJS) $(OTHEROBJS) $(GUEST_OBJS) $(LIBS) $(LIBS_STATOPT) -o $@
 
-$(PROGRAM): $(DEPS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(GUEST_INIT) $(GUEST_INIT_S2)
+$(PROGRAM): $(DEPS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(GUEST_INIT)
 	$(E) "  LINK    " $@
 	$(Q) $(CC) $(CFLAGS) $(OBJS) $(OBJS_DYNOPT) $(OTHEROBJS) $(GUEST_OBJS) $(LIBS) $(LIBS_DYNOPT) -o $@
 
@@ -306,11 +305,6 @@ $(GUEST_INIT): guest/init.c
 	$(Q) $(CC) -static guest/init.c -o $@
 	$(Q) $(LD) -r -b binary -o guest/guest_init.o $(GUEST_INIT)
 
-$(GUEST_INIT_S2): guest/init_stage2.c
-	$(E) "  LINK    " $@
-	$(Q) $(CC) -static guest/init_stage2.c -o $@
-	$(Q) $(LD) -r -b binary -o guest/guest_init_stage2.o $(GUEST_INIT_S2)
-
 $(DEPS):
 
 util/rbtree.d: ../../lib/rbtree.c
@@ -407,7 +401,7 @@ clean:
 	$(Q) rm -f x86/bios/bios-rom.h
 	$(Q) rm -f tests/boot/boot_test.iso
 	$(Q) rm -rf tests/boot/rootfs/
-	$(Q) rm -f $(DEPS) $(OBJS) $(OTHEROBJS) $(OBJS_DYNOPT) $(STATIC_OBJS) $(PROGRAM) $(PROGRAM_ALIAS) $(PROGRAM)-static $(GUEST_INIT) $(GUEST_INIT_S2) $(GUEST_OBJS)
+	$(Q) rm -f $(DEPS) $(OBJS) $(OTHEROBJS) $(OBJS_DYNOPT) $(STATIC_OBJS) $(PROGRAM) $(PROGRAM_ALIAS) $(PROGRAM)-static $(GUEST_INIT) $(GUEST_OBJS)
 	$(Q) rm -f cscope.*
 	$(Q) rm -f tags
 	$(Q) rm -f TAGS
tools/kvm/builtin-run.c +0 -15
index 1b407a6..0412e58 100644
@@ -111,8 +111,6 @@ bool do_debug_print = false;
 static int nrcpus;
 static int vidmode = -1;
 
-extern char _binary_guest_init_stage2_start;
-extern char _binary_guest_init_stage2_size;
 extern char _binary_guest_init_start;
 extern char _binary_guest_init_size;
 
@@ -829,19 +827,6 @@ static int kvm_setup_guest_init(void)
 		die("Fail to setup %s", tmp);
 	close(fd);
 
-	/* Setup /virt/init_stage2 */
-	size = (size_t)&_binary_guest_init_stage2_size;
-	data = (char *)&_binary_guest_init_stage2_start;
-	snprintf(tmp, PATH_MAX, "%s%s/virt/init_stage2", kvm__get_dir(), rootfs);
-	remove(tmp);
-	fd = open(tmp, O_CREAT | O_WRONLY, 0755);
-	if (fd < 0)
-		die("Fail to setup %s", tmp);
-	ret = xwrite(fd, data, size);
-	if (ret < 0)
-		die("Fail to setup %s", tmp);
-	close(fd);
-
 	return 0;
 }
 
tools/kvm/guest/init.c +32 -1
index 032a261..6a2e1cd 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <errno.h>
+#include <linux/reboot.h>
 
 static int run_process(char *filename)
 {
@@ -16,6 +17,14 @@ static int run_process(char *filename)
 	return execve(filename, new_argv, new_env);
 }
 
+static int run_process_sandbox(char *filename)
+{
+	char *new_argv[] = { filename, "/virt/sandbox.sh", NULL };
+	char *new_env[] = { "TERM=linux", NULL };
+
+	return execve(filename, new_argv, new_env);
+}
+
 static void do_mounts(void)
 {
 	mount("hostfs", "/host", "9p", MS_RDONLY, "trans=virtio,version=9p2000.L");
@@ -26,11 +35,33 @@ static void do_mounts(void)
 
 int main(int argc, char *argv[])
 {
+	pid_t child;
+	int status;
+
 	puts("Mounting...");
 
 	do_mounts();
 
-	run_process("/virt/init_stage2");
+	/* get session leader */
+	setsid();
+
+	/* set controlling terminal */
+	ioctl(0, TIOCSCTTY, 1);
+
+	child = fork();
+	if (child < 0) {
+		printf("Fatal: fork() failed with %d\n", child);
+		return 0;
+	} else if (child == 0) {
+		if (access("/virt/sandbox.sh", R_OK) == 0)
+			run_process_sandbox("/bin/sh");
+		else
+			run_process("/bin/sh");
+	} else {
+		waitpid(child, &status, 0);
+	}
+
+	reboot(LINUX_REBOOT_CMD_RESTART);
 
 	printf("Init failed: %s\n", strerror(errno));
 
tools/kvm/guest/init_stage2.c deleted +0 -55

55 lines not on this page · raw message

Signature · 1 line
1.7.11.4
author
#3 lore raw

PATCH 4/4 kvm tools: Make 'lkvm setup' work outside kernel/tools/kvm dir

In reply to Asias He

Generate

~/.lkvm/$guest/virt/etc/passwd
~/.lkvm/$guest/virt/init

on the fly.

---
 tools/kvm/builtin-setup.c | 33 +++++++++++++++++++++++++++++++--
 tools/kvm/guest/passwd    |  1 -
 2 files changed, 31 insertions(+), 3 deletions(-)
 delete mode 100644 tools/kvm/guest/passwd
tools/kvm/builtin-setup.c +31 -2
index 232aa60..11aa676 100644
@@ -3,6 +3,7 @@
 #include <kvm/builtin-setup.h>
 #include <kvm/kvm.h>
 #include <kvm/parse-options.h>
+#include <kvm/read-write.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -19,6 +20,9 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+extern char _binary_guest_init_start;
+extern char _binary_guest_init_size;
+
 static const char *instance_name;
 
 static const char * const setup_usage[] = {
@@ -126,19 +130,44 @@ static const char *guestfs_symlinks[] = {
 static int copy_init(const char *guestfs_name)
 {
 	char path[PATH_MAX];
+	size_t size;
+	int fd, ret;
+	char *data;
 
+	size = (size_t)&_binary_guest_init_size;
+	data = (char *)&_binary_guest_init_start;
 	snprintf(path, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), guestfs_name);
+	remove(path);
+	fd = open(path, O_CREAT | O_WRONLY, 0755);
+	if (fd < 0)
+		die("Fail to setup %s", path);
+	ret = xwrite(fd, data, size);
+	if (ret < 0)
+		die("Fail to setup %s", path);
+	close(fd);
 
-	return copy_file("guest/init", path);
+	return 0;
 }
 
 static int copy_passwd(const char *guestfs_name)
 {
 	char path[PATH_MAX];
+	FILE *file;
+	int ret;
 
 	snprintf(path, PATH_MAX, "%s%s/etc/passwd", kvm__get_dir(), guestfs_name);
 
-	return copy_file("guest/passwd", path);
+	file = fopen(path, "w");
+	if (!file)
+		return -1;
+
+	ret = fprintf(file, "root:x:0:0:root:/root:/bin/sh\n");
+	if (ret < 0)
+		return ret;
+
+	fclose(file);
+
+	return 0;
 }
 
 static int make_guestfs_symlink(const char *guestfs_name, const char *path)
tools/kvm/guest/passwd deleted +0 -1
deleted file mode 100644
index eb85a55..0000000
@@ -1 +0,0 @@
-root:x:0:0:root:/root:/bin/sh
Signature · 1 line
1.7.11.4
author
#1 lore raw

PATCH 2/4 kvm tools: Use LD instead using 'ld' directly

In reply to Asias He
---
 tools/kvm/Makefile | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/kvm/Makefile +4 -3
index 1099b76..2bb5713 100644
@@ -20,6 +20,7 @@ include config/utilities.mak
 include config/feature-tests.mak
 
 CC	:= $(CROSS_COMPILE)$(CC)
+LD	:= $(CROSS_COMPILE)$(LD)
 
 FIND	:= find
 CSCOPE	:= cscope
@@ -303,12 +304,12 @@ $(PROGRAM_ALIAS): $(PROGRAM)
 $(GUEST_INIT): guest/init.c
 	$(E) "  LINK    " $@
 	$(Q) $(CC) -static guest/init.c -o $@
-	$(Q) ld -r -b binary -o guest/guest_init.o $(GUEST_INIT)
+	$(Q) $(LD) -r -b binary -o guest/guest_init.o $(GUEST_INIT)
 
 $(GUEST_INIT_S2): guest/init_stage2.c
 	$(E) "  LINK    " $@
 	$(Q) $(CC) -static guest/init_stage2.c -o $@
-	$(Q) ld -r -b binary -o guest/guest_init_stage2.o $(GUEST_INIT_S2)
+	$(Q) $(LD) -r -b binary -o guest/guest_init_stage2.o $(GUEST_INIT_S2)
 
 $(DEPS):
 
@@ -372,7 +373,7 @@ x86/bios/bios.bin.elf: x86/bios/entry.S x86/bios/e820.c x86/bios/int10.c x86/bio
 	$(E) "  CC       x86/bios/entry.o"
 	$(Q) $(CC) $(CFLAGS) $(BIOS_CFLAGS) -c -s x86/bios/entry.S -o x86/bios/entry.o
 	$(E) "  LD      " $@
-	$(Q) ld -T x86/bios/rom.ld.S -o x86/bios/bios.bin.elf x86/bios/memcpy.o x86/bios/entry.o x86/bios/e820.o x86/bios/int10.o x86/bios/int15.o
+	$(Q) $(LD) -T x86/bios/rom.ld.S -o x86/bios/bios.bin.elf x86/bios/memcpy.o x86/bios/entry.o x86/bios/e820.o x86/bios/int10.o x86/bios/int15.o
 
 x86/bios/bios.bin: x86/bios/bios.bin.elf
 	$(E) "  OBJCOPY " $@
Signature · 1 line
1.7.11.4