Skip to contentSkip to conversation

kvm tools: Remove unconditional warning in ivshmem

PATCH In review Sasha Levin 2 participants 6 messages Sep 5, 2011 – Sep 6, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 kvm tools: Remove unconditional warning in ivshmem

This patch prevents printing a warning if ivshmem device wasn't requested.

---
 tools/kvm/hw/pci-shmem.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
tools/kvm/hw/pci-shmem.c +1 -3
index 295f5cd..f820164 100644
@@ -222,10 +222,8 @@ int pci_shmem__init(struct kvm *kvm)
 	u8 dev, line, pin;
 	char *mem;
 
-	if (shmem_region == 0) {
-		pr_warning("pci_shmem_init: memory region not registered\n");
+	if (shmem_region == 0)
 		return 0;
-	}
 
 	/* Register good old INTx */
 	if (irq__register_device(PCI_DEVICE_ID_PCI_SHMEM, &dev, &pin, &line) < 0)
Signature · 1 line
1.7.6.1
#1 lore raw

PATCH 2/5 kvm tools: Fix 32bit warnings

---
 tools/kvm/hw/pci-shmem.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/kvm/hw/pci-shmem.c +1 -1
index f820164..2907a66 100644
@@ -205,7 +205,7 @@ static void *setup_shmem(const char *key, size_t len, int creating)
 	if (creating) {
 		rtn = ftruncate(fd, (off_t) len);
 		if (rtn < 0)
-			pr_warning("Can't ftruncate(fd,%ld)\n", len);
+			pr_warning("Can't ftruncate(fd,%zu)\n", len);
 	}
 	mem = mmap(NULL, len,
 		   PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd, 0);
Signature · 1 line
1.7.6.1
#2 lore raw

PATCH 3/5 kvm tools: Attach default 'root=' only if required

In reply to Sasha Levin

This patch fixes a case where the 'root=' parameter was passed twice when using virtio-9p root mode.

---
 tools/kvm/builtin-run.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
tools/kvm/builtin-run.c +3 -4
index b9efde2..ef1a358 100644
@@ -739,11 +739,10 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 			strlcat(real_cmdline, " init=/bin/sh ", sizeof(real_cmdline));
 	}
 
-	if (!strstr(real_cmdline, "root="))
-		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
-
 	if (using_rootfs)
-		strcat(real_cmdline, " root=/dev/root rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
+		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
+	else if (!strstr(real_cmdline, "root="))
+		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
 
 	if (image_count) {
 		kvm->nr_disks = image_count;
Signature · 1 line
1.7.6.1
#3 lore raw

PATCH 4/5 kvm tools: Teach 'run' to handle guestfs

In reply to Sasha Levin

This patch allows to run previously created guestfs by simply specifying it with the '-d' parameter.

This allows running guestfs which were created before using:

kvm setup -n [name]

---
 tools/kvm/builtin-run.c     |   24 ++++++++++++++++++++++--
 tools/kvm/include/kvm/kvm.h |    3 +++
 tools/kvm/kvm.c             |    2 --
 3 files changed, 25 insertions(+), 4 deletions(-)
tools/kvm/builtin-run.c +22 -2
index ef1a358..725c23c 100644
@@ -85,6 +85,7 @@ static bool vnc;
 static bool sdl;
 static bool balloon;
 static bool using_rootfs;
+static bool custom_rootfs;
 extern bool ioport_debug;
 extern int  active_console;
 extern int  debug_iodelay;
@@ -103,6 +104,7 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)
 {
 	char *sep;
 	struct stat st;
+	char path[PATH_MAX];
 
 	if (stat(arg, &st) == 0 &&
 	    S_ISDIR(st.st_mode)) {
@@ -115,6 +117,21 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)
 		return 0;
 	}
 
+	snprintf(path, PATH_MAX, "%s%s%s", HOME_DIR, KVM_PID_FILE_PATH, arg);
+
+	if (stat(path, &st) == 0 &&
+	    S_ISDIR(st.st_mode)) {
+		char tmp[PATH_MAX];
+
+		if (realpath(path, tmp) == 0 ||
+		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
+			die("Unable to initialize virtio 9p");
+		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
+			die("Unable to initialize virtio 9p");
+		using_rootfs = custom_rootfs = 1;
+		return 0;
+	}
+
 	if (image_count >= MAX_DISK_IMAGES)
 		die("Currently only 4 images are supported");
 
@@ -739,10 +756,13 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 			strlcat(real_cmdline, " init=/bin/sh ", sizeof(real_cmdline));
 	}
 
-	if (using_rootfs)
+	if (using_rootfs) {
 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
-	else if (!strstr(real_cmdline, "root="))
+		if (custom_rootfs)
+			strcat(real_cmdline, " init=/virt/init");
+	} else if (!strstr(real_cmdline, "root=")) {
 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
+	}
 
 	if (image_count) {
 		kvm->nr_disks = image_count;
tools/kvm/include/kvm/kvm.h +3 -0
index d471cee..bb40c4c 100644
@@ -24,6 +24,9 @@
 #define SIGKVMRESUME		(SIGRTMIN + 5)
 #define SIGKVMMEMSTAT		(SIGRTMIN + 6)
 
+#define KVM_PID_FILE_PATH	"/.kvm-tools/"
+#define HOME_DIR		getenv("HOME")
+
 struct kvm {
 	int			sys_fd;		/* For system ioctls(), i.e. /dev/kvm */
 	int			vm_fd;		/* For VM ioctls() */
tools/kvm/kvm.c +0 -2
index bbfdea2..199c9b9 100644
@@ -32,8 +32,6 @@
 #include <dirent.h>
 
 #define DEFINE_KVM_EXIT_REASON(reason) [reason] = #reason
-#define KVM_PID_FILE_PATH	"/.kvm-tools/"
-#define HOME_DIR		getenv("HOME")
 
 const char *kvm_exit_reasons[] = {
 	DEFINE_KVM_EXIT_REASON(KVM_EXIT_UNKNOWN),
Signature · 1 line
1.7.6.1
#4 lore raw

Re: [PATCH 4/5] kvm tools: Teach 'run' to handle guestfs

Quoted 9 lines · Sasha Levin

This patch allows to run previously created guestfs by simply specifying it with the '-d' parameter.

This allows running guestfs which were created before using:

kvm setup -n [name]

---

...

tools/kvm/builtin-run.c static int img_name_parser(const struct option *opt, const char *arg, int unset) · line 108

Quoted 6 lines
tools/kvm/builtin-run.c +1 -0
@@ -103,6 +104,7 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)
 {
 	char *sep;
 	struct stat st;
+	char path[PATH_MAX];
 

Hi Sasha, the whole series looks good to me, thanks a lot!
The only thing which was always bothering me -- is PATH_MAX on the stack. As far as I remember it might be up to 4K which is not that good ;) Probably we might move it somewhere into .bss? (in some patches on top)

Cyrill

#5 lore raw

PATCH 5/5 kvm tools: Add guestfs network autoconfiguration

In reply to Sasha Levin

Add a script to automatically configure networking in the guest.

---
 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
tools/kvm/builtin-setup.c +14 -0
index 7587476..f7588dd 100644
@@ -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);
 }
 
tools/kvm/guest/init.c +4 -0
index 837acfb..7733026 100644
@@ -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");
tools/kvm/guest/setnet.sh new file +22 -0
new file mode 100755
index 0000000..3da9c22
@@ -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
Signature · 1 line
1.7.6.1