This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
280 lines (231 loc) · 9.32 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use clap::Parser;
use dagrs::{init_logger, DagEngine, EnvVar, Inputval, Retval, TaskTrait, TaskWrapper};
use log::{error, warn};
use std::env;
use std::fs;
mod build_lfs_sys;
mod build_rust_packages;
mod build_temp_toolchain;
mod config_sys;
mod prepare_host_sys;
mod utils;
mod vars;
use cmd_lib::*;
use std::path::Path;
#[allow(unused)]
use requestty::Question;
// TODO: 配置导出镜像
#[allow(unused)]
struct OutputLfsImg {}
impl TaskTrait for OutputLfsImg {
fn run(&self, _input: Inputval, _env: EnvVar) -> Retval {
Retval::new(())
}
}
#[warn(dead_code)]
/// 初始化
fn init() {
//TODO:目标分区检查,防止破坏宿主机系统
let _init_disk_info = vars::DISK_INFO.clone();
//删除已经存在的stop标志
let stop_flag_path = Path::new("./stop");
if stop_flag_path.exists() {
if let Err(e) = fs::remove_file(stop_flag_path) {
panic!("Cannot init stop flag {}", e);
}
}
}
fn main() {
//解析cli指令
let cli = vars::Cli::parse();
//初始化
init();
//初始化日志处理
init_logger(None);
//检测是否在必要的工作目录下
let current_dir = env::current_dir().unwrap();
if current_dir.parent() != Some(Path::new("/mnt")) {
error!(
"Cannot run at path {:?} , you must take the program in /mnt/[TEMP_SYSTEM]",
current_dir
);
return;
}
//检查环境变量
let check_env = TaskWrapper::new(prepare_host_sys::CheckEnv {}, "Check host env");
//准备配置目标文件目录
let mut prepare_dirs = TaskWrapper::new(prepare_host_sys::PreparingDirs {}, "PREPARE DIRS");
//准备下载软件
let mut prepare_software =
TaskWrapper::new(prepare_host_sys::PreparingSoftware {}, "Prepare software");
//安装临时工具链
let mut compile_toolchains = TaskWrapper::new(
build_temp_toolchain::CompilingCrossToolChain {},
"Compile Toolchains",
);
//进入chroot环境
let mut enter_chroot = TaskWrapper::new(utils::EnterChroot {}, "Enter Chroot");
//配置进入chroot环境后的部分
let mut after_chroot =
TaskWrapper::new(build_temp_toolchain::AfterChroot {}, "After chroot config");
//安装chroot后的工具
let mut install_other_packages = TaskWrapper::new(
build_temp_toolchain::AfterChrootInstall {},
"Install other packages",
);
//清理系统
let mut clean_system = TaskWrapper::new(
build_temp_toolchain::CleanUpAndSaveTempSystem {},
"Clean up",
);
//安装基础软件包
let mut install_packages = TaskWrapper::new(
build_lfs_sys::InstallBasicSystemSoftware {},
"Install Packages",
);
//删除debug的符号
let mut remove_debug_symbol =
TaskWrapper::new(build_lfs_sys::RemoveDebugSymbol {}, "Remove debug symbol");
//TODO:清理系统
#[allow(unused)]
let clean_up_system = TaskWrapper::new(build_lfs_sys::CleanUpSystem {}, "Clean up system");
//配置系统
let mut config_fstab = TaskWrapper::new(config_sys::Fstab {}, "Config fstab");
let mut config_inputrc = TaskWrapper::new(config_sys::Inputrc {}, "Config inputrc");
let mut config_network = TaskWrapper::new(config_sys::Network {}, "Config network");
let mut config_profile = TaskWrapper::new(config_sys::Profile {}, "Config profile");
let mut config_rcsite = TaskWrapper::new(config_sys::RcSite {}, "Config rc_site");
let mut config_shell = TaskWrapper::new(config_sys::Shell {}, "Config shell");
let mut config_sysvinit = TaskWrapper::new(config_sys::Sysvinit {}, "Config sysvinit");
let mut config_time = TaskWrapper::new(config_sys::Time {}, "Config time");
//安装内核和rust支持
let mut build_rust_packages = TaskWrapper::new(
build_rust_packages::InstallRustSupportPackages {},
"Build rust support package and kernel",
);
//安装grub启动
let mut grub_install = TaskWrapper::new(config_sys::ConfigGrub {}, "Install grub");
let mut dagrs = DagEngine::new();
let mut dag_nodes: Vec<TaskWrapper> = Vec::new();
//全流程顺序构建
match &cli.build_option {
vars::BuildOption::Build => {
prepare_dirs.exec_after(&[&check_env]);
prepare_software.exec_after(&[&prepare_dirs]);
compile_toolchains.exec_after(&[&prepare_software]);
enter_chroot.exec_after(&[&compile_toolchains]);
after_chroot.exec_after(&[&enter_chroot]);
install_other_packages.exec_after(&[&after_chroot]);
clean_system.exec_after(&[&install_other_packages]);
install_packages.exec_after(&[&clean_system]);
remove_debug_symbol.exec_after(&[&install_packages]);
config_fstab.exec_after(&[&remove_debug_symbol]);
config_inputrc.exec_after(&[&remove_debug_symbol]);
config_network.exec_after(&[&remove_debug_symbol]);
config_profile.exec_after(&[&remove_debug_symbol]);
config_rcsite.exec_after(&[&remove_debug_symbol]);
config_shell.exec_after(&[&remove_debug_symbol]);
config_sysvinit.exec_after(&[&remove_debug_symbol]);
config_time.exec_after(&[&remove_debug_symbol]);
build_rust_packages.exec_after(&[&config_network]);
grub_install.exec_after(&[&build_rust_packages]);
dag_nodes.push(check_env);
dag_nodes.push(prepare_dirs);
dag_nodes.push(prepare_software);
dag_nodes.push(compile_toolchains);
dag_nodes.push(enter_chroot);
dag_nodes.push(after_chroot);
dag_nodes.push(install_other_packages);
dag_nodes.push(clean_system);
dag_nodes.push(install_packages);
dag_nodes.push(remove_debug_symbol);
dag_nodes.push(config_fstab);
dag_nodes.push(config_inputrc);
dag_nodes.push(config_network);
dag_nodes.push(config_profile);
dag_nodes.push(config_rcsite);
dag_nodes.push(config_shell);
dag_nodes.push(config_sysvinit);
dag_nodes.push(config_time);
dag_nodes.push(build_rust_packages);
dag_nodes.push(grub_install);
}
vars::BuildOption::HostConfig => {
prepare_dirs.exec_after(&[&check_env]);
dag_nodes.push(check_env);
dag_nodes.push(prepare_dirs);
}
vars::BuildOption::PackageDownload => {
dag_nodes.push(prepare_software);
}
vars::BuildOption::BuildTempToolchains => {
compile_toolchains.exec_after(&[&check_env]);
enter_chroot.exec_after(&[&compile_toolchains]);
after_chroot.exec_after(&[&enter_chroot]);
install_other_packages.exec_after(&[&after_chroot]);
clean_system.exec_after(&[&install_other_packages]);
dag_nodes.push(check_env);
dag_nodes.push(compile_toolchains);
dag_nodes.push(enter_chroot);
dag_nodes.push(after_chroot);
dag_nodes.push(install_other_packages);
dag_nodes.push(clean_system);
}
vars::BuildOption::BuildBasePackages => {
enter_chroot.exec_after(&[&check_env]);
install_packages.exec_after(&[&enter_chroot]);
dag_nodes.push(check_env);
dag_nodes.push(enter_chroot);
dag_nodes.push(install_packages);
}
vars::BuildOption::CleanUp => {
dag_nodes.push(remove_debug_symbol);
}
vars::BuildOption::ConfigTargetSystem => {
enter_chroot.exec_after(&[&check_env]);
config_fstab.exec_after(&[&enter_chroot]);
config_inputrc.exec_after(&[&enter_chroot]);
config_network.exec_after(&[&enter_chroot]);
config_profile.exec_after(&[&enter_chroot]);
config_rcsite.exec_after(&[&enter_chroot]);
config_shell.exec_after(&[&enter_chroot]);
config_sysvinit.exec_after(&[&enter_chroot]);
config_time.exec_after(&[&enter_chroot]);
dag_nodes.push(check_env);
dag_nodes.push(enter_chroot);
dag_nodes.push(config_fstab);
dag_nodes.push(config_inputrc);
dag_nodes.push(config_network);
dag_nodes.push(config_profile);
dag_nodes.push(config_rcsite);
dag_nodes.push(config_shell);
dag_nodes.push(config_sysvinit);
dag_nodes.push(config_time);
}
vars::BuildOption::BuildRustSupportPackageAndKernel => {
enter_chroot.exec_after(&[&check_env]);
build_rust_packages.exec_after(&[&enter_chroot]);
dag_nodes.push(check_env);
dag_nodes.push(enter_chroot);
dag_nodes.push(build_rust_packages);
}
vars::BuildOption::InstallGrub => {
enter_chroot.exec_after(&[&check_env]);
grub_install.exec_after(&[&enter_chroot]);
dag_nodes.push(check_env);
dag_nodes.push(enter_chroot);
dag_nodes.push(grub_install);
}
}
match &cli.operate {
vars::StartMode::Start => {
dagrs.add_tasks(dag_nodes);
}
vars::StartMode::Reset => {
//TODO:增加reset部分
}
};
assert!(dagrs.run().unwrap());
}
#[cfg(test)]
mod tests {}