Mercurial > hg > Applications > virsh-wrapper
annotate ie-virsh.c @ 38:16840a2375ae
Adapt to CentOS7
author | atton |
---|---|
date | Fri, 06 Nov 2015 21:51:43 +0900 |
parents | 3b8858a63694 |
children | 6e385ccc40bf |
rev | line source |
---|---|
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdlib.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include <unistd.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 #include <stdio.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 #include <sys/types.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 #include <unistd.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 #include <signal.h> |
2 | 9 #include <string.h> |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 |
2 | 11 #include <sys/types.h> |
1 | 12 #include <regex.h> |
13 | |
21 | 14 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 |
29 | 16 #define command "/usr/bin/virsh" |
17 /* #define command "/usr/local/bin/virsh" */ | |
18 #define list_command "/usr/bin/virsh list --all" | |
19 /* #define list_command "/usr/local/bin/virsh list --all" */ | |
2 | 20 #define start_command "start" |
3 | 21 #define stop_command "destroy" |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
22 #define define_command "define" |
9 | 23 #define undefine_command "undefine" |
4
349bbbd3fbd5
added dumpxml command
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
24 #define dumpxml_command "dumpxml" |
22 | 25 #define console_command "console" |
32 | 26 #define debug_command "debug" |
1 | 27 |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
28 |
34 | 29 const char *guests[] = {}; |
30 const char *managers[] = {"atton"}; | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
31 const char students_sym[] = "students"; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
32 const char managers_sym[] = "managers"; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
33 const char guests_sym[] = "guests"; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
34 const char delimiter[] = "_"; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
35 |
32 | 36 static char bad_name[] = "Bad vmname. Try 01 or 02 ... 04\n"; |
9 | 37 |
35 | 38 const char newvm_command[] = "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/fedora23.xml -n "; |
39 | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
40 enum { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
41 NAME_LENGTH = 50, |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
42 VM_NAME_LENGTH = 50 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
43 }; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
44 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
45 enum { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
46 STUDENTS, |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
47 GUESTS, |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
48 MANAGERS |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
49 }; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
50 |
1 | 51 #define VMNAME_MAX (512) |
52 | |
53 typedef struct vmlist { | |
54 char name[VMNAME_MAX]; | |
55 struct vmlist *next; | |
56 } VMLIST, *VMLISTPTR; | |
57 | |
58 #define NEW(type) ((type*)malloc(sizeof(type))) | |
59 | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 /* Define global variables */ |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 |
22 | 62 VMLISTPTR |
37 | 63 get_vmlist(char *list_pattern_str) |
1 | 64 { |
37 | 65 regex_t *list_pattern = NEW(regex_t); |
66 if (regcomp(list_pattern, list_pattern_str, 0) != 0) { | |
67 exit(0); | |
68 } | |
69 | |
1 | 70 VMLISTPTR list = NEW(VMLIST); |
71 VMLISTPTR p = list ; | |
72 p->name[0] = 0; | |
73 p->next = 0; | |
74 FILE *fp = popen(list_command,"r"); | |
75 while(fgets(p->name,VMNAME_MAX,fp)!=NULL) { | |
76 if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue; | |
77 p->next = NEW(VMLIST); | |
78 p = p->next; | |
79 } | |
80 p->name[0] = 0; | |
81 pclose(fp); | |
37 | 82 regfree(list_pattern); |
1 | 83 return list; |
84 } | |
85 | |
36 | 86 void |
87 print_vmlist(VMLISTPTR list) | |
1 | 88 { |
89 for(;list && list->name[0]; list=list->next) { | |
90 fprintf(stdout, " %s\n",list->name); | |
91 } | |
92 } | |
93 | |
37 | 94 void |
95 check_vmlist_name(VMLISTPTR list, char * name) | |
1 | 96 { |
97 for(;list && list->name[0]; list=list->next) { | |
37 | 98 if (strstr(list->name, name)!=0) return; |
1 | 99 } |
37 | 100 |
101 fprintf(stderr, bad_name); | |
102 print_vmlist(list); | |
103 exit(1); | |
1 | 104 } |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105 |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
106 void |
29 | 107 change_delimiter_to_slash(char *name) |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
108 { |
29 | 109 long size = sizeof(char) * VM_NAME_LENGTH; |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
110 int i = 0; |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
111 for (; i < size; i++) { |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
112 if (name[i] == '_') { |
29 | 113 name[i] = '/'; |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
114 } |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
115 } |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
116 } |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
117 |
22 | 118 int |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
119 check_name(const char *p) |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
120 { |
21 | 121 if (!p) return 1; |
122 for(;*p;p++) { | |
123 char c = *p; | |
124 if (c<=' ') return 1; | |
125 if (('a'<=c && c<='z') || | |
126 ('0'<=c && c<='9') || | |
127 ('/'==c ) || | |
128 ('-'==c )) continue; | |
129 return 1; | |
130 printf("%c", c); | |
131 } | |
132 return 0; | |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
133 } |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
134 |
36 | 135 void |
3 | 136 usage() |
137 { | |
36 | 138 printf("Usage: COMMAND [define|list|start|destroy|dumpxml|undefine|console] [vm-name]\n"); |
32 | 139 printf(" vm-name should be 01 - 04\n"); |
3 | 140 } |
141 | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
142 int |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
143 check_user_name(const char *account_name) |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
144 { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
145 const char *regex = "[ek]([0-9]{6})"; |
36 | 146 |
28 | 147 regex_t *pattern = NEW(regex_t); |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
148 int ret = 1; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
149 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
150 if (regcomp(pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
151 exit(0); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
152 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
153 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
154 ret = regexec(pattern, account_name, (size_t) 0, NULL, 0); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
155 regfree(pattern); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
156 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
157 if (!ret){ |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
158 return STUDENTS; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
159 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
160 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
161 const int managers_num = sizeof(managers) / sizeof(managers[0]); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
162 int i = 0; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
163 for (; i < managers_num; i++) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
164 if (strncmp(account_name, managers[i], NAME_LENGTH) == 0) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
165 return MANAGERS; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
166 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
167 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
168 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
169 const int guests_num = sizeof(guests) / sizeof(guests[0]); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
170 int j = 0; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
171 for (; j < guests_num; j++) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
172 if (strncmp(account_name, guests[j], NAME_LENGTH) == 0) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
173 return GUESTS; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
174 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
175 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
176 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
177 return -1; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
178 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
179 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
180 void |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
181 bind_name(char *name, const char *first, const char *second) |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
182 { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
183 strncat(name, first, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
184 strncat(name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
185 strncat(name, second, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
186 strncat(name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
187 return; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
188 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
189 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
190 void |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
191 make_vm_name(char *vm_name, const int account_type, const char *account_name, const char *vm_num) |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
192 { |
36 | 193 const char *regex = "0[1-4]"; |
194 regex_t *num_pattern = NEW(regex_t); | |
195 | |
196 if (regcomp(num_pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) { | |
197 exit(0); | |
198 } | |
199 | |
200 int ret = regexec(num_pattern, vm_num, (size_t) 0, NULL, 0); | |
201 regfree(num_pattern); | |
202 | |
203 if (ret){ | |
204 fprintf(stderr, "This name is invalid number: %s\n", vm_num); | |
205 exit(0); | |
206 } | |
207 | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
208 switch(account_type) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
209 case STUDENTS: |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
210 strncat(vm_name, students_sym, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
211 strncat(vm_name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
212 strncat(vm_name, account_name, 3); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
213 strncat(vm_name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
214 strncat(vm_name, account_name, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
215 strncat(vm_name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
216 break; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
217 case GUESTS: |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
218 bind_name(vm_name, guests_sym, account_name); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
219 break; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
220 case MANAGERS: |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
221 bind_name(vm_name, managers_sym, account_name); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
222 break; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
223 default : |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
224 fprintf(stderr, "Error: no registered type name."); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
225 return; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
226 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
227 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
228 strncat(vm_name, vm_num, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
229 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
230 |
37 | 231 void |
232 create_new_vm(const char const *name, const char const *vm_name, char* xml_name){ | |
233 if (check_name(name)) { | |
234 fprintf(stderr, bad_name); | |
235 exit(0); | |
236 } | |
237 char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); | |
238 strncpy(vm_path, vm_name, VM_NAME_LENGTH); | |
239 change_delimiter_to_slash(vm_path); | |
240 char exec[1024]; | |
241 strncpy(exec, newvm_command ,900); | |
242 strncat(exec, vm_path,1000); | |
243 fprintf(stdout, "excuting %s\n",exec ); | |
244 printf("%s\n", vm_path); | |
245 system(exec); | |
246 strncpy(xml_name, vm_path,900); | |
247 strncat(xml_name,".xml",1000); | |
248 free(vm_path); | |
249 } | |
250 | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
251 /* main(int argc, char **argv) - main process loop */ |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
252 |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
253 int main(int argc, char **argv) |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
254 { |
35 | 255 /* Set euid and egid to actual user */ |
256 int uid = getuid(); | |
257 int gid = getgid(); | |
258 char *name = getlogin(); | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
259 |
21 | 260 printf("uid %d gid %d name %s\n", uid,gid,name); |
261 setegid(getgid()); | |
262 seteuid(getuid()); | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
263 |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
264 int account_type = check_user_name(name); |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
265 if (account_type < 0) { |
30
2888c7ec9013
can't define vm name is not number.
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
266 fprintf(stderr, "Parmission denied. :%s\n", name); |
37 | 267 exit(1); |
21 | 268 } |
1 | 269 |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
270 |
21 | 271 /* Confirm user is in GROUP(999) group */ |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
272 |
21 | 273 /* |
274 if ( gid != 999 ) { | |
275 printf("User Not Authorized! Exiting...\n"); | |
276 exit(1); | |
277 } | |
278 */ | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
279 |
35 | 280 if (argc<2) { |
281 usage(); | |
282 exit(0); | |
283 } | |
284 | |
21 | 285 /* Set uid, gid, euid and egid to root */ |
286 | |
287 setegid(0); | |
288 seteuid(0); | |
289 setgid(0); | |
290 setuid(0); | |
37 | 291 |
29 | 292 char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); |
293 vm_name[0] = '\0'; | |
37 | 294 if ((argc > 2) && (strncmp(argv[1], list_command, 4) != 0)) { make_vm_name(vm_name, account_type, name, argv[2]); } |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
295 |
37 | 296 VMLISTPTR vmlist = get_vmlist(name); |
1 | 297 |
36 | 298 |
299 /* Check argv for proper arguments and run | |
21 | 300 * the corresponding script, if invoked. |
301 */ | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
302 |
22 | 303 if (argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { |
21 | 304 print_vmlist(vmlist); |
22 | 305 } else if (strncmp(argv[1], start_command, 5) == 0) { |
37 | 306 if (argc<3) {usage(); exit(1);} |
307 check_vmlist_name(vmlist, argv[2]); | |
28 | 308 if (execl(command, command, start_command, vm_name, NULL) < 0) { |
21 | 309 perror("Execl:"); |
310 } | |
22 | 311 } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { |
37 | 312 if (argc<3) {usage(); exit(1);} |
313 check_vmlist_name(vmlist, argv[2]); | |
28 | 314 if (execl(command, command, stop_command, vm_name, NULL) < 0) { |
21 | 315 perror("Execl:"); |
316 } | |
22 | 317 } else if ( strncmp(argv[1], dumpxml_command, 7) == 0 ) { |
37 | 318 if (argc<3) {usage(); exit(1);} |
319 check_vmlist_name(vmlist, argv[2]); | |
28 | 320 if (execl(command, command, dumpxml_command, vm_name, NULL) < 0) { |
21 | 321 perror("Execl:"); |
322 } | |
22 | 323 } else if ( strncmp(argv[1], console_command, 8) == 0 ) { |
37 | 324 if (argc<3) {usage(); exit(1);} |
325 check_vmlist_name(vmlist, argv[2]); | |
28 | 326 if (execl(command, command, console_command, vm_name, NULL) < 0) { |
22 | 327 perror("Execl:"); |
328 } | |
329 } else if (strncmp(argv[1], define_command, 6) == 0 ) { | |
37 | 330 if (argc<3) {usage(); exit(1);} |
331 char xml_name[1024]; | |
332 xml_name[0] = 0; | |
333 create_new_vm(argv[2], vm_name, xml_name); | |
29 | 334 /* chdir("/usr/local/etc/libvirt/qemu"); */ |
335 chdir("/etc/libvirt/qemu"); | |
37 | 336 if (execl(command, command, define_command, xml_name, NULL) < 0) { |
21 | 337 perror("Execl:"); |
338 } | |
22 | 339 } else if ( strncmp(argv[1], undefine_command, 8) == 0 ) { |
29 | 340 /* chdir("/usr/local/etc/libvirt/qemu"); */ |
341 chdir("/etc/libvirt/qemu"); | |
28 | 342 if (execl(command, command, undefine_command, vm_name, NULL) < 0) { |
21 | 343 perror("Execl:"); |
344 } | |
32 | 345 } else if (strncmp(argv[1], debug_command, 5) == 0) { |
346 char *user_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); | |
347 strncpy(user_name, "\0", 1); | |
348 make_vm_name(user_name, account_type, name, debug_command); | |
349 | |
350 char str_uid[1024]; | |
351 sprintf(str_uid, "%d", uid); | |
352 | |
353 char debug_exec[1026]; | |
354 sprintf(debug_exec, "/usr/local/bin/debugvm.py --name %s --uid %s --config %s", user_name, str_uid, "/etc/libvirt/qemu/debugvm.xml"); | |
355 free(user_name); | |
356 | |
357 printf("debug_exec::%s \n", debug_exec); | |
358 | |
359 char gdb_exec[1024]; | |
360 strncpy(gdb_exec, "/usr/bin/gdb",900); | |
361 // strncat(gdb_exec, " /usr/lib/debug/lib/modules/3.17.4-200.fc20.x86_64/vmlinux", 900); | |
362 // system(gdb_exec); | |
21 | 363 } else { |
37 | 364 printf("%s is invalid command.\n", argv[1]); |
21 | 365 usage(); |
366 exit(1); | |
367 } | |
29 | 368 free(vm_name); |
21 | 369 exit(0); |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
370 } |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
371 |
1 | 372 /* end */ |