changeset 46:6b1dddd65d7c

Generate kernel debug port randomly between 10000 to 30000
author atton
date Fri, 15 Jan 2016 16:26:43 +0900
parents 7f6c5d7355bc
children 96661b8aaf68
files ie-virsh.c newvm.py
diffstat 2 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ie-virsh.c	Fri Jan 15 15:22:10 2016 +0900
+++ b/ie-virsh.c	Fri Jan 15 16:26:43 2016 +0900
@@ -10,6 +10,10 @@
 #include <string.h>
 #include <regex.h>
 #include <pwd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <time.h>
 
 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
 
@@ -231,6 +235,31 @@
     strncat(vm_name, vm_num, VM_NAME_LENGTH);
 }
 
+int
+get_kernel_debug_port(void){
+    struct sockaddr_in addr;
+    memset(&addr, 0, sizeof(addr));
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = htonl(INADDR_ANY);
+    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
+
+    int i;
+    srand(time(NULL));
+    for (i = 0; i < 10; i++) {
+        addr.sin_port = htons((rand() % 20000) + 10000);
+
+        if ((bind(socket_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr) ) == 0) && (listen(socket_fd, 1) == 0)) {
+            printf("Port %d was allocated for your kernel debug.\n", ntohs(addr.sin_port));
+            close(socket_fd);
+            return ntohs(addr.sin_port);
+        } else {
+            printf("Port %d was used. Trying another port...\n", ntohs(addr.sin_port));
+        }
+    }
+    printf("Cannot allocate debug port.\n");
+    exit(EXIT_FAILURE);
+}
+
 void
 create_new_vm(const char const *name, const char const *vm_name, char* xml_name, bool debug_kernel){
     if (check_name(name)) {
@@ -242,7 +271,7 @@
     change_delimiter_to_slash(vm_path);
     char exec[1024];
     if (debug_kernel) {
-        snprintf(exec, 1024, "%s %s -d true", newvm_command, vm_path);
+        snprintf(exec, 1024, "%s %s -d %d", newvm_command, vm_path, get_kernel_debug_port());
     } else {
         snprintf(exec, 1024, "%s %s", newvm_command, vm_path);
     }
--- a/newvm.py	Fri Jan 15 15:22:10 2016 +0900
+++ b/newvm.py	Fri Jan 15 16:26:43 2016 +0900
@@ -76,9 +76,6 @@
             random.randint(0x00, 0xff) ]
     return ':'.join(map(lambda x: "%02x" % x, mac))
 
-def gen_debug_port():
-    return 10000
-
 parser = OptionParser();
 parser.add_option("-n", "--name", dest="name",
         help="VM name");
@@ -86,7 +83,7 @@
         help="Template VM XML config file");
 parser.add_option("-i", "--iso", dest="iso",
         help="When boot VM from ISO");
-parser.add_option("-d", "--debug", dest="debug",
+parser.add_option("-d", "--debug", dest="debug", type="int",
         help="Kernel debug mode");
 
 (options, args) = parser.parse_args();
@@ -124,7 +121,7 @@
     domain.attrib['xmlns:qemu'] = "http://libvirt.org/schemas/domain/qemu/1.0"
     qemu_elem                   = ET.Element('qemu:commandline')
     ET.SubElement(qemu_elem, 'qemu:arg', {'value' : '-S'})
-    ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('-s %d' % gen_debug_port())})
+    ET.SubElement(qemu_elem, 'qemu:arg', {'value' : ('-s %d' % options.debug)})
     domain.append(qemu_elem)
 
 if os.path.exists(vm_name + '.xml'):