blob: f3e007e231c9a7344e304ffb5f3cf8fa020785d7 (
plain) (
blame)
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
|
use anyhow::bail;
use std::env;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::Path;
use std::process::Command;
const LATEST: &str = "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.1.1.tar.xz";
const CONFIG: &str = r#"
CONFIG_SQUASHFS=y
CONFIG_SQUASHFS_DECOMP_MULTI_PERCPU=y
CONFIG_IPV6=y
"#;
fn download_kernel(file_name: &str) -> anyhow::Result<()> {
println!("Downloading kernel source...");
let mut file = File::create(file_name)?;
reqwest::blocking::get(LATEST)?
.error_for_status()?
.copy_to(&mut file)?;
println!("Kernel source downloaded successfully");
Ok(())
}
fn compile() -> anyhow::Result<()> {
let mut defconfig = Command::new("make");
defconfig.arg("defconfig");
if !defconfig.spawn()?.wait()?.success() {
bail!("make defconfig failed");
}
let mut mod2noconfig = Command::new("make");
mod2noconfig.arg("mod2noconfig");
if !mod2noconfig.spawn()?.wait()?.success() {
bail!("make mod2noconfig failed");
}
// Drop and close the file before continuing.
{
let mut file = File::options()
.truncate(false)
.append(true)
.open(".config")?;
file.write_all(CONFIG.as_bytes())?;
}
let mut make = Command::new("make");
make.arg("bzImage")
.arg("modules")
.arg("-j".to_owned() + &num_cpus::get().to_string());
if !make.spawn()?.wait()?.success() {
bail!("make failed");
}
Ok(())
}
fn copy_file<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> anyhow::Result<()> {
let mut outfile = File::create(dst)?;
let mut infile = File::open(src)?;
let mut buf = Vec::new();
infile.read_to_end(&mut buf)?;
outfile.write_all(&buf)?;
outfile.set_permissions(infile.metadata()?.permissions())?;
Ok(())
}
fn main() -> anyhow::Result<()> {
let file_name = Path::new(LATEST).file_name().unwrap().to_str().unwrap();
download_kernel(file_name)?;
let mut untar = Command::new("tar");
untar.arg("xf").arg(file_name);
if !untar.spawn()?.wait()?.success() {
bail!("untar failed");
}
println!("Kernel source unpacked successfully");
let current_dir = env::current_dir()?;
env::set_current_dir(file_name.trim_end_matches(".tar.xz"))?;
println!("Compiling kernel...");
compile()?;
println!("Kernel compiled successfully");
let kernel_path = "arch/x86_64/boot/bzImage"; /* FIXME: arch independent */
env::set_current_dir(current_dir)?;
copy_file(
Path::new(file_name.trim_end_matches(".tar.xz")).join(kernel_path),
"vmlinuz",
)?;
fs::remove_file(file_name)?;
fs::remove_dir_all(file_name.trim_end_matches(".tar.xz"))?;
Ok(())
}
|