* [PATCH] kvm tools: Split custom rootfs init into two stages
@ 2011-12-05 9:22 Sasha Levin
2011-12-05 9:30 ` Cyrill Gorcunov
0 siblings, 1 reply; 2+ messages in thread
From: Sasha Levin @ 2011-12-05 9:22 UTC (permalink / raw)
To: penberg; +Cc: kvm, mingo, asias.hejun, gorcunov, Sasha Levin
Currently custom rootfs init is built along with the main KVM tools executable
and is copied into custom rootfs directories when they are created with
'kvm setup'. The problem there is that if the init code changes, they have
to be manually copied to custom rootfs directories.
Instead, this patch splits init process into two parts. One part that simply
handles mounts, and passes it to stage 2 of the init.
Stage 2 really sits along in the code tree, and does all the heavy lifting.
This allows us to make init changes in the code tree and have it automatically
be updated in custom rootfs guests without having to copy files over manually.
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
tools/kvm/Makefile | 9 +++++++--
tools/kvm/builtin-run.c | 27 +++++++++++++++++++++++++++
tools/kvm/guest/init.c | 14 +++-----------
3 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile
index bb5f6b0..ece3306 100644
--- a/tools/kvm/Makefile
+++ b/tools/kvm/Makefile
@@ -21,6 +21,7 @@ TAGS := ctags
PROGRAM := kvm
GUEST_INIT := guest/init
+GUEST_INIT_S2 := guest/init_stage2
OBJS += builtin-balloon.o
OBJS += builtin-debug.o
@@ -179,7 +180,7 @@ WARNINGS += -Wwrite-strings
CFLAGS += $(WARNINGS)
-all: $(PROGRAM) $(GUEST_INIT)
+all: $(PROGRAM) $(GUEST_INIT) $(GUEST_INIT_S2)
KVMTOOLS-VERSION-FILE:
@$(SHELL_PATH) util/KVMTOOLS-VERSION-GEN $(OUTPUT)
@@ -193,6 +194,10 @@ $(GUEST_INIT): guest/init.c
$(E) " LINK " $@
$(Q) $(CC) -static guest/init.c -o $@
+$(GUEST_INIT_S2): guest/init_stage2.c
+ $(E) " LINK " $@
+ $(Q) $(CC) -static guest/init_stage2.c -o $@
+
$(DEPS):
%.d: %.c
@@ -269,7 +274,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) $(GUEST_INIT)
+ $(Q) rm -f $(DEPS) $(OBJS) $(PROGRAM) $(GUEST_INIT) $(GUEST_INIT_S2)
$(Q) rm -f cscope.*
$(Q) rm -f $(KVM_INCLUDE)/common-cmds.h
$(Q) rm -f KVMTOOLS-VERSION-FILE
diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index 43cf2c4..7c5ae47 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -702,6 +702,31 @@ void kvm_run_help(void)
usage_with_options(run_usage, options);
}
+static int kvm_custom_stage2(void)
+{
+ char tmp[PATH_MAX], dst[PATH_MAX], *src;
+ const char *rootfs;
+ int r;
+
+ src = realpath("guest/init_stage2", NULL);
+ if (src == NULL)
+ return -ENOMEM;
+
+ if (image_filename[0] == NULL)
+ rootfs = "default";
+ else
+ rootfs = image_filename[0];
+
+ sprintf(tmp, "%s%s/virt/init_stage2", kvm__get_dir(), rootfs);
+ remove(tmp);
+
+ sprintf(dst, "/host/%s", src);
+ r = symlink(dst, tmp);
+ free(src);
+
+ return r;
+}
+
int kvm_cmd_run(int argc, const char **argv, const char *prefix)
{
static char real_cmdline[2048], default_name[20];
@@ -867,6 +892,8 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
strcat(real_cmdline, " init=/virt/init");
if (!no_dhcp)
strcat(real_cmdline, " ip=dhcp");
+ if (kvm_custom_stage2())
+ die("Failed linking stage 2 of init.");
}
} else if (!strstr(real_cmdline, "root=")) {
strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
diff --git a/tools/kvm/guest/init.c b/tools/kvm/guest/init.c
index 8975023..032a261 100644
--- a/tools/kvm/guest/init.c
+++ b/tools/kvm/guest/init.c
@@ -1,6 +1,6 @@
/*
- * This is a simple init for shared rootfs guests. It brings up critical
- * mountpoints and then launches /bin/sh.
+ * This is a simple init for shared rootfs guests. This part should be limited
+ * to doing mounts and running stage 2 of the init process.
*/
#include <sys/mount.h>
#include <string.h>
@@ -30,15 +30,7 @@ int main(int argc, char *argv[])
do_mounts();
- /* get session leader */
- setsid();
-
- /* set controlling terminal */
- ioctl (0, TIOCSCTTY, 1);
-
- puts("Starting '/bin/sh'...");
-
- run_process("/bin/sh");
+ run_process("/virt/init_stage2");
printf("Init failed: %s\n", strerror(errno));
--
1.7.8
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] kvm tools: Split custom rootfs init into two stages
2011-12-05 9:22 [PATCH] kvm tools: Split custom rootfs init into two stages Sasha Levin
@ 2011-12-05 9:30 ` Cyrill Gorcunov
0 siblings, 0 replies; 2+ messages in thread
From: Cyrill Gorcunov @ 2011-12-05 9:30 UTC (permalink / raw)
To: Sasha Levin; +Cc: penberg, kvm, mingo, asias.hejun
On Mon, Dec 05, 2011 at 11:22:11AM +0200, Sasha Levin wrote:
>
> +static int kvm_custom_stage2(void)
> +{
> + char tmp[PATH_MAX], dst[PATH_MAX], *src;
> + const char *rootfs;
> + int r;
> +
> + src = realpath("guest/init_stage2", NULL);
> + if (src == NULL)
> + return -ENOMEM;
> +
> + if (image_filename[0] == NULL)
> + rootfs = "default";
> + else
> + rootfs = image_filename[0];
> +
> + sprintf(tmp, "%s%s/virt/init_stage2", kvm__get_dir(), rootfs);
> + remove(tmp);
> +
> + sprintf(dst, "/host/%s", src);
> + r = symlink(dst, tmp);
> + free(src);
> +
> + return r;
> +}
> +
I might be paranoid -- but could you please use snprintf here? :)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-12-05 9:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-05 9:22 [PATCH] kvm tools: Split custom rootfs init into two stages Sasha Levin
2011-12-05 9:30 ` Cyrill Gorcunov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox