From patchwork Wed Aug 22 12:48:16 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 1361141 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 5BA6F3FD40 for ; Wed, 22 Aug 2012 12:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932381Ab2HVMsn (ORCPT ); Wed, 22 Aug 2012 08:48:43 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:47336 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752765Ab2HVMsj (ORCPT ); Wed, 22 Aug 2012 08:48:39 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr13so1437056pbb.19 for ; Wed, 22 Aug 2012 05:48:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=bym2FzkfoyYWVU5fU3rc5QY5w4R8F2xHT6vGWEMqj5w=; b=NoP9H99mcAeqspl/LAvxN0Zuu0dYkWvsxQ3CPhP+66F95UIHMyoH18v0rdGlPs7TOH V5TcHu6pE5vYe2zAqtqB/bId2/Dy4CpFBVtvSImDFt1I2Y/ypLP4n2syGsnzifHEgCih EiHP6a74gHENrd3Q7gK9gtGo39y1m7r3IBkDNyb9ofSzwwDYCVvBsz5zo9PUsHkHd6aS yK/3EVpObc2+oLm608wXEatB3dmhs+Lg5tNis/16CAGdA8n/xssxYoUs2z/s9N4QxvDg AuEWvTCat1sYUcibVKgd44U9Cb01UZo5B1RTDuHpcbR5YrfVmo69qmfKiogSkM+xEPh/ DM8g== Received: by 10.68.229.2 with SMTP id sm2mr52026524pbc.57.1345639719154; Wed, 22 Aug 2012 05:48:39 -0700 (PDT) Received: from hj.localdomain ([58.194.229.103]) by mx.google.com with ESMTPS id ou6sm3684076pbc.9.2012.08.22.05.48.36 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 22 Aug 2012 05:48:38 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Sasha Levin , Ingo Molnar , Cyrill Gorcunov , kvm@vger.kernel.org Subject: [PATCH 3/4] kvm tools: Merge guest init and init_stage2.c Date: Wed, 22 Aug 2012 20:48:16 +0800 Message-Id: <1345639697-22680-3-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.11.4 In-Reply-To: <1345639697-22680-1-git-send-email-asias.hejun@gmail.com> References: <1345639697-22680-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. Signed-off-by: Asias He --- 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 diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 2bb5713..8aa0125 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -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 diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index 1b407a6..0412e58 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -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; } diff --git a/tools/kvm/guest/init.c b/tools/kvm/guest/init.c index 032a261..6a2e1cd 100644 --- a/tools/kvm/guest/init.c +++ b/tools/kvm/guest/init.c @@ -7,6 +7,7 @@ #include #include #include +#include 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)); diff --git a/tools/kvm/guest/init_stage2.c b/tools/kvm/guest/init_stage2.c deleted file mode 100644 index 5f93894..0000000 --- a/tools/kvm/guest/init_stage2.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This is a stage 2 of the init. This part should do all the heavy - * lifting such as setting up the console and calling /bin/sh. - */ -#include -#include -#include -#include -#include -#include - -static int run_process(char *filename) -{ - char *new_argv[] = { filename, NULL }; - char *new_env[] = { "TERM=linux", NULL }; - - 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); -} - -int main(int argc, char *argv[]) -{ - pid_t child; - int status; - - /* 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); - - return 0; -}