changeset 3:203445eb53a8

add xml generator. fitering is not working.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 12 Nov 2012 12:25:04 +0900
parents 1a35a6cb23dd
children 349bbbd3fbd5
files ie-virsh.c newvm.py
diffstat 2 files changed, 60 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ie-virsh.c	Mon Nov 12 11:51:26 2012 +0900
+++ b/ie-virsh.c	Mon Nov 12 12:25:04 2012 +0900
@@ -27,7 +27,7 @@
 #define command "/usr/bin/virsh"
 #define list_command "/usr/bin/virsh list --all"
 #define start_command "start"
-#define stop_command "shutdown"
+#define stop_command "destroy"
 
 #define VMNAME_MAX (512)
 
@@ -77,6 +77,12 @@
     return 0;
 }
 
+void 
+usage()
+{
+  printf("Usage: COMMAND [list|start|stop] [vm-name]\n");
+}
+
 /* main(int argc, char **argv) - main process loop */
 
 int main(int argc, char **argv)
@@ -116,19 +122,22 @@
 
  VMLISTPTR vmlist = get_vmlist(pattern);
 
- if (argc==2) {
+ if (argc==3) {
     if (check_vmlist_name(vmlist, argv[2])!=0) {
         fprintf(stderr, "bad vmname\n");
         print_vmlist(vmlist);
         exit(0);
     }
+ } else if (argc<2) {
+        usage();
+        exit(0);
  }
 
 /* Check argv for proper arguments and run 
  * the corresponding script, if invoked.
  */
 
- if ( strncmp(argv[1], "list", 4) == 0 ) {
+ if ( argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) {
     print_vmlist(vmlist);
  } else if ( strncmp(argv[1], "start", 5) == 0 ) {
   if (execl(command, command, start_command, argv[2], NULL) < 0) {
@@ -139,8 +148,8 @@
    perror("Execl:");
   }
  } else {
-  printf("Usage: COMMAND [list|start|stop] [vm-name]\n");
-  exit(1);
+    usage();
+    exit(1);
  }
  exit(0);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/newvm.py	Mon Nov 12 12:25:04 2012 +0900
@@ -0,0 +1,46 @@
+#!/usr/bin/python
+# Used to create a vm from template
+# By Curu Wong contact: prinbra(at)gmail(dot)com
+import sys,os
+from optparse import OptionParser
+from virtinst.util import *
+if sys.version_info < (2,5):
+        import lxml.etree as ET
+else:
+        import xml.etree.ElementTree as ET
+ 
+ 
+parser = OptionParser();
+parser.add_option("-n", "--name", dest="name",
+        help="VM name");
+parser.add_option("-c", "--config", dest="config",
+        help="Template VM XML config file");
+ 
+(options, args) = parser.parse_args();
+ 
+if not options.name or not options.config:
+        print "Usage %s -n name -c template_xml" % sys.argv[0]
+        sys.exit(1)
+ 
+config = ET.parse(options.config)
+vm_name = options.name
+name = config.find('name')
+name.text = vm_name
+uuid = config.find('uuid')
+uuid.text = uuidToString(randomUUID())
+mac = config.find('devices/interface/mac')
+mac.attrib['address'] = randomMAC(type='qemu')
+disk = config.find('devices/disk/source')
+disk_old = disk.attrib['file']
+disk_path = os.path.dirname(disk_old)
+disk_ext = os.path.splitext(disk_old)[1]
+disk_image = disk_path + '/' + vm_name + disk_ext
+disk.attrib['file'] = disk_image
+ 
+if os.path.exists(vm_name + '.xml'):
+        print "File %s.xml exists, abort" % vm_name
+        sys.exit(1)
+config.write(vm_name + '.xml')
+print "Created vm config file %s.xml" % vm_name
+print "Use disk image %s, you must create it from the template disk: %s" % (disk_image, disk_old)
+print "Done!"