forked from bootc-dev/bootc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
408 lines (352 loc) · 11.3 KB
/
lib.rs
File metadata and controls
408 lines (352 loc) · 11.3 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Mount helpers for bootc-initramfs
use std::{
ffi::OsString,
fmt::Debug,
io::ErrorKind,
os::fd::{AsFd, AsRawFd, OwnedFd},
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
use clap::Parser;
use rustix::{
fs::{CWD, Mode, OFlags, major, minor, mkdirat, openat, stat, symlink},
io::Errno,
mount::{
FsMountFlags, MountAttrFlags, OpenTreeFlags, UnmountFlags, fsconfig_create,
fsconfig_set_string, fsmount, open_tree, unmount,
},
path,
};
use serde::Deserialize;
use cfsctl::composefs;
use cfsctl::composefs_boot;
use composefs::{
fsverity::{FsVerityHashValue, Sha512HashValue},
mount::FsHandle,
mountcompat::{overlayfs_set_fd, overlayfs_set_lower_and_data_fds, prepare_mount},
repository::Repository,
};
use composefs_boot::cmdline::get_cmdline_composefs;
use fn_error_context::context;
use bootc_kernel_cmdline::utf8::Cmdline;
// mount_setattr syscall support
const MOUNT_ATTR_RDONLY: u64 = 0x00000001;
#[repr(C)]
struct MountAttr {
attr_set: u64,
attr_clr: u64,
propagation: u64,
userns_fd: u64,
}
/// Set mount attributes using mount_setattr syscall
#[context("Setting mount attributes")]
#[allow(unsafe_code)]
fn mount_setattr(fd: impl AsFd, flags: libc::c_int, attr: &MountAttr) -> Result<()> {
let ret = unsafe {
libc::syscall(
libc::SYS_mount_setattr,
fd.as_fd().as_raw_fd(),
c"".as_ptr(),
flags,
attr as *const MountAttr,
std::mem::size_of::<MountAttr>(),
)
};
if ret == -1 {
Err(std::io::Error::last_os_error())?;
}
Ok(())
}
/// Set mount to readonly
#[context("Setting mount readonly")]
fn set_mount_readonly(fd: impl AsFd) -> Result<()> {
let attr = MountAttr {
attr_set: MOUNT_ATTR_RDONLY,
attr_clr: 0,
propagation: 0,
userns_fd: 0,
};
mount_setattr(fd, libc::AT_EMPTY_PATH, &attr)
}
/// Types of mounts supported by the configuration
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MountType {
/// No mount
None,
/// Bind mount
Bind,
/// Overlay mount
Overlay,
/// Transient mount
Transient,
}
#[derive(Debug, Default, Deserialize)]
struct RootConfig {
#[serde(default)]
transient: bool,
}
/// Configuration for mount operations
#[derive(Debug, Default, Deserialize)]
pub struct MountConfig {
/// The type of mount to use
pub mount: Option<MountType>,
#[serde(default)]
/// Whether this mount should be transient (temporary)
pub transient: bool,
}
#[derive(Deserialize, Default)]
struct Config {
#[serde(default)]
etc: MountConfig,
#[serde(default)]
var: MountConfig,
#[serde(default)]
root: RootConfig,
}
/// Command-line arguments
#[derive(Parser, Debug)]
#[command(version)]
pub struct Args {
#[arg(help = "Execute this command (for testing)")]
/// Execute this command (for testing)
pub cmd: Vec<OsString>,
#[arg(
long,
default_value = "/sysroot",
help = "sysroot directory in initramfs"
)]
/// sysroot directory in initramfs
pub sysroot: PathBuf,
#[arg(
long,
default_value = "/usr/lib/composefs/setup-root-conf.toml",
help = "Config path (for testing)"
)]
/// Config path (for testing)
pub config: PathBuf,
// we want to test in a userns, but can't mount erofs there
#[arg(long, help = "Bind mount root-fs from (for testing)")]
/// Bind mount root-fs from (for testing)
pub root_fs: Option<PathBuf>,
#[arg(long, help = "Kernel commandline args (for testing)")]
/// Kernel commandline args (for testing)
pub cmdline: Option<Cmdline<'static>>,
#[arg(long, help = "Mountpoint (don't replace sysroot, for testing)")]
/// Mountpoint (don't replace sysroot, for testing)
pub target: Option<PathBuf>,
}
/// Wrapper around [`composefs::mount::mount_at`]
pub fn mount_at_wrapper(
fs_fd: impl AsFd,
dirfd: impl AsFd,
path: impl path::Arg + Debug + Clone,
) -> Result<()> {
composefs::mount::mount_at(fs_fd, dirfd, path.clone())
.with_context(|| format!("Mounting at path {path:?}"))
}
/// Wrapper around [`rustix::fs::openat`]
#[context("Opening dir {name:?}")]
pub fn open_dir(dirfd: impl AsFd, name: impl AsRef<Path> + Debug) -> Result<OwnedFd> {
let res = openat(
dirfd,
name.as_ref(),
OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
);
Ok(res?)
}
#[context("Ensure dir")]
fn ensure_dir(dirfd: impl AsFd, name: &str, mode: Option<rustix::fs::Mode>) -> Result<OwnedFd> {
match mkdirat(dirfd.as_fd(), name, mode.unwrap_or(0o700.into())) {
Ok(()) | Err(Errno::EXIST) => {}
Err(err) => Err(err).with_context(|| format!("Creating dir {name}"))?,
}
open_dir(dirfd, name)
}
#[context("Bind mounting to path {path}")]
fn bind_mount(fd: impl AsFd, path: &str) -> Result<OwnedFd> {
let res = open_tree(
fd.as_fd(),
path,
OpenTreeFlags::OPEN_TREE_CLONE
| OpenTreeFlags::OPEN_TREE_CLOEXEC
| OpenTreeFlags::AT_EMPTY_PATH,
);
Ok(res?)
}
#[context("Mounting tmpfs")]
fn mount_tmpfs() -> Result<OwnedFd> {
let tmpfs = FsHandle::open("tmpfs")?;
fsconfig_create(tmpfs.as_fd())?;
Ok(fsmount(
tmpfs.as_fd(),
FsMountFlags::FSMOUNT_CLOEXEC,
MountAttrFlags::empty(),
)?)
}
#[context("Mounting state as overlay")]
fn overlay_state(
base: impl AsFd,
state: impl AsFd,
source: &str,
mode: Option<rustix::fs::Mode>,
mount_attr_flags: Option<MountAttrFlags>,
) -> Result<()> {
let upper = ensure_dir(state.as_fd(), "upper", mode)?;
let work = ensure_dir(state.as_fd(), "work", mode)?;
let overlayfs = FsHandle::open("overlay")?;
fsconfig_set_string(overlayfs.as_fd(), "source", source)?;
overlayfs_set_fd(overlayfs.as_fd(), "workdir", work.as_fd())?;
overlayfs_set_fd(overlayfs.as_fd(), "upperdir", upper.as_fd())?;
overlayfs_set_lower_and_data_fds(&overlayfs, base.as_fd(), None::<OwnedFd>)?;
fsconfig_create(overlayfs.as_fd())?;
let fs = fsmount(
overlayfs.as_fd(),
FsMountFlags::FSMOUNT_CLOEXEC,
mount_attr_flags.unwrap_or(MountAttrFlags::empty()),
)?;
mount_at_wrapper(fs, base, ".").context("Moving mount")
}
/// Mounts a transient overlayfs with passed in fd as the lowerdir
#[context("Mounting transient overlayfs")]
pub fn overlay_transient(
base: impl AsFd,
mode: Option<rustix::fs::Mode>,
mount_attr_flags: Option<MountAttrFlags>,
) -> Result<()> {
overlay_state(
base,
prepare_mount(mount_tmpfs()?)?,
"transient",
mode,
mount_attr_flags,
)
}
#[context("Opening rootfs")]
fn open_root_fs(path: &Path) -> Result<OwnedFd> {
let rootfs = open_tree(
CWD,
path,
OpenTreeFlags::OPEN_TREE_CLONE | OpenTreeFlags::OPEN_TREE_CLOEXEC,
)?;
set_mount_readonly(&rootfs)?;
Ok(rootfs)
}
/// Prepares a floating mount for composefs and returns the fd
///
/// # Arguments
/// * sysroot - fd for /sysroot
/// * name - Name of the EROFS image to be mounted
/// * allow_missing_fsverity - Whether to allow mount without fsverity support
#[context("Mounting composefs image")]
pub fn mount_composefs_image(
sysroot: &OwnedFd,
name: &str,
allow_missing_fsverity: bool,
) -> Result<OwnedFd> {
let mut repo = Repository::<Sha512HashValue>::open_path(sysroot, "composefs")?;
repo.set_insecure(allow_missing_fsverity);
let rootfs = repo
.mount(name)
.context("Failed to mount composefs image")?;
set_mount_readonly(&rootfs)?;
Ok(rootfs)
}
/// Mounts a subdirectory with the specified configuration
#[context("Mounting subdirectory")]
pub fn mount_subdir(
new_root: impl AsFd,
state: impl AsFd,
subdir: &str,
config: MountConfig,
default: MountType,
) -> Result<()> {
let mount_type = match config.mount {
Some(mt) => mt,
None => match config.transient {
true => MountType::Transient,
false => default,
},
};
match mount_type {
MountType::None => Ok(()),
MountType::Bind => Ok(mount_at_wrapper(
bind_mount(&state, subdir)?,
&new_root,
subdir,
)?),
MountType::Overlay => overlay_state(
open_dir(&new_root, subdir)?,
open_dir(&state, subdir)?,
"overlay",
None,
None,
),
MountType::Transient => overlay_transient(open_dir(&new_root, subdir)?, None, None),
}
}
#[context("GPT workaround")]
/// Workaround for /dev/gpt-auto-root
pub fn gpt_workaround() -> Result<()> {
// https://github.com/systemd/systemd/issues/35017
let rootdev = stat("/dev/gpt-auto-root");
let rootdev = match rootdev {
Ok(r) => r,
Err(e) if e.kind() == ErrorKind::NotFound => return Ok(()),
Err(e) => Err(e)?,
};
let target = format!(
"/dev/block/{}:{}",
major(rootdev.st_rdev),
minor(rootdev.st_rdev)
);
symlink(target, "/run/systemd/volatile-root")?;
Ok(())
}
/// Sets up /sysroot for switch-root
#[context("Setting up /sysroot")]
pub fn setup_root(args: Args) -> Result<()> {
let config = match std::fs::read_to_string(args.config) {
Ok(text) => toml::from_str(&text)?,
Err(err) if err.kind() == ErrorKind::NotFound => Config::default(),
Err(err) => Err(err)?,
};
let sysroot = open_dir(CWD, &args.sysroot)
.with_context(|| format!("Failed to open sysroot {:?}", args.sysroot))?;
let cmdline = args
.cmdline
.unwrap_or(Cmdline::from_proc().context("Failed to read cmdline")?);
let (image, insecure) = get_cmdline_composefs::<Sha512HashValue>(&cmdline)?;
let new_root = match args.root_fs {
Some(path) => open_root_fs(&path).context("Failed to clone specified root fs")?,
None => mount_composefs_image(&sysroot, &image.to_hex(), insecure)?,
};
// we need to clone this before the next step to make sure we get the old one
let sysroot_clone = bind_mount(&sysroot, "")?;
set_mount_readonly(&sysroot_clone)?;
let mount_target = args.target.unwrap_or(args.sysroot.clone());
// Ideally we build the new root filesystem together before we mount it, but that only works on
// 6.15 and later. Before 6.15 we can't mount into a floating tree, so mount it first. This
// will leave an abandoned clone of the sysroot mounted under it, but that's OK for now.
if cfg!(feature = "pre-6.15") {
mount_at_wrapper(&new_root, CWD, &mount_target)?;
}
if config.root.transient {
overlay_transient(&new_root, None, None)?;
}
match composefs::mount::mount_at(&sysroot_clone, &new_root, "sysroot") {
Ok(()) | Err(Errno::NOENT) => {}
Err(err) => Err(err)?,
}
// etc + var
let state = open_dir(open_dir(&sysroot, "state/deploy")?, image.to_hex())?;
mount_subdir(&new_root, &state, "etc", config.etc, MountType::Bind)?;
mount_subdir(&new_root, &state, "var", config.var, MountType::Bind)?;
if cfg!(not(feature = "pre-6.15")) {
// Replace the /sysroot with the new composed root filesystem
unmount(&args.sysroot, UnmountFlags::DETACH)?;
mount_at_wrapper(&new_root, CWD, &mount_target)?;
}
Ok(())
}