patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob b40402b1b15729bdd2228025850efe8d87a48e3b 10111 bytes (raw)
name: tools/mount-flatpak/src/main.rs 	 # note: path name is non-authoritative(*)

  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
 
// SPDX-FileCopyrightText: 2025 Alyssa Ross <hi@alyssa.is>
// SPDX-License-Identifier: EUPL-1.2+

//! Flatpak installations look like this:
//!
//! ```text
//! flatpak/
//! ├── app/
//! │   └── org.gnome.TextEditor/
//! │       ├── current -> x86_64/stable
//! │       └── x86_64/
//! │           └── stable/
//! │               ├── 0029140121b39f5b7cf4d44fd46b0708eee67f395b5e1291628612a0358fb909/
//! │               │   └── …
//! │               └── active -> 0029140121b39f5b7cf4d44fd46b0708eee67f395b5e1291628612a0358fb909
//! ├── db/
//! ├── exports/
//! │   └── …
//! ├── repo
//! │   ├── config
//! │   ├── objects
//! │   ├── tmp
//! │   │   └── cache
//! │   │       └── …
//! │   └── …
//! └── runtime
//!     ├── org.gnome.Platform
//!     │   └── x86_64
//!     │       └── 49
//!     │           ├── active -> bf6aa432cb310726f4ac0ec08cc88558619e1d4bd4b964e27e95187ecaad5400
//!     │           └── bf6aa432cb310726f4ac0ec08cc88558619e1d4bd4b964e27e95187ecaad5400
//!     │               └── …
//!     └── …
//! ```
//!
//! The purpose of this program is to use bind mounts to construct a
//! Flatpak installation containing only a single application and
//! runtime, which can be passed through to a VM without exposing
//! other installed applications.

mod keyfile;
mod metadata;

use std::borrow::Cow;
use std::env::{ArgsOs, args_os};
use std::ffi::{OsStr, OsString};
use std::io;
use std::os::unix::prelude::*;
use std::path::{Path, PathBuf};
use std::process::exit;

use pathrs::flags::{OpenFlags, ResolverFlags};
use pathrs::{Root, error::Error};
use rustix::fs::{CWD, FileType, fstat};
use rustix::mount::{MoveMountFlags, OpenTreeFlags, move_mount, open_tree};

use metadata::extract_runtime;

fn ex_usage() -> ! {
    eprintln!("Usage: mount-flatpak userdata installation app");
    exit(1);
}

fn open_subdir(root: &Root, path: &Path) -> Result<Root, Error> {
    root.open_subpath(path, OpenFlags::O_PATH | OpenFlags::O_DIRECTORY)
        .map(|f| Root::from_fd(f).with_resolver_flags(ResolverFlags::NO_SYMLINKS))
}

fn mount_commit(
    source_installation: &Root,
    source_path: &Path,
    target_installation: &Root,
    target_path: &Path,
    kind: &str,
) -> Result<(Root, OsString), String> {
    let source_commit_parent_dir = open_subdir(source_installation, source_path)
        .map_err(|e| format!("opening source {kind} commit parent: {e}"))?;
    let commit = source_commit_parent_dir
        .readlink("active")
        .map_err(|e| format!("reading active {kind} commit: {e}"))?;
    let source_root = open_subdir(&source_commit_parent_dir, &commit)
        .map_err(|e| format!("opening source {kind} commit: {e}"))?;
    let source_commit_tree = open_tree(
        &source_root,
        "",
        OpenTreeFlags::AT_EMPTY_PATH
            | OpenTreeFlags::OPEN_TREE_CLONE
            | OpenTreeFlags::OPEN_TREE_CLOEXEC
            | OpenTreeFlags::AT_RECURSIVE,
    )
    .map_err(|e| format!("cloning source commit tree: {e}"))?;
    let target_commit_dir = target_installation
        .mkdir_all(target_path, &PermissionsExt::from_mode(0o700))
        .map_err(|e| format!("creating target commit directory: {e}"))?;
    move_mount(
        source_commit_tree,
        "",
        target_commit_dir,
        "",
        MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH | MoveMountFlags::MOVE_MOUNT_T_EMPTY_PATH,
    )
    .map_err(|e| format!("mounting commit: {e}"))?;
    Ok((source_root, commit.as_os_str().to_owned()))
}

fn run(mut args: ArgsOs) -> Result<(), String> {
    let Some(user_data_path) = args.next().map(PathBuf::from) else {
        ex_usage();
    };
    let Some(installation_path) = args.next().map(PathBuf::from) else {
        ex_usage();
    };
    let Some(app) = args.next() else {
        ex_usage();
    };
    if args.next().is_some() {
        ex_usage();
    }

    let user_data = Root::open(&user_data_path)
        .map_err(|e| format!("opening user data partition: {e}"))?
        .with_resolver_flags(ResolverFlags::NO_SYMLINKS);

    let source_installation_dir = open_subdir(&user_data, &installation_path)
        .map_err(|e| format!("opening source flatpak installation: {e}"))?;

    std::fs::create_dir("flatpak")
        .map_err(|e| format!("creating target flatpak installation: {e}"))?;

    let target_installation_dir = open_tree(
        CWD,
        "flatpak",
        OpenTreeFlags::OPEN_TREE_CLONE
            | OpenTreeFlags::OPEN_TREE_CLOEXEC
            | OpenTreeFlags::AT_RECURSIVE
            | OpenTreeFlags::AT_SYMLINK_NOFOLLOW,
    )
    .map_err(|e| format!("opening target flatpak installation: {e}"))?;
    let target_installation_dir =
        Root::from_fd(target_installation_dir).with_resolver_flags(ResolverFlags::NO_SYMLINKS);

    let mut app_path = PathBuf::new();
    app_path.push("app");
    app_path.push(&app);
    let source_app_dir = open_subdir(&source_installation_dir, &app_path)
        .map_err(|e| format!("opening source flatpak app: {e}"))?;
    let arch_and_branch = source_app_dir
        .readlink("current")
        .map_err(|e| format!("reading current app arch and branch: {e}"))?;
    let mut components = arch_and_branch.components();
    let arch = components.next().unwrap().as_os_str();
    let branch = components.as_path().as_os_str();
    if branch.is_empty() {
        return Err("can't infer branch from \"current\" link".to_string());
    }

    let (source_commit_dir, commit) = mount_commit(
        &source_app_dir,
        &app_path.join(&arch_and_branch),
        &target_installation_dir,
        &arch_and_branch,
        "app",
    )?;

    let metadata = source_commit_dir
        .resolve("metadata")
        .map_err(|e| format!("resolving app metadata: {e}"))?;

    let metadata_stat =
        fstat(&metadata).map_err(|e| format!("checking app metadata is a regular file: {e}"))?;
    let metadata_type = FileType::from_raw_mode(metadata_stat.st_mode);
    if !metadata_type.is_file() {
        let e = format!("type of app metadata is {metadata_type:?}, not RegularFile");
        return Err(e);
    }
    let metadata = metadata
        .reopen(OpenFlags::O_RDONLY)
        .map_err(|e| format!("opening app metadata: {e}"))?;

    let runtime =
        extract_runtime(metadata).map_err(|e| format!("reading runtime from metadata: {e}"))?;
    let runtime_path = Path::new("runtime").join(runtime);

    let (_, runtime_commit) = mount_commit(
        &source_installation_dir,
        &runtime_path,
        &target_installation_dir,
        &runtime_path,
        "runtime",
    )?;

    target_installation_dir
        .mkdir_all("repo/objects", &PermissionsExt::from_mode(0o700))
        .map_err(|e| format!("creating repo/objects: {e}"))?;
    target_installation_dir
        .mkdir_all("repo/tmp/cache", &PermissionsExt::from_mode(0o700))
        .map_err(|e| format!("creating repo/tmp/cache: {e}"))?;
    let config_target = target_installation_dir
        .create_file(
            "repo/config",
            OpenFlags::O_WRONLY | OpenFlags::O_CLOEXEC,
            &PermissionsExt::from_mode(0o700),
        )
        .map_err(|e| format!("creating repo/config: {e}"))?;
    let config_source_path = env!("MOUNT_FLATPAK_CONFIG_PATH");
    let config_source = open_tree(
        CWD,
        config_source_path,
        OpenTreeFlags::OPEN_TREE_CLONE | OpenTreeFlags::OPEN_TREE_CLOEXEC,
    )
    .map_err(|e| format!("opening {config_source_path}: {e}"))?;
    move_mount(
        config_source,
        "",
        config_target,
        "",
        MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH | MoveMountFlags::MOVE_MOUNT_T_EMPTY_PATH,
    )
    .map_err(|e| format!("mounting config: {e}"))?;

    let mut attr = libc::mount_attr {
        attr_clr: libc::MOUNT_ATTR_NOSYMFOLLOW,
        attr_set: libc::MOUNT_ATTR_RDONLY | libc::MOUNT_ATTR_NODEV,
        propagation: libc::MS_SLAVE,
        userns_fd: 0,
    };
    let empty = b"\0";
    // SAFETY: we pass a valid FD, valid C string, and a valid mutable pointer with the
    // correct size.
    unsafe {
        let r = libc::syscall(
            libc::SYS_mount_setattr,
            target_installation_dir.as_fd().as_raw_fd() as libc::c_long,
            empty.as_ptr() as *const libc::c_char,
            (libc::AT_EMPTY_PATH | libc::AT_RECURSIVE) as libc::c_long,
            &mut attr as *mut libc::mount_attr,
            size_of::<libc::mount_attr>() as libc::c_long,
        );
        if r == -1 {
            return Err(format!(
                "setting target mount attributes: {}",
                io::Error::last_os_error()
            ));
        }
    }
    move_mount(
        target_installation_dir,
        "",
        CWD,
        "flatpak",
        MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
    )
    .map_err(|e| format!("mounting target installation dir: {e}"))?;

    std::fs::create_dir("params").map_err(|e| format!("creating params directory: {e}"))?;
    std::fs::write("params/id", app.as_bytes()).map_err(|e| format!("writing params/id: {e}"))?;
    std::fs::write("params/commit", commit.as_bytes())
        .map_err(|e| format!("writing params/commit: {e}"))?;
    std::fs::write("params/arch", arch.as_bytes())
        .map_err(|e| format!("writing params/arch: {e}"))?;
    std::fs::write("params/branch", branch.as_bytes())
        .map_err(|e| format!("writing params/branch: {e}"))?;
    std::fs::write("params/runtime-commit", runtime_commit.as_bytes())
        .map_err(|e| format!("writing params/runtime-commit: {e}"))?;

    Ok(())
}

fn main() {
    let mut args = args_os();

    let prog_name = args
        .next()
        .as_ref()
        .map(Path::new)
        .and_then(Path::file_name)
        .map_or(Cow::Borrowed("mount-flatpak"), OsStr::to_string_lossy)
        .into_owned();

    if let Err(e) = run(args) {
        eprintln!("{prog_name}: {e}");
        exit(1);
    }
}

debug log:

solving b40402b1b15729bdd2228025850efe8d87a48e3b ...
found b40402b1b15729bdd2228025850efe8d87a48e3b in https://inbox.spectrum-os.org/spectrum-devel/20251205-better-mount-flatpak-v1-3-229a81366091@gmail.com/
found 68a1f6ea308387a9471682c927d3a71439ab69e4 in https://inbox.spectrum-os.org/spectrum-devel/20251205-better-mount-flatpak-v1-2-229a81366091@gmail.com/
found dca42e5007e52b24c9a4b6940d5667f770a64bd0 in https://inbox.spectrum-os.org/spectrum-devel/20251205-better-mount-flatpak-v1-1-229a81366091@gmail.com/
retrying 6fc05569b9736e70311a7e2e20af4131a8d66303 as 6fc05569b9736e70311a7e2e20af4131a8d6630
retrying 6fc05569b9736e70311a7e2e20af4131a8d6630 as 6fc05569b9736e70311a7e2e20af4131a8d663
retrying 6fc05569b9736e70311a7e2e20af4131a8d663 as 6fc05569b9736e70311a7e2e20af4131a8d66
retrying 6fc05569b9736e70311a7e2e20af4131a8d66 as 6fc05569b9736e70311a7e2e20af4131a8d6
retrying 6fc05569b9736e70311a7e2e20af4131a8d6 as 6fc05569b9736e70311a7e2e20af4131a8d
retrying 6fc05569b9736e70311a7e2e20af4131a8d as 6fc05569b9736e70311a7e2e20af4131a8
retrying 6fc05569b9736e70311a7e2e20af4131a8 as 6fc05569b9736e70311a7e2e20af4131a
retrying 6fc05569b9736e70311a7e2e20af4131a as 6fc05569b9736e70311a7e2e20af4131
retrying 6fc05569b9736e70311a7e2e20af4131 as 6fc05569b9736e70311a7e2e20af413
retrying 6fc05569b9736e70311a7e2e20af413 as 6fc05569b9736e70311a7e2e20af41
retrying 6fc05569b9736e70311a7e2e20af41 as 6fc05569b9736e70311a7e2e20af4
retrying 6fc05569b9736e70311a7e2e20af4 as 6fc05569b9736e70311a7e2e20af
retrying 6fc05569b9736e70311a7e2e20af as 6fc05569b9736e70311a7e2e20a
retrying 6fc05569b9736e70311a7e2e20a as 6fc05569b9736e70311a7e2e20
retrying 6fc05569b9736e70311a7e2e20 as 6fc05569b9736e70311a7e2e2
retrying 6fc05569b9736e70311a7e2e2 as 6fc05569b9736e70311a7e2e
retrying 6fc05569b9736e70311a7e2e as 6fc05569b9736e70311a7e2
retrying 6fc05569b9736e70311a7e2 as 6fc05569b9736e70311a7e
retrying 6fc05569b9736e70311a7e as 6fc05569b9736e70311a7
retrying 6fc05569b9736e70311a7 as 6fc05569b9736e70311a
retrying 6fc05569b9736e70311a as 6fc05569b9736e70311
retrying 6fc05569b9736e70311 as 6fc05569b9736e7031
retrying 6fc05569b9736e7031 as 6fc05569b9736e703
retrying 6fc05569b9736e703 as 6fc05569b9736e70
retrying 6fc05569b9736e70 as 6fc05569b9736e7
retrying 6fc05569b9736e7 as 6fc05569b9736e
retrying 6fc05569b9736e as 6fc05569b9736
retrying 6fc05569b9736 as 6fc05569b973
retrying 6fc05569b973 as 6fc05569b97
retrying 6fc05569b97 as 6fc05569b9
retrying 6fc05569b9 as 6fc05569b
retrying 6fc05569b as 6fc05569
retrying 6fc05569 as 6fc0556
found 6fc0556 in https://inbox.spectrum-os.org/spectrum-devel/20251201170458.4186683-7-hi@alyssa.is/

applying [1/4] https://inbox.spectrum-os.org/spectrum-devel/20251201170458.4186683-7-hi@alyssa.is/
diff --git a/tools/mount-flatpak/src/main.rs b/tools/mount-flatpak/src/main.rs
new file mode 100644
index 0000000..6fc0556


applying [2/4] https://inbox.spectrum-os.org/spectrum-devel/20251205-better-mount-flatpak-v1-1-229a81366091@gmail.com/
diff --git a/tools/mount-flatpak/src/main.rs b/tools/mount-flatpak/src/main.rs
index 6fc05569b9736e70311a7e2e20af4131a8d66303..dca42e5007e52b24c9a4b6940d5667f770a64bd0 100644


applying [3/4] https://inbox.spectrum-os.org/spectrum-devel/20251205-better-mount-flatpak-v1-2-229a81366091@gmail.com/
diff --git a/tools/mount-flatpak/src/main.rs b/tools/mount-flatpak/src/main.rs
index dca42e5007e52b24c9a4b6940d5667f770a64bd0..68a1f6ea308387a9471682c927d3a71439ab69e4 100644


applying [4/4] https://inbox.spectrum-os.org/spectrum-devel/20251205-better-mount-flatpak-v1-3-229a81366091@gmail.com/
diff --git a/tools/mount-flatpak/src/main.rs b/tools/mount-flatpak/src/main.rs
index 68a1f6ea308387a9471682c927d3a71439ab69e4..b40402b1b15729bdd2228025850efe8d87a48e3b 100644

Checking patch tools/mount-flatpak/src/main.rs...
Applied patch tools/mount-flatpak/src/main.rs cleanly.
Checking patch tools/mount-flatpak/src/main.rs...
Applied patch tools/mount-flatpak/src/main.rs cleanly.
Checking patch tools/mount-flatpak/src/main.rs...
Applied patch tools/mount-flatpak/src/main.rs cleanly.
Checking patch tools/mount-flatpak/src/main.rs...
Applied patch tools/mount-flatpak/src/main.rs cleanly.

index at:
100644 b40402b1b15729bdd2228025850efe8d87a48e3b	tools/mount-flatpak/src/main.rs

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).