Mercurial > hg > Applications > virsh-wrapper
changeset 29:cfa197bad124
fix bug
author | taiki <taiki@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 26 Aug 2014 06:51:06 -1000 |
parents | 554085aec682 |
children | 2888c7ec9013 |
files | ie-virsh.c newvm.py |
diffstat | 2 files changed, 52 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/ie-virsh.c Mon Aug 18 16:45:32 2014 -1000 +++ b/ie-virsh.c Tue Aug 26 06:51:06 2014 -1000 @@ -13,10 +13,10 @@ /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ -/* #define command "/usr/bin/virsh" */ -#define command "/usr/local/bin/virsh" -/* #define list_command "/usr/bin/virsh list --all" */ -#define list_command "/usr/local/bin/virsh list --all" +#define command "/usr/bin/virsh" +/* #define command "/usr/local/bin/virsh" */ +#define list_command "/usr/bin/virsh list --all" +/* #define list_command "/usr/local/bin/virsh list --all" */ #define start_command "start" #define stop_command "destroy" #define define_command "define" @@ -92,20 +92,17 @@ } void -change_delimiter(char *name) +change_delimiter_to_slash(char *name) { - long size = sizeof(name); + long size = sizeof(char) * VM_NAME_LENGTH; int i = 0; for (; i < size; i++) { if (name[i] == '_') { - name [i] = '/'; + name[i] = '/'; } } - } - - int check_name(const char *p) { @@ -127,7 +124,7 @@ usage() { printf("Usage: COMMAND [define|list|start|destroy|xmldump|undefine|console] [vm-name]\n"); - printf(" vm-name should be students/e11/e115711/01 or teachers/kono/02\n"); + printf(" vm-name should be 01 - 02\n"); } int @@ -202,8 +199,6 @@ } strncat(vm_name, vm_num, VM_NAME_LENGTH); - - printf("%s\n", vm_name); } /* main(int argc, char **argv) - main process loop */ @@ -228,9 +223,6 @@ fprintf(stderr, "Parmission denied. :%s", name); } - char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); - - make_vm_name(vm_name, account_type, name, argv[1]); /* Confirm user is in GROUP(999) group */ @@ -248,6 +240,12 @@ setgid(0); setuid(0); + char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); + vm_name[0] = '\0'; + if (strncmp(argv[1], "list", 4) != 0) { + make_vm_name(vm_name, account_type, name, argv[2]); + } + if (argc>=3) { if ( strncmp(argv[1], "define", 6) == 0 ) { if (check_name(argv[2])) { @@ -256,17 +254,16 @@ } char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); strncpy(vm_path, vm_name, VM_NAME_LENGTH); - change_delimiter(vm_path); + change_delimiter_to_slash(vm_path); char exec[1024]; - // strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora16.xml -n ",900); - strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora19.xml -n ",900); + strncpy(exec, "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora20.xml -n ",900); strncat(exec, vm_path,1000); fprintf(stdout, "excuting %s\n",exec ); system(exec); + free(vm_path); } } - regex_t *pattern = NEW(regex_t); if (regcomp(pattern, name, 0) != 0) { exit(0); @@ -276,11 +273,17 @@ char name_xml[1024]; name_xml[0] = 0; + if (argc>=3) { if ( strncmp(argv[1], "define", 6) == 0 ) { - change_delimiter(vm_name); - strncpy(name_xml, vm_name,900); + char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); + vm_path[0] = '\0'; + strncpy(vm_path, vm_name, VM_NAME_LENGTH); + change_delimiter_to_slash(vm_path); + printf("%s\n", vm_path); + strncpy(name_xml, vm_path,900); strncat(name_xml,".xml",1000); + free(vm_path); } else if (check_vmlist_name(vmlist, argv[2])==0) { fprintf(stderr, bad_name); print_vmlist(vmlist); @@ -315,12 +318,14 @@ perror("Execl:"); } } else if (strncmp(argv[1], define_command, 6) == 0 ) { - chdir("/usr/local/etc/libvirt/qemu"); + /* chdir("/usr/local/etc/libvirt/qemu"); */ + chdir("/etc/libvirt/qemu"); if (execl(command, command, define_command, name_xml, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], undefine_command, 8) == 0 ) { - chdir("/usr/local/etc/libvirt/qemu"); + /* chdir("/usr/local/etc/libvirt/qemu"); */ + chdir("/etc/libvirt/qemu"); if (execl(command, command, undefine_command, vm_name, NULL) < 0) { perror("Execl:"); } @@ -328,6 +333,7 @@ usage(); exit(1); } + free(vm_name); exit(0); }
--- a/newvm.py Mon Aug 18 16:45:32 2014 -1000 +++ b/newvm.py Tue Aug 26 06:51:06 2014 -1000 @@ -1,7 +1,7 @@ #!/usr/bin/python # Used to create a vm from template # By Curu Wong contact: prinbra(at)gmail(dot)com -import sys,os +import sys,os,string from optparse import OptionParser from virtinst.util import * @@ -13,8 +13,8 @@ # vm_name should be # students/e10/e105730/01 # teachers/kono/01 -# manager/name/01 -# guest/name/01 +# managers/name/01 +# guests/name/01 # bad name returns 1 def ie_check_name(name): m=re.match('^students/e(\d\d)/e(\d\d)5\d\d\d/0[1-4]$',name) @@ -25,9 +25,9 @@ return 1 elif re.match('^teachers/[-a-z0-9]+/0[1-4]$',name): return 0 - elif re.match('^manager/[-a-z0-9]+/0[1-4]$',name): + elif re.match('^managers/[-a-z0-9]+/0[1-4]$',name): return 0 - elif re.match('^guest/[-a-z0-9]+/0[1-4]$',name): + elif re.match('^guests/[-a-z0-9]+/0[1-4]$',name): return 0 else: return 1 @@ -46,16 +46,20 @@ if m is None: m=re.match('^(teachers/[-a-z0-9]+)/0[1-4]$',name) if m is None: - m=re.match('^(manager/[-a-z0-9]+)/0[1-4]$',name) + m=re.match('^(managers/[-a-z0-9]+)/0[1-4]$',name) if m is None: - m=re.match('^(guest/[-a-z0-9]+)/0[1-4]$',name) + m=re.match('^(guests/[-a-z0-9]+)/0[1-4]$',name) if m is not None: dir=m.group(1) ie_mkdir1('/media/fcs/'+dir) - ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir) - ie_mkdir1('/usr/local/var/log/libvirt/qemu/'+dir) - ie_mkdir1('/usr/local/var/run/libvirt/qemu/'+dir) - ie_mkdir1('/usr/local/var/lib/libvirt/qemu/'+dir) +# ie_mkdir1('/usr/local/etc/libvirt/qemu/'+dir) +# ie_mkdir1('/usr/local/var/log/libvirt/qemu/'+dir) +# ie_mkdir1('/usr/local/var/run/libvirt/qemu/'+dir) +# ie_mkdir1('/usr/local/var/lib/libvirt/qemu/'+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) os.system("/bin/chown "+os.getlogin()+" /media/fcs/"+dir) parser = OptionParser(); @@ -77,12 +81,15 @@ vm_name = options.name print(options.config) if ie_check_name(vm_name): - print "Bad vmname %s. Try students/e11/e115711/01 or teachers/kono/02" % vm_name + print "Bad vmname %s. Try 01 - 04" % vm_name sys.exit(1) ie_mkdir(vm_name) name = config.find('name') -name.text = vm_name + +new_name = vm_name.translate(string.maketrans('/','_')) + +name.text = new_name uuid = config.find('uuid') uuid.text = uuidToString(randomUUID()) mac = config.find('devices/interface/mac') @@ -97,7 +104,8 @@ if os.path.exists(vm_name + '.xml'): print "File %s.xml exists, abort" % vm_name sys.exit(1) -config.write('/usr/local/etc/libvirt/qemu/' + vm_name + '.xml') +# config.write('/usr/local/etc/libvirt/qemu/' + 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!"