Mercurial > hg > Members > anatofuz > ie-virsh
annotate src/main.rs @ 11:9fac2c578228
...
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Thu, 29 Oct 2020 09:44:38 +0900 |
parents | 188bf8ab3e81 |
children | 0df4d067badb |
rev | line source |
---|---|
8 | 1 use clap::Clap; |
1 | 2 use nix; |
3 | 3 use std::process::Command; |
1 | 4 |
5 | 5 #[derive(Clap)] |
7 | 6 #[clap(version = "1.0", author = "AnaTofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")] |
5 | 7 struct Opts { |
8 #[clap(subcommand)] | |
9 subcmd: SubCommand, | |
10 } | |
11 | |
12 #[derive(Clap)] | |
13 enum SubCommand { | |
10 | 14 List(List), |
15 Undefine(UnDefine), | |
16 Define(Define), | |
17 Shutdown(Shutdown), | |
18 Destroy(Destroy), | |
19 Console(Console), | |
20 Start(Start), | |
21 Ttyconsole(TTyConsole), | |
11 | 22 VNCDisplay(VNCDisplay) |
23 //todo define-gdb dumpxml | |
5 | 24 } |
25 | |
7 | 26 /// define (but don't start) a domain from an template XML file |
5 | 27 #[derive(Clap)] |
28 struct Define { | |
29 name: String, | |
30 } | |
31 | |
11 | 32 // vncdisplay |
33 #[derive(Clap)] | |
34 struct VNCDisplay { | |
35 name_or_id: String, | |
36 } | |
37 | |
10 | 38 /// undefine a domain |
39 #[derive(Clap)] | |
40 struct UnDefine { | |
41 name: String, | |
42 } | |
43 | |
44 /// start a (previously defined) inactive domain | |
45 #[derive(Clap)] | |
46 struct Start { | |
47 name: String, | |
48 } | |
49 | |
50 /// tty console | |
51 #[derive(Clap)] | |
52 struct TTyConsole { | |
53 name_or_id: String, | |
54 } | |
55 | |
7 | 56 /// gracefully shutdown a domain |
5 | 57 #[derive(Clap)] |
58 struct Shutdown { | |
59 name: String, | |
60 } | |
61 | |
7 | 62 /// list domains |
63 #[derive(Clap)] | |
64 struct List {} | |
65 | |
10 | 66 /// destroy (stop) a domain |
67 #[derive(Clap)] | |
68 struct Destroy { | |
69 name: String, | |
70 } | |
71 | |
72 /// connect to the guest console | |
73 #[derive(Clap)] | |
74 struct Console { | |
75 name_or_id: String, | |
76 } | |
77 | |
78 struct ListDumpMsg { | |
7 | 79 info_msg: String, |
80 border_line: String, | |
81 } | |
82 | |
5 | 83 struct VM { |
84 id: u32, | |
85 name: String, | |
86 is_vm_running: bool, | |
87 } | |
88 | |
0 | 89 fn main() { |
7 | 90 let opts: Opts = Opts::parse(); |
91 | |
4 | 92 let uid = getuid(); |
93 let gid = getgid(); | |
3 | 94 let user_name = getlogin(uid); |
95 println!("uid: {} gid: {} name: {}", uid, gid, user_name); | |
5 | 96 |
7 | 97 match opts.subcmd { |
10 | 98 SubCommand::List(_) => { |
7 | 99 set_root_id(); |
9
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
100 let (ldump_msg, vm_list_strs) = list_command(user_name); |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
101 println!("{}\n{}", ldump_msg.info_msg, ldump_msg.border_line); |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
102 for vm_info in vm_list_strs { |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
103 println!("{}", vm_info); |
7 | 104 } |
5 | 105 } |
10 | 106 SubCommand::Define(name) => {} |
107 SubCommand::Shutdown(name) => {} | |
108 SubCommand::Console(name) => {} | |
109 | |
110 SubCommand::Destroy(name_or_id) => {} | |
111 _ => {} | |
5 | 112 } |
113 | |
114 //set_root_id(); | |
115 //list_command(user_name); | |
0 | 116 } |
1 | 117 |
2 | 118 fn getlogin(uid: u32) -> &'static str { |
119 use std::ffi::CStr; | |
4 | 120 let user_passwd = unsafe { nix::libc::getpwuid(uid) }; |
121 let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) }; | |
1 | 122 return c_str.to_str().unwrap(); |
123 } | |
124 | |
125 fn getuid() -> u32 { | |
126 let uid_struct = nix::unistd::getuid(); | |
3 | 127 return uid_struct.into(); |
1 | 128 } |
129 | |
130 fn getgid() -> u32 { | |
131 let gid_struct = nix::unistd::getgid(); | |
3 | 132 return gid_struct.into(); |
133 } | |
134 | |
10 | 135 fn list_command(user_name: &'static str) -> (ListDumpMsg, Vec<String>) { |
4 | 136 let output = Command::new("virsh") |
137 .arg("list") | |
138 .arg("--all") | |
139 .output() | |
140 .expect("failed to virsh"); | |
141 let virsh_list = String::from_utf8_lossy(&output.stdout); | |
142 let mut virsh_list = virsh_list.split("\n"); | |
7 | 143 |
4 | 144 let info_msg = virsh_list.next().unwrap(); |
145 let border_line = virsh_list.next().unwrap(); | |
10 | 146 let ldump_msg = ListDumpMsg { |
7 | 147 info_msg: String::from(info_msg), |
148 border_line: String::from(border_line), | |
149 }; | |
4 | 150 |
9
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
151 return ( |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
152 ldump_msg, |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
153 virsh_list |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
154 .filter(|&x| x.contains(user_name)) |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
155 .map(|x| x.to_string()) |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
156 .collect(), |
b89466455757
not parse when executing the list command
AnaTofuZ <anatofuz@gmail.com>
parents:
8
diff
changeset
|
157 ); |
1 | 158 } |
3 | 159 |
4 | 160 fn set_root_id() { |
3 | 161 let id = 0; |
162 match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) { | |
163 Err(err) => panic!("failed seteuid {}", err), | |
164 Ok(_) => {} | |
165 } | |
166 match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) { | |
167 Err(err) => panic!("failed setedid{}", err), | |
168 Ok(_) => {} | |
169 } | |
170 | |
171 match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) { | |
172 Err(err) => panic!("failed setuid {}", err), | |
173 Ok(_) => {} | |
174 } | |
175 | |
176 match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) { | |
177 Err(err) => panic!("failed setdid{}", err), | |
178 Ok(_) => {} | |
179 } | |
4 | 180 } |
5 | 181 |
6 | 182 fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel: bool) {} |