-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.rs
186 lines (165 loc) · 4.98 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2021 Red Hat, Inc.
// SPDX-License-Identifier: Apache-2.0
use std::collections::HashMap;
#[cfg(target_os = "macos")]
use std::fs::File;
#[cfg(target_os = "macos")]
use std::io::{self, Read, Write};
use crate::commands::{ChangeVmCmd, ConfigCmd, CreateCmd, DeleteCmd, ListCmd, StartCmd};
use clap::{Parser, Subcommand};
use serde_derive::{Deserialize, Serialize};
#[cfg(target_os = "macos")]
use text_io::read;
#[allow(unused)]
mod bindings;
mod commands;
mod utils;
const APP_NAME: &str = "krunvm";
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct VmConfig {
name: String,
cpus: u32,
mem: u32,
container: String,
workdir: String,
dns: String,
mapped_volumes: HashMap<String, String>,
mapped_ports: HashMap<String, String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct KrunvmConfig {
version: u8,
default_cpus: u32,
default_mem: u32,
default_dns: String,
storage_volume: String,
vmconfig_map: HashMap<String, VmConfig>,
}
impl Default for KrunvmConfig {
fn default() -> KrunvmConfig {
KrunvmConfig {
version: 1,
default_cpus: 2,
default_mem: 1024,
default_dns: "1.1.1.1".to_string(),
storage_volume: String::new(),
vmconfig_map: HashMap::new(),
}
}
}
#[cfg(target_os = "macos")]
fn check_case_sensitivity(volume: &str) -> Result<bool, io::Error> {
let first_path = format!("{}/krunvm_test", volume);
let second_path = format!("{}/krunVM_test", volume);
{
let mut first = File::create(&first_path)?;
first.write_all(b"first")?;
}
{
let mut second = File::create(&second_path)?;
second.write_all(b"second")?;
}
let mut data = String::new();
{
let mut test = File::open(&first_path)?;
test.read_to_string(&mut data)?;
}
if data == "first" {
let _ = std::fs::remove_file(first_path);
let _ = std::fs::remove_file(second_path);
Ok(true)
} else {
let _ = std::fs::remove_file(first_path);
Ok(false)
}
}
#[cfg(target_os = "macos")]
fn check_volume(cfg: &mut KrunvmConfig) {
if !cfg.storage_volume.is_empty() {
return;
}
println!(
"
On macOS, krunvm requires a dedicated, case-sensitive volume.
You can easily create such volume by executing something like
this on another terminal:
diskutil apfs addVolume disk3 \"Case-sensitive APFS\" krunvm
NOTE: APFS volume creation is a non-destructive action that
doesn't require a dedicated disk nor \"sudo\" privileges. The
new volume will share the disk space with the main container
volume.
"
);
loop {
print!("Please enter the mountpoint for this volume [/Volumes/krunvm]: ");
io::stdout().flush().unwrap();
let answer: String = read!("{}\n");
let volume = if answer.is_empty() {
"/Volumes/krunvm".to_string()
} else {
answer.to_string()
};
print!("Checking volume... ");
match check_case_sensitivity(&volume) {
Ok(res) => {
if res {
println!("success.");
println!("The volume has been configured. Please execute krunvm again");
cfg.storage_volume = volume;
confy::store(APP_NAME, cfg).unwrap();
std::process::exit(-1);
} else {
println!("failed.");
println!("This volume failed the case sensitivity test.");
}
}
Err(err) => {
println!("error.");
println!("There was an error running the test: {}", err);
}
}
}
}
#[cfg(target_os = "linux")]
fn check_unshare() {
let uid = unsafe { libc::getuid() };
if uid != 0 && !std::env::vars().any(|(key, _)| key == "BUILDAH_ISOLATION") {
println!("Please re-run krunvm inside a \"buildah unshare\" session");
std::process::exit(-1);
}
}
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Cli {
/// Sets the level of verbosity
#[arg(short)]
verbosity: Option<u8>, //TODO: implement or remove this
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command {
Start(StartCmd),
Create(CreateCmd),
List(ListCmd),
Delete(DeleteCmd),
#[command(name = "changevm")]
ChangeVm(ChangeVmCmd),
Config(ConfigCmd),
}
fn main() {
let mut cfg: KrunvmConfig = confy::load(APP_NAME).unwrap();
let cli_args = Cli::parse();
#[cfg(target_os = "macos")]
check_volume(&mut cfg);
#[cfg(target_os = "linux")]
check_unshare();
match cli_args.command {
Command::Start(cmd) => cmd.run(&cfg),
Command::Create(cmd) => cmd.run(&mut cfg),
Command::List(cmd) => cmd.run(&cfg),
Command::Delete(cmd) => cmd.run(&mut cfg),
Command::ChangeVm(cmd) => cmd.run(&mut cfg),
Command::Config(cmd) => cmd.run(&mut cfg),
}
}