[pve-devel] [PATCH 2/2] added kvm_wrapper

Stefan Priebe s.priebe at profihost.ag
Wed Apr 1 11:24:52 CEST 2015


Signed-off-by: Stefan Priebe <s.priebe at profihost.ag>
---
 debian/control |    2 +-
 debian/rules   |    2 ++
 kvm_wrapper.pl |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100755 kvm_wrapper.pl

diff --git a/debian/control b/debian/control
index 4ca1687..2633ab9 100644
--- a/debian/control
+++ b/debian/control
@@ -7,7 +7,7 @@ Standards-Version: 3.7.2
 
 Package: pve-qemu-kvm
 Architecture: any
-Depends: iproute, bridge-utils, python, libsdl1.2debian, libaio1, libuuid1, ceph-common (>= 0.48), libiscsi4 (>= 1.12.0), pve-libspice-server1 (>= 0.12.3), ${shlibs:Depends}, ${misc:Depends}, libusbredirparser1 (>= 0.6-2), glusterfs-common (>= 3.5.2-1), libusb-1.0-0 (>= 1.0.17-1), numactl
+Depends: iproute, bridge-utils, python, libsdl1.2debian, libaio1, libuuid1, ceph-common (>= 0.48), libiscsi4 (>= 1.12.0), pve-libspice-server1 (>= 0.12.3), ${shlibs:Depends}, ${misc:Depends}, libusbredirparser1 (>= 0.6-2), glusterfs-common (>= 3.5.2-1), libusb-1.0-0 (>= 1.0.17-1), numactl, libbsd-resource-perl, libfile-slurp-perl
 Conflicts: qemu, qemu-kvm, kvm, pve-kvm, pve-qemu-kvm-2.6.18
 Replaces: pve-kvm, pve-qemu-kvm-2.6.18
 Description: Full virtualization on x86 hardware
diff --git a/debian/rules b/debian/rules
index 2bf49eb..1b99fbc 100755
--- a/debian/rules
+++ b/debian/rules
@@ -80,6 +80,8 @@ install: build
 	install -m 0755 scripts/kvm/kvm_stat $(destdir)/usr/bin/
 	install -s -m 0755 vma $(destdir)/usr/bin/
 
+	install -m 0755 $(CURDIR)/../kvm_wrapper.pl $(destdir)/usr/bin/
+
 	install -D -m 0755 $(CURDIR)/debian/kvm-ifup $(destdir)/etc/kvm/kvm-ifup
 	install -D -m 0755 $(CURDIR)/debian/kvm-ifdown $(destdir)/etc/kvm/kvm-ifdown
 
diff --git a/kvm_wrapper.pl b/kvm_wrapper.pl
new file mode 100755
index 0000000..c9c19e2
--- /dev/null
+++ b/kvm_wrapper.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use POSIX ":sys_wait_h";
+use Time::HiRes qw(sleep);
+use Data::Dumper;
+use File::Slurp;
+use BSD::Resource;
+
+my $idfound = 0;
+my $kvmid   = 0;
+my $daemon  = 0;
+my @args    = ();
+foreach my $arg ( @ARGV ) {
+    $kvmid = $arg if $idfound && !$kvmid;
+    $idfound = 1 if $arg eq "-id";
+
+    if ( $arg eq "-daemonize" ) {
+        $daemon = 1;
+        next;
+    }
+    push( @args, $arg );
+}
+
+# ignore errors - who cares
+setrlimit( RLIMIT_CORE, -1, -1 );
+
+if ( $daemon ) {
+    my $childpid = fork();
+
+    # parent
+    if ( $childpid > 0 ) {
+
+        # wait 3s * 10 => 30
+        my $res;
+        my $exitcode = 0;
+        for ( 1 .. 30 ) {
+            $res = waitpid( $childpid, WNOHANG );
+            if ( $res == -1 ) {
+                $exitcode = $? >> 8;
+                print STDERR "$0: some strange error has happened!\n";
+                print STDERR read_file( "/tmp/kvm.$kvmid.log" );
+                last;
+            }
+            elsif ( $res ) {
+                $exitcode = $? >> 8;
+                print STDERR read_file( "/tmp/kvm.$kvmid.log" );
+                last;
+            }
+            sleep( 0.1 );
+        }
+
+        close( STDIN );
+        close( STDOUT );
+        close( STDERR );
+
+        # could wait 1-2s fir child and read exitcode...
+        exit( $exitcode );
+    }
+
+    POSIX::setsid();
+
+    close( STDIN );
+    close( STDOUT );
+    close( STDERR );
+
+    open( STDIN,  "</dev/null" );
+    open( STDOUT, ">/tmp/kvm.$kvmid.log" );
+    chmod 0640, "/tmp/kvm.$kvmid.log";
+    open( STDERR, ">>&STDOUT" );
+
+    print Dumper( @args );
+    print "-" x 80;
+    print "\n";
+}
+
+select STDERR;
+$| = 1;
+select STDOUT;
+$| = 1;
+
+exec( '/usr/bin/kvm', @args );
-- 
1.7.10.4



More information about the pve-devel mailing list