From patchwork Wed Aug 22 12:48:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 1361131 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 019FB3FD40 for ; Wed, 22 Aug 2012 12:48:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756984Ab2HVMsk (ORCPT ); Wed, 22 Aug 2012 08:48:40 -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 S1752648Ab2HVMsi (ORCPT ); Wed, 22 Aug 2012 08:48:38 -0400 Received: by pbbrr13 with SMTP id rr13so1437056pbb.19 for ; Wed, 22 Aug 2012 05:48:37 -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; bh=CpU56GuqVWUfRVFQDXIWqyzSgvoz5skbrT4MuV+hSWA=; b=dp+j6JsahQWt36x+Bc8T9ZYw/wGzEsZ0NOQ7/aiR8YFkMuy+1GqBMRoWkBdUJqlUCA jqRVM8UP1iMJtAhwZ477iFBUD4YtZVNMzTEu49b/tyYw9ifQgU5IlBMvHQ6bbZ9XkKUl tfQ+Ozu9+HPTu//2RdOGJ9/khocv4ygvvb3I8y/MBDU7aMviFEKnXb1AlVUgrUy/OVa/ 2GItiCoLLV1S+F1T9fDWSXXO6+1y78bj2ji8WUoESEliuKQclJSU0jzcIVnlhXtJvi6r gd7xQc3auXVlyzvff/yQ+nA9ct6rq+0xraP+KEe6c7tEP9amK+zpDIXDp4zBQmiwclBl +nKg== Received: by 10.68.224.161 with SMTP id rd1mr33724354pbc.133.1345639713684; Wed, 22 Aug 2012 05:48:33 -0700 (PDT) Received: from hj.localdomain ([58.194.229.103]) by mx.google.com with ESMTPS id ou6sm3684076pbc.9.2012.08.22.05.48.30 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 22 Aug 2012 05:48:32 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Sasha Levin , Ingo Molnar , Cyrill Gorcunov , kvm@vger.kernel.org Subject: [PATCH 1/4] kvm tools: Embed init and init_stage2 into lkvm binary Date: Wed, 22 Aug 2012 20:48:14 +0800 Message-Id: <1345639697-22680-1-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.11.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. Signed-off-by: Asias He --- tools/kvm/Makefile | 13 ++++++++----- tools/kvm/builtin-run.c | 50 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 2bf931b..1099b76 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -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 diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index befce9e..1b407a6 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -111,6 +111,11 @@ 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; + static const char * const run_usage[] = { "lkvm run [] []", NULL @@ -803,24 +808,41 @@ void kvm_run_help(void) usage_with_options(run_usage, options); } -static int kvm_custom_stage2(void) +static int kvm_setup_guest_init(void) { - char tmp[PATH_MAX], dst[PATH_MAX], *src; const char *rootfs = custom_rootfs_name; - int r; - - src = realpath("guest/init_stage2", NULL); - if (src == NULL) - return -ENOMEM; + char tmp[PATH_MAX]; + size_t size; + int fd, ret; + char *data; + + /* Setup /virt/init */ + size = (size_t)&_binary_guest_init_size; + data = (char *)&_binary_guest_init_start; + snprintf(tmp, PATH_MAX, "%s%s/virt/init", 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); + /* 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); - snprintf(dst, PATH_MAX, "/host/%s", src); - r = symlink(dst, tmp); - free(src); - - return r; + return 0; } static int kvm_run_set_sandbox(void) @@ -1147,8 +1169,8 @@ static int kvm_cmd_run_init(int argc, const char **argv) if (!no_dhcp) strcat(real_cmdline, " ip=dhcp"); - if (kvm_custom_stage2()) - die("Failed linking stage 2 of init."); + if (kvm_setup_guest_init()) + die("Failed to setup init for guest."); } } else if (!strstr(real_cmdline, "root=")) { strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));