public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <levinsasha928@gmail.com>
To: penberg@kernel.org
Cc: kvm@vger.kernel.org, mingo@elte.hu, gorcunov@gmail.com,
	asias.hejun@gmail.com, Sasha Levin <levinsasha928@gmail.com>
Subject: [PATCH 5/5] kvm tools: Add guestfs network autoconfiguration
Date: Tue,  6 Sep 2011 02:23:55 +0300	[thread overview]
Message-ID: <1315265035-9496-5-git-send-email-levinsasha928@gmail.com> (raw)
In-Reply-To: <1315265035-9496-1-git-send-email-levinsasha928@gmail.com>

Add a script to automatically configure networking in the guest.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/builtin-setup.c |   14 ++++++++++++++
 tools/kvm/guest/init.c    |    4 ++++
 tools/kvm/guest/setnet.sh |   22 ++++++++++++++++++++++
 3 files changed, 40 insertions(+), 0 deletions(-)
 create mode 100755 tools/kvm/guest/setnet.sh

diff --git a/tools/kvm/builtin-setup.c b/tools/kvm/builtin-setup.c
index 7587476..f7588dd 100644
--- a/tools/kvm/builtin-setup.c
+++ b/tools/kvm/builtin-setup.c
@@ -129,6 +129,15 @@ static int copy_init(const char *guestfs_name)
 	return copy_file("guest/init", path);
 }
 
+static int copy_net(const char *guestfs_name)
+{
+	char path[PATH_MAX];
+
+	snprintf(path, PATH_MAX, "%s%s%s/virt/setnet.sh", HOME_DIR, KVM_PID_FILE_PATH, guestfs_name);
+
+	return copy_file("guest/setnet.sh", path);
+}
+
 static int make_guestfs_symlink(const char *guestfs_name, const char *path)
 {
 	char target[PATH_MAX];
@@ -171,6 +180,7 @@ static void make_guestfs_dir(const char *guestfs_name, const char *dir)
 static int do_setup(const char *guestfs_name)
 {
 	unsigned int i;
+	int ret;
 
 	make_root_dir();
 
@@ -183,6 +193,10 @@ static int do_setup(const char *guestfs_name)
 		make_guestfs_symlink(guestfs_name, guestfs_symlinks[i]);
 	}
 
+	ret = copy_net(guestfs_name);
+	if (ret < 0)
+		return ret;
+
 	return copy_init(guestfs_name);
 }
 
diff --git a/tools/kvm/guest/init.c b/tools/kvm/guest/init.c
index 837acfb..7733026 100644
--- a/tools/kvm/guest/init.c
+++ b/tools/kvm/guest/init.c
@@ -30,6 +30,10 @@ int main(int argc, char *argv[])
 
 	do_mounts();
 
+	puts("Setting up network...");
+
+	system("/bin/sh virt/setnet.sh");
+
 	puts("Starting '/bin/sh'...");
 
 	run_process("/bin/sh");
diff --git a/tools/kvm/guest/setnet.sh b/tools/kvm/guest/setnet.sh
new file mode 100755
index 0000000..3da9c22
--- /dev/null
+++ b/tools/kvm/guest/setnet.sh
@@ -0,0 +1,22 @@
+for f in /sys/class/net/*; do
+	type=`cat $f/type`
+	if [ $type -eq 1 ]; then
+		f=${f#/sys/class/net/}
+
+		eval "dhcpcd -A $f 2> /dev/null"
+		if [ $? -eq 0 ]; then
+			exit
+		fi
+
+		eval "dhclient $f 2> /dev/null"
+		if [ $? -eq 0 ]; then
+			exit
+		fi
+
+		ifconfig $f 192.168.33.15
+		route add default 192.168.33.1
+		echo "nameserver 8.8.8.8" >> /etc/resolv.conf
+
+		exit
+	fi
+done
-- 
1.7.6.1


      parent reply	other threads:[~2011-09-05 23:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-05 23:23 [PATCH 1/5] kvm tools: Remove unconditional warning in ivshmem Sasha Levin
2011-09-05 23:23 ` [PATCH 2/5] kvm tools: Fix 32bit warnings Sasha Levin
2011-09-05 23:23 ` [PATCH 3/5] kvm tools: Attach default 'root=' only if required Sasha Levin
2011-09-05 23:23 ` [PATCH 4/5] kvm tools: Teach 'run' to handle guestfs Sasha Levin
2011-09-06 13:37   ` Cyrill Gorcunov
2011-09-05 23:23 ` Sasha Levin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1315265035-9496-5-git-send-email-levinsasha928@gmail.com \
    --to=levinsasha928@gmail.com \
    --cc=asias.hejun@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox