# HG changeset patch # User Shinji KONO # Date 1352770575 -32400 # Node ID 25c14d1144a8d5e6ea7e15556c2a9762418ed3e4 # Parent 9ecd833b95703b4d1f6e151ef993fae88c508ccd add xml generation and define diff -r 9ecd833b9570 -r 25c14d1144a8 ie-virsh.c --- a/ie-virsh.c Tue Nov 13 07:59:17 2012 +0900 +++ b/ie-virsh.c Tue Nov 13 10:36:15 2012 +0900 @@ -28,6 +28,7 @@ #define list_command "/usr/bin/virsh list --all" #define start_command "start" #define stop_command "destroy" +#define define_command "define" #define dumpxml_command "dumpxml" #define VMNAME_MAX (512) @@ -41,8 +42,6 @@ /* Define global variables */ - - VMLISTPTR get_vmlist(regex_t *list_pattern) { @@ -78,6 +77,22 @@ return 0; } +int +check_name(const char *p) +{ + if (!p) return 1; + for(;*p;p++) { + char c = *p; + if (c<=' ') return 1; + if (('a'<=c && c<='z') || + ('0'<=c && c<='9') || + ('/'==c ) || + ('-'==c )) continue; + return 1; + } + return 0; +} + void usage() { @@ -121,10 +136,29 @@ setgid(0); setuid(0); + if (argc>=3) { + if ( strncmp(argv[1], "define", 6) == 0 ) { + if (regexec(pattern, argv[2], (size_t) 0, NULL, 0)) { + fprintf(stderr, "bad vmname for define\n"); + exit(0); + } + if (check_name(argv[2])) { + fprintf(stderr, "bad vmname for define\n"); + exit(0); + } + char exec[1024]; + strncpy("/usr/local/bin/newvm.py ",exec,1024); + strncpy(argv[2],exec,1000); + system(exec); + } + } + + VMLISTPTR vmlist = get_vmlist(pattern); if (argc>=3) { - if (check_vmlist_name(vmlist, argv[2])==0) { + if ( strncmp(argv[1], "define", 6) == 0 ) { + } else if (check_vmlist_name(vmlist, argv[2])==0) { fprintf(stderr, "bad vmname\n"); print_vmlist(vmlist); exit(0); @@ -153,6 +187,10 @@ if (execl(command, command, dumpxml_command, argv[2], NULL) < 0) { perror("Execl:"); } + } else if ( strncmp(argv[1], "define", 6) == 0 ) { + if (execl(command, command, define_command, argv[2], NULL) < 0) { + perror("Execl:"); + } } else { usage(); exit(1); diff -r 9ecd833b9570 -r 25c14d1144a8 newvm.py --- a/newvm.py Tue Nov 13 07:59:17 2012 +0900 +++ b/newvm.py Tue Nov 13 10:36:15 2012 +0900 @@ -9,6 +9,42 @@ else: import xml.etree.ElementTree as ET +# vm_name should be +# students/e10/e105730/01 +# teachers/kono/01 +# +def ie_check_name(name): + m=re.match('^students/e(\d\d)/e(\d\d57)\d\d/0[1-4]$',name) + if m is not None: + if m.group(1)==m.group(2): + return 1 + else: + return 0 + elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): + return 0 + else: + return 1 + +# make necessary sub directory +# /etc/libvirt/qemu/teachers +# /var/log/libvirt/qemu/teachers +# /var/run/libvirt/qemu/teachers + +def ie_mkdir1(name): + if not os.path.exists(name): + os.makedirs(name); + +def ie_mkdir(name): + m=re.match('^(students/e\d\d/e\d\d57\d\d)/0[1-4]$',name) + if m is None: + m=re.match('^(teachers/[-a-z0-9]+/0[1-4]$',name) + if m is not None: + dir=m.group(1) + ie_mkdir1('/media/fcs/'+dir) + ie_mkdir1('/etc/libvirt/qemu/'+dir) + ie_mkdir1('/var/log/libvirt/qemu/'+dir) + ie_mkdir1('/var/run/libvirt/qemu/'+dir) + ie_mkdir1('/var/lib/libvirt/qemu/'+dir) parser = OptionParser(); parser.add_option("-n", "--name", dest="name", @@ -24,6 +60,11 @@ config = ET.parse(options.config) vm_name = options.name +if ie_check_name(vm_name): + print "Bad vmname %s. Try students/e11/e115711/01 or teacher/kono/02" % vm_name + sys.exit(1) +ie_mkdir(vm_name) + name = config.find('name') name.text = vm_name uuid = config.find('uuid') @@ -40,7 +81,8 @@ if os.path.exists(vm_name + '.xml'): print "File %s.xml exists, abort" % vm_name sys.exit(1) -config.write(vm_name + '.xml') +config.write('/etc/libvirt/qemu/' + 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!" +