forked from Morganamilo/paru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchroot.rs
165 lines (138 loc) · 4.31 KB
/
chroot.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
use crate::config::Config;
use crate::exec;
use anyhow::{Context, Result};
use nix::unistd::{Uid, User};
use std::ffi::OsStr;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug)]
pub struct Chroot {
pub sudo: String,
pub path: PathBuf,
pub pacman_conf: String,
pub makepkg_conf: String,
pub mflags: Vec<String>,
pub ro: Vec<String>,
pub rw: Vec<String>,
pub extra_pkgs: Vec<String>,
}
fn pacman_conf(pacman_conf: &str) -> Result<tempfile::NamedTempFile> {
let mut tmp = tempfile::NamedTempFile::new()?;
let conf = pacmanconf::Config::expand_with_opts(None, Some(pacman_conf), Some("/"))?;
// Bug with dbpath in pacstrap
let conf = conf
.lines()
.filter(|l| !l.starts_with("DBPath"))
.collect::<Vec<_>>()
.join("\n");
tmp.as_file_mut().write_all(conf.as_bytes())?;
tmp.flush()?;
Ok(tmp)
}
impl Chroot {
pub fn exists(&self) -> bool {
self.path.join("root").exists()
}
pub fn create<S: AsRef<OsStr>>(&self, config: &Config, pkgs: &[S]) -> Result<()> {
let mut cmd = Command::new(&config.sudo_bin);
cmd.arg("install").arg("-dm755").arg(&self.path);
exec::command(&mut cmd)?;
let tmp = pacman_conf(&self.pacman_conf)?;
let dir = self.path.join("root");
let mut cmd = Command::new(&self.sudo);
cmd.arg("mkarchroot")
.arg("-C")
.arg(tmp.path())
.arg("-M")
.arg(&self.makepkg_conf)
.arg(dir)
.args(pkgs);
exec::command(&mut cmd)?;
Ok(())
}
pub fn run<S: AsRef<OsStr>>(&self, args: &[S]) -> Result<()> {
self.run_as(true, args)
}
pub fn run_usr<S: AsRef<OsStr>>(&self, args: &[S]) -> Result<()> {
self.run_as(false, args)
}
fn run_as<S: AsRef<OsStr>>(&self, root: bool, args: &[S]) -> Result<()> {
let dir = if root {
self.path.join("root")
} else {
let user = User::from_uid(Uid::current())
.context("failed to get username")?
.context("failed to get username")?;
self.path.join(&user.name)
};
let tmp = pacman_conf(&self.pacman_conf)?;
let mut cmd = Command::new(&self.sudo);
cmd.arg("arch-nspawn")
.arg("-C")
.arg(tmp.path())
.arg("-M")
.arg(&self.makepkg_conf)
.arg(dir);
if Path::new(&format!("{}.d", self.makepkg_conf)).exists() {
cmd.arg("--bind-ro");
cmd.arg(format!("{}.d:/etc/makepkg.conf.d", self.makepkg_conf));
}
for file in &self.ro {
cmd.arg("--bind-ro");
cmd.arg(file);
}
for file in &self.rw {
cmd.arg("--bind");
cmd.arg(file);
}
cmd.args(args);
exec::command(&mut cmd)?;
Ok(())
}
pub fn update(&self) -> Result<()> {
let conf = pacmanconf::Config::with_opts(None, Some(self.pacman_conf.as_str()), Some("/"))?;
let db = Path::new(&conf.db_path).join("sync");
let dir = self.path.join("root");
let mut cmd = Command::new(&self.sudo);
cmd.arg("cp")
.arg("-auT")
.arg(&db)
.arg(dir.join(db.strip_prefix("/")?));
let _ = exec::command(&mut cmd);
self.run(&["pacman", "-Syu", "--noconfirm"])
}
pub fn build<S: AsRef<OsStr>>(
&self,
pkgbuild: &Path,
pkgs: &[&str],
chroot_flags: &[S],
flags: &[&str],
env: &[(String, String)],
) -> Result<()> {
let mut cmd = Command::new("makechrootpkg");
cmd.current_dir(pkgbuild)
.args(chroot_flags)
.arg("-r")
.arg(&self.path);
for pkg in pkgs {
cmd.arg("-I").arg(pkg);
}
for file in &self.ro {
cmd.arg("-D").arg(file);
}
for file in &self.rw {
cmd.arg("-d").arg(file);
}
cmd.arg("--").args(flags).args(&self.mflags);
for (key, value) in env {
if key == "PKGDEST" {
cmd.env(key, value);
} else {
cmd.arg(format!("{}={}", key, value));
}
}
exec::command(&mut cmd)?;
Ok(())
}
}