-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathcomposepost.rs
1229 lines (1104 loc) · 43.8 KB
/
composepost.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Logic for post-processing a filesystem tree, server-side.
//!
//! This code runs server side to "postprocess" a filesystem tree (usually
//! containing mostly RPMs) in order to prepare it as an OSTree commit.
// SPDX-License-Identifier: Apache-2.0 OR MIT
use crate::bwrap;
use crate::cxxrsutil::{CxxResult, FFIGObjectWrapper};
use crate::passwd::PasswdDB;
use crate::treefile::Treefile;
use anyhow::{anyhow, bail, Context, Result};
use camino::Utf8Path;
use fn_error_context::context;
use gio::CancellableExt;
use nix::sys::stat::Mode;
use openat_ext::OpenatDirExt;
use rayon::prelude::*;
use std::borrow::Cow;
use std::convert::TryInto;
use std::fmt::Write as FmtWrite;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Seek, Write};
use std::os::unix::fs::PermissionsExt;
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::pin::Pin;
/* See rpmostree-core.h */
const RPMOSTREE_BASE_RPMDB: &str = "usr/lib/sysimage/rpm-ostree-base-db";
const RPMOSTREE_RPMDB_LOCATION: &str = "usr/share/rpm";
const RPMOSTREE_SYSIMAGE_RPMDB: &str = "usr/lib/sysimage/rpm";
const TRADITIONAL_RPMDB_LOCATION: &str = "var/lib/rpm";
#[context("Moving {}", name)]
fn dir_move_if_exists(src: &openat::Dir, dest: &openat::Dir, name: &str) -> Result<()> {
if src.exists(name)? {
openat::rename(src, name, dest, name)?;
}
Ok(())
}
/// Initialize an ostree-oriented root filesystem.
///
/// This is hardcoded; in the future we may make more things configurable,
/// but the goal is for all state to be in `/etc` and `/var`.
#[context("Initializing rootfs")]
fn compose_init_rootfs(rootfs_dfd: &openat::Dir, tmp_is_dir: bool) -> Result<()> {
use nix::fcntl::OFlag;
println!("Initializing rootfs");
let default_dirmode = Mode::from_bits(0o755).unwrap();
// Unfortunately fchmod() doesn't operate on an O_PATH descriptor
let flag = OFlag::O_DIRECTORY | OFlag::O_CLOEXEC;
let nix_rootfs = nix::dir::Dir::openat(rootfs_dfd.as_raw_fd(), ".", flag, default_dirmode)?;
const TOPLEVEL_DIRS: &[&str] = &["dev", "proc", "run", "sys", "var", "sysroot"];
const TOPLEVEL_SYMLINKS: &[(&str, &str)] = &[
("var/opt", "opt"),
("var/srv", "srv"),
("var/mnt", "mnt"),
("var/roothome", "root"),
("var/home", "home"),
("run/media", "media"),
("sysroot/ostree", "ostree"),
];
nix::sys::stat::fchmod(nix_rootfs.as_raw_fd(), default_dirmode).context("rootfs chmod")?;
TOPLEVEL_DIRS
.par_iter()
.try_for_each(|&d| rootfs_dfd.ensure_dir(d, default_dirmode.bits()))?;
TOPLEVEL_SYMLINKS
.par_iter()
.try_for_each(|&(dest, src)| rootfs_dfd.symlink(src, dest))?;
if tmp_is_dir {
let tmp_mode = 0o1777;
rootfs_dfd.ensure_dir("tmp", tmp_mode)?;
nix::sys::stat::fchmodat(
Some(nix_rootfs.as_raw_fd()),
"tmp",
Mode::from_bits(tmp_mode).unwrap(),
nix::sys::stat::FchmodatFlags::FollowSymlink,
)?;
} else {
rootfs_dfd.symlink("tmp", "sysroot/tmp")?;
}
Ok(())
}
/// Prepare rootfs for commit.
///
/// Initialize a basic root filesystem in @target_root_dfd, then walk over the
/// root filesystem in @src_rootfs_fd and take the basic content (/usr, /boot, /var)
/// and cherry pick only specific bits of the rest of the toplevel like compatibility
/// symlinks (e.g. /lib64 -> /usr/lib64) if they exist.
#[context("Preparing rootfs for commit")]
pub fn compose_prepare_rootfs(
src_rootfs_dfd: i32,
target_rootfs_dfd: i32,
treefile: &mut Treefile,
) -> CxxResult<()> {
let src_rootfs_dfd = &crate::ffiutil::ffi_view_openat_dir(src_rootfs_dfd);
let target_rootfs_dfd = &crate::ffiutil::ffi_view_openat_dir(target_rootfs_dfd);
let tmp_is_dir = treefile.parsed.tmp_is_dir.unwrap_or_default();
compose_init_rootfs(target_rootfs_dfd, tmp_is_dir)?;
println!("Moving /usr to target");
openat::rename(src_rootfs_dfd, "usr", target_rootfs_dfd, "usr")?;
/* The kernel may be in the source rootfs /boot; to handle that, we always
* rename the source /boot to the target, and will handle everything after
* that in the target root.
*/
dir_move_if_exists(src_rootfs_dfd, target_rootfs_dfd, "boot")?;
/* And grab /var - we'll convert to tmpfiles.d later */
dir_move_if_exists(src_rootfs_dfd, target_rootfs_dfd, "var")?;
const TOPLEVEL_LINKS: &[&str] = &["lib", "lib64", "lib32", "bin", "sbin"];
println!("Copying toplevel compat symlinks");
TOPLEVEL_LINKS
.par_iter()
.try_for_each(|&l| dir_move_if_exists(src_rootfs_dfd, target_rootfs_dfd, l))?;
Ok(())
}
// rpm-ostree uses /home → /var/home by default as generated by our
// rootfs; we don't expect people to change this. Let's be nice
// and also fixup the $HOME entries generated by `useradd` so
// that `~` shows up as expected in shells, etc.
//
// https://github.com/coreos/fedora-coreos-config/pull/18
// https://pagure.io/workstation-ostree-config/pull-request/121
// https://discussion.fedoraproject.org/t/adapting-user-home-in-etc-passwd/487/6
// https://github.com/justjanne/powerline-go/issues/94
fn postprocess_useradd(rootfs_dfd: &openat::Dir) -> Result<()> {
let path = Path::new("usr/etc/default/useradd");
if let Some(f) = rootfs_dfd.open_file_optional(path)? {
rootfs_dfd.write_file_with(&path, 0o644, |bufw| -> Result<_> {
let f = BufReader::new(&f);
for line in f.lines() {
let line = line?;
if !line.starts_with("HOME=") {
bufw.write_all(line.as_bytes())?;
} else {
bufw.write_all(b"HOME=/var/home")?;
}
bufw.write_all(b"\n")?;
}
Ok(())
})?;
}
Ok(())
}
// We keep hitting issues with the ostree-remount preset not being
// enabled; let's just do this rather than trying to propagate the
// preset everywhere.
fn postprocess_presets(rootfs_dfd: &openat::Dir) -> Result<()> {
let wantsdir = "usr/lib/systemd/system/multi-user.target.wants";
rootfs_dfd.ensure_dir_all(wantsdir, 0o755)?;
for service in &["ostree-remount.service", "ostree-finalize-staged.path"] {
let target = format!("../{}", service);
let loc = Path::new(wantsdir).join(service);
rootfs_dfd.symlink(&loc, target)?;
}
Ok(())
}
// We keep hitting issues with the ostree-remount preset not being
// enabled; let's just do this rather than trying to propagate the
// preset everywhere.
fn postprocess_rpm_macro(rootfs_dfd: &openat::Dir) -> Result<()> {
let rpm_macros_dir = "usr/lib/rpm/macros.d";
rootfs_dfd.ensure_dir_all(rpm_macros_dir, 0o755)?;
let rpm_macros_dfd = rootfs_dfd.sub_dir(rpm_macros_dir)?;
rpm_macros_dfd.write_file_with("macros.rpm-ostree", 0o644, |w| -> Result<()> {
w.write_all(b"%_dbpath /")?;
w.write_all(RPMOSTREE_RPMDB_LOCATION.as_bytes())?;
Ok(())
})?;
Ok(())
}
// This function does two things: (1) make sure there is a /home --> /var/home substitution rule,
// and (2) make sure there *isn't* a /var/home -> /home substition rule. The latter check won't
// technically be needed once downstreams have:
// https://src.fedoraproject.org/rpms/selinux-policy/pull-request/14
fn postprocess_subs_dist(rootfs_dfd: &openat::Dir) -> Result<()> {
let path = Path::new("usr/etc/selinux/targeted/contexts/files/file_contexts.subs_dist");
if let Some(f) = rootfs_dfd.open_file_optional(path)? {
rootfs_dfd.write_file_with(&path, 0o644, |w| -> Result<()> {
let f = BufReader::new(&f);
for line in f.lines() {
let line = line?;
if line.starts_with("/var/home ") {
w.write_all(b"# https://github.com/projectatomic/rpm-ostree/pull/1754\n")?;
w.write_all(b"# ")?;
}
w.write_all(line.as_bytes())?;
w.write_all(b"\n")?;
}
w.write_all(b"# https://github.com/projectatomic/rpm-ostree/pull/1754\n")?;
w.write_all(b"/home /var/home")?;
w.write_all(b"\n")?;
Ok(())
})?;
}
Ok(())
}
/// Final processing steps.
///
/// This function is called from rpmostree_postprocess_final(); think of
/// it as the bits of that function that we've chosen to implement in Rust.
/// It takes care of all things that are really required to use rpm-ostree
/// on the target host.
pub fn compose_postprocess_final(rootfs_dfd: i32) -> CxxResult<()> {
let rootfs_dfd = crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
let tasks = [
postprocess_useradd,
postprocess_presets,
postprocess_subs_dist,
postprocess_rpm_macro,
];
Ok(tasks.par_iter().try_for_each(|f| f(&rootfs_dfd))?)
}
#[context("Handling treefile 'units'")]
fn compose_postprocess_units(rootfs_dfd: &openat::Dir, treefile: &mut Treefile) -> Result<()> {
let units = if let Some(u) = treefile.parsed.units.as_ref() {
u
} else {
return Ok(());
};
let multiuser_wants = Path::new("usr/etc/systemd/system/multi-user.target.wants");
// Sanity check
if !rootfs_dfd.exists("usr/etc")? {
return Err(anyhow!("Missing usr/etc in rootfs"));
}
rootfs_dfd.ensure_dir_all(multiuser_wants, 0o755)?;
for unit in units {
let dest = multiuser_wants.join(unit);
if rootfs_dfd.exists(&dest)? {
continue;
}
println!("Adding {} to multi-user.target.wants", unit);
let target = format!("/usr/lib/systemd/system/{}", unit);
rootfs_dfd.symlink(&dest, &target)?;
}
Ok(())
}
#[context("Handling treefile 'default-target'")]
fn compose_postprocess_default_target(rootfs_dfd: &openat::Dir, target: &str) -> Result<()> {
/* This used to be in /etc, but doing it in /usr makes more sense, as it's
* part of the OS defaults. This was changed in particular to work with
* ConditionFirstBoot= which runs `systemctl preset-all`:
* https://github.com/projectatomic/rpm-ostree/pull/1425
*/
let default_target_path = "usr/lib/systemd/system/default.target";
rootfs_dfd.remove_file_optional(default_target_path)?;
let dest = format!("/usr/lib/systemd/system/{}", target);
rootfs_dfd.symlink(default_target_path, dest)?;
Ok(())
}
/// The treefile format has two kinds of postprocessing scripts;
/// there's a single `postprocess-script` as well as inline (anonymous)
/// scripts. This function executes both kinds in bwrap containers.
fn compose_postprocess_scripts(
rootfs_dfd: &openat::Dir,
treefile: &mut Treefile,
unified_core: bool,
) -> Result<()> {
// Execute the anonymous (inline) scripts.
for (i, script) in treefile.parsed.postprocess.iter().flatten().enumerate() {
let binpath = format!("/usr/bin/rpmostree-postprocess-inline-{}", i);
let target_binpath = &binpath[1..];
rootfs_dfd.write_file_contents(target_binpath, 0o755, script)?;
println!("Executing `postprocess` inline script '{}'", i);
let child_argv = vec![binpath.to_string()];
let _ =
bwrap::bubblewrap_run_sync(rootfs_dfd.as_raw_fd(), &child_argv, false, unified_core)?;
rootfs_dfd.remove_file(target_binpath)?;
}
// And the single postprocess script.
if let Some(postprocess_script) = treefile.get_postprocess_script() {
let binpath = "/usr/bin/rpmostree-treefile-postprocess-script";
let target_binpath = &binpath[1..];
postprocess_script.seek(std::io::SeekFrom::Start(0))?;
let mut reader = std::io::BufReader::new(postprocess_script);
rootfs_dfd.write_file_with(target_binpath, 0o755, |w| std::io::copy(&mut reader, w))?;
println!("Executing postprocessing script");
let child_argv = &vec![binpath.to_string()];
let _ = crate::bwrap::bubblewrap_run_sync(
rootfs_dfd.as_raw_fd(),
child_argv,
false,
unified_core,
)
.context("Executing postprocessing script")?;
rootfs_dfd.remove_file(target_binpath)?;
println!("Finished postprocessing script");
}
Ok(())
}
/// Logic for handling treefile `remove-files`.
#[context("Handling `remove-files`")]
pub fn compose_postprocess_remove_files(
rootfs_dfd: &openat::Dir,
treefile: &mut Treefile,
) -> CxxResult<()> {
for name in treefile.parsed.remove_files.iter().flatten() {
let p = Path::new(name);
if p.is_absolute() {
return Err(anyhow!("Invalid absolute path: {}", name).into());
}
if name.contains("..") {
return Err(anyhow!("Invalid '..' in path: {}", name).into());
}
println!("Deleting: {}", name);
rootfs_dfd.remove_all(name)?;
}
Ok(())
}
fn compose_postprocess_add_files(rootfs_dfd: &openat::Dir, treefile: &mut Treefile) -> Result<()> {
// Make a deep copy here because get_add_file_fd() also wants an &mut
// reference.
let add_files: Vec<_> = treefile
.parsed
.add_files
.iter()
.flatten()
.cloned()
.collect();
for (src, dest) in add_files {
let reldest = dest.trim_start_matches('/');
if reldest.is_empty() {
return Err(anyhow!("Invalid add-files destination: {}", dest).into());
}
let dest = if reldest.starts_with("etc/") {
Cow::Owned(format!("usr/{}", reldest))
} else {
Cow::Borrowed(reldest)
};
println!("Adding file {}", dest);
let dest = Path::new(&*dest);
if let Some(parent) = dest.parent() {
rootfs_dfd.ensure_dir_all(parent, 0o755)?;
}
let fd = treefile.get_add_file(&src);
fd.seek(std::io::SeekFrom::Start(0))?;
let mut reader = std::io::BufReader::new(fd);
let mode = reader.get_mut().metadata()?.permissions().mode();
rootfs_dfd.write_file_with(dest, mode, |w| std::io::copy(&mut reader, w))?;
}
Ok(())
}
#[context("Symlinking {}", TRADITIONAL_RPMDB_LOCATION)]
fn compose_postprocess_rpmdb(rootfs_dfd: &openat::Dir) -> Result<()> {
/* This works around a potential issue with libsolv if we go down the
* rpmostree_get_pkglist_for_root() path. Though rpm has been using the
* /usr/share/rpm location (since the RpmOstreeContext set the _dbpath macro),
* the /var/lib/rpm directory will still exist, but be empty. libsolv gets
* confused because it sees the /var/lib/rpm dir and doesn't even try the
* /usr/share/rpm location, and eventually dies when it tries to load the
* data. XXX: should probably send a patch upstream to libsolv.
*
* So we set the symlink now. This is also what we do on boot anyway for
* compatibility reasons using tmpfiles.
* */
rootfs_dfd.remove_all(TRADITIONAL_RPMDB_LOCATION)?;
rootfs_dfd.symlink(
TRADITIONAL_RPMDB_LOCATION,
format!("../../{}", RPMOSTREE_RPMDB_LOCATION),
)?;
Ok(())
}
/// Rust portion of rpmostree_treefile_postprocessing()
pub fn compose_postprocess(
rootfs_dfd: i32,
treefile: &mut Treefile,
next_version: &str,
unified_core: bool,
) -> CxxResult<()> {
let rootfs_dfd = &crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
// One of several dances we do around this that really needs to be completely
// reworked.
if rootfs_dfd.exists("etc")? {
rootfs_dfd.local_rename("etc", "usr/etc")?;
}
compose_postprocess_rpmdb(rootfs_dfd)?;
compose_postprocess_units(&rootfs_dfd, treefile)?;
if let Some(t) = treefile.parsed.default_target.as_deref() {
compose_postprocess_default_target(&rootfs_dfd, t)?;
}
treefile.write_compose_json(rootfs_dfd)?;
let etc_guard = crate::core::prepare_tempetc_guard(rootfs_dfd.as_raw_fd())?;
// These ones depend on the /etc path
compose_postprocess_mutate_os_release(rootfs_dfd, treefile, next_version)?;
compose_postprocess_remove_files(rootfs_dfd, treefile)?;
compose_postprocess_add_files(rootfs_dfd, treefile)?;
etc_guard.undo()?;
compose_postprocess_scripts(rootfs_dfd, treefile, unified_core)?;
Ok(())
}
/// Implementation of the treefile `mutate-os-release` field.
#[context("Updating os-release with commit version")]
fn compose_postprocess_mutate_os_release(
rootfs_dfd: &openat::Dir,
treefile: &mut Treefile,
next_version: &str,
) -> Result<()> {
let base_version = if let Some(base_version) = treefile.parsed.mutate_os_release.as_deref() {
base_version
} else {
return Ok(());
};
if next_version.is_empty() {
println!("Ignoring mutate-os-release: no commit version specified.");
return Ok(());
}
// find the real path to os-release using bwrap; this is an overkill but safer way
// of resolving a symlink relative to a rootfs (see discussions in
// https://github.com/projectatomic/rpm-ostree/pull/410/)
let mut bwrap = crate::bwrap::Bubblewrap::new_with_mutability(
rootfs_dfd,
crate::ffi::BubblewrapMutability::Immutable,
)?;
bwrap.append_child_argv(&["realpath", "/etc/os-release"]);
let cancellable = &gio::Cancellable::new();
let cancellable = Some(cancellable);
let path = bwrap.run_captured(cancellable)?;
let path = std::str::from_utf8(&path)
.context("Parsing realpath")?
.trim_start_matches("/")
.trim_end();
let path = if path.is_empty() {
// fallback on just overwriting etc/os-release
"etc/os-release"
} else {
path
};
println!("Updating {}", path);
let contents = rootfs_dfd
.read_to_string(path)
.with_context(|| format!("Reading {}", path))?;
let new_contents = mutate_os_release_contents(&contents, base_version, next_version);
rootfs_dfd
.write_file_contents(path, 0o644, new_contents.as_bytes())
.with_context(|| format!("Writing {}", path))?;
Ok(())
}
/// Given the contents of a /usr/lib/os-release file,
/// update the `VERSION` and `PRETTY_NAME` fields.
fn mutate_os_release_contents(contents: &str, base_version: &str, next_version: &str) -> String {
let mut buf = String::new();
for line in contents.lines() {
if line.is_empty() {
continue;
}
let prefixes = &["VERSION=", "PRETTY_NAME="];
if let Some((prefix, rest)) = strip_any_prefix(line, prefixes) {
buf.push_str(prefix);
let replaced = rest.replace(base_version, next_version);
buf.push_str(&replaced);
} else {
buf.push_str(line);
}
buf.push('\n');
}
// Unwrap safety; we provided it UTF-8
let quoted_version = glib::shell_quote(next_version).unwrap();
let quoted_version = quoted_version.to_str().unwrap();
// Unwrap safety: write! to a String can't fail
writeln!(buf, "OSTREE_VERSION={}", quoted_version).unwrap();
buf
}
/// Given a string and a set of possible prefixes, return the split
/// prefix and remaining string, or `None` if no matches.
fn strip_any_prefix<'a, 'b>(s: &'a str, prefixes: &[&'b str]) -> Option<(&'b str, &'a str)> {
prefixes
.iter()
.find_map(|&p| s.strip_prefix(p).map(|r| (p, r)))
}
/// Inject `altfiles` after `files` for `passwd:` and `group:` entries.
fn add_altfiles(buf: &str) -> Result<String> {
let mut r = String::with_capacity(buf.len());
for line in buf.lines() {
let parts = if let Some(p) = strip_any_prefix(line, &["passwd:", "group:"]) {
p
} else {
r.push_str(line);
r.push('\n');
continue;
};
let (prefix, rest) = parts;
r.push_str(prefix);
let mut inserted = false;
for elt in rest.split_whitespace() {
// Already have altfiles? We're done
if elt == "altfiles" {
return Ok(buf.to_string());
}
// We prefer `files altfiles`
if !inserted && elt == "files" {
r.push_str(" files altfiles");
inserted = true;
} else {
r.push(' ');
r.push_str(elt);
}
}
if !inserted {
r.push_str(" altfiles");
}
r.push('\n');
}
Ok(r)
}
/// Add `altfiles` entries to `nsswitch.conf`.
///
/// rpm-ostree currently depends on `altfiles`
#[context("Adding altfiles to /etc/nsswitch.conf")]
pub fn composepost_nsswitch_altfiles(rootfs_dfd: i32) -> CxxResult<()> {
let rootfs_dfd = &crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
let path = "usr/etc/nsswitch.conf";
let nsswitch = rootfs_dfd.read_to_string(path)?;
let nsswitch = add_altfiles(&nsswitch)?;
rootfs_dfd.write_file_contents(path, 0o644, nsswitch.as_bytes())?;
Ok(())
}
pub fn convert_var_to_tmpfiles_d(
rootfs_dfd: i32,
mut cancellable: Pin<&mut crate::FFIGCancellable>,
) -> CxxResult<()> {
let rootfs = crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
let cancellable = &cancellable.gobj_wrap();
// TODO(lucab): unify this logic with the one in rpmostree-importer.cxx.
var_to_tmpfiles(&rootfs, Some(cancellable))?;
Ok(())
}
#[context("Converting /var to tmpfiles.d")]
fn var_to_tmpfiles(rootfs: &openat::Dir, cancellable: Option<&gio::Cancellable>) -> Result<()> {
/* List of files that are known to possibly exist, but in practice
* things work fine if we simply ignore them. Don't add something
* to this list unless you've verified it's handled correctly at
* runtime. (And really both in CentOS and Fedora)
*/
static KNOWN_STATE_FILES: &[&str] = &[
// https://bugzilla.redhat.com/show_bug.cgi?id=789407
"var/lib/systemd/random-seed",
"var/lib/systemd/catalog/database",
"var/lib/plymouth/boot-duration",
// These two are part of systemd's var.tmp
"var/log/wtmp",
"var/log/btmp",
];
let pwdb = PasswdDB::populate_new(rootfs)?;
// We never want to traverse into /run when making tmpfiles since it's a tmpfs
// Note that in a Fedora root, /var/run is a symlink, though on el7, it can be a dir.
// See: https://github.com/projectatomic/rpm-ostree/pull/831
rootfs
.remove_all("var/run")
.context("Failed to remove /var/run")?;
// Here, delete some files ahead of time to avoid emitting warnings
// for things that are known to be harmless.
for path in KNOWN_STATE_FILES {
rootfs
.remove_file_optional(*path)
.with_context(|| format!("unlinkat({})", path))?;
}
// Convert /var wholesale to tmpfiles.d. Note that with unified core, this
// code should no longer be necessary as we convert packages on import.
// Make output file world-readable, no reason why not to
// https://bugzilla.redhat.com/show_bug.cgi?id=1631794
rootfs.ensure_dir_all("usr/lib/tmpfiles.d", 0o755)?;
rootfs.write_file_with_sync(
"usr/lib/tmpfiles.d/rpm-ostree-1-autovar.conf",
0o644,
|bufwr| -> Result<()> {
let mut prefix = "var".to_string();
convert_path_to_tmpfiles_d_recurse(bufwr, &pwdb, &rootfs, &mut prefix, &cancellable)
.with_context(|| format!("Analyzing /{} content", prefix))?;
Ok(())
},
)?;
Ok(())
}
/// Recursively explore target directory and translate content to tmpfiles.d entries.
///
/// This proceeds depth-first and progressively deletes translated subpaths as it goes.
/// `prefix` is updated at each recursive step, so that in case of errors it can be
/// used to pinpoint the faulty path.
fn convert_path_to_tmpfiles_d_recurse(
tmpfiles_bufwr: &mut BufWriter<File>,
pwdb: &PasswdDB,
rootfs: &openat::Dir,
prefix: &mut String,
cancellable: &Option<&gio::Cancellable>,
) -> Result<()> {
use openat::SimpleType;
let current_prefix = prefix.clone();
for subpath in rootfs.list_dir(¤t_prefix)? {
if cancellable.map(|c| c.is_cancelled()).unwrap_or_default() {
bail!("Cancelled");
};
let subpath = subpath?;
let fname: &Utf8Path = Path::new(subpath.file_name()).try_into()?;
let full_path = format!("{}/{}", ¤t_prefix, fname);
let path_type = subpath.simple_type().unwrap_or(SimpleType::Other);
// Workaround for nfs-utils in RHEL7:
// https://bugzilla.redhat.com/show_bug.cgi?id=1427537
let mut retain_entry = false;
if path_type == SimpleType::File && full_path.starts_with("var/lib/nfs") {
retain_entry = true;
}
if !retain_entry && !matches!(path_type, SimpleType::Dir | SimpleType::Symlink) {
rootfs.remove_file_optional(&full_path)?;
println!("Ignoring non-directory/non-symlink '{}'", &full_path);
continue;
}
let filetype_char = match path_type {
SimpleType::Dir => 'd',
SimpleType::Symlink => 'L',
SimpleType::File => 'f',
x => unreachable!("invalid path type: {:?}", x),
};
write!(tmpfiles_bufwr, "{} ", filetype_char)?;
write!(tmpfiles_bufwr, "/{} ", full_path)?;
if path_type == SimpleType::Symlink {
let link_target = rootfs.read_link(&full_path)?;
write!(tmpfiles_bufwr, "- - - - ")?;
write!(tmpfiles_bufwr, "{}", link_target.display())?;
} else {
let meta = rootfs.metadata(&full_path)?;
let perm = meta.stat().st_mode & !libc::S_IFMT;
write!(tmpfiles_bufwr, "{:04o} ", perm)?;
let username = pwdb.lookup_user(meta.stat().st_uid)?;
write!(tmpfiles_bufwr, "{} ", username)?;
let groupname = pwdb.lookup_group(meta.stat().st_gid)?;
write!(tmpfiles_bufwr, "{} ", groupname)?;
write!(tmpfiles_bufwr, "- -")?;
};
write!(tmpfiles_bufwr, "\n")?;
if path_type == SimpleType::Dir {
// New subdirectory discovered, recurse into it.
*prefix = full_path.clone();
convert_path_to_tmpfiles_d_recurse(tmpfiles_bufwr, pwdb, rootfs, prefix, cancellable)?;
}
rootfs.remove_all(&full_path)?;
}
tmpfiles_bufwr.flush()?;
Ok(())
}
/// Walk over the root filesystem and perform some core conversions
/// from RPM conventions to OSTree conventions.
///
/// For example:
/// - Symlink /usr/local -> /var/usrlocal
/// - Symlink /var/lib/alternatives -> /usr/lib/alternatives
/// - Symlink /var/lib/vagrant -> /usr/lib/vagrant
#[context("Preparing symlinks in rootfs")]
pub fn rootfs_prepare_links(rootfs_dfd: i32) -> CxxResult<()> {
let rootfs = crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
rootfs
.remove_all("usr/local")
.context("Removing /usr/local")?;
let state_paths = &["usr/lib/alternatives", "usr/lib/vagrant"];
for entry in state_paths {
rootfs
.ensure_dir_all(*entry, 0o0755)
.with_context(|| format!("Creating '/{}'", entry))?;
}
let symlinks = &[
("../var/usrlocal", "usr/local"),
("../../usr/lib/alternatives", "var/lib/alternatives"),
("../../usr/lib/vagrant", "var/lib/vagrant"),
];
for (target, linkpath) in symlinks {
ensure_symlink(&rootfs, target, linkpath)?;
}
Ok(())
}
/// Create a symlink at `linkpath` if it does not exist, pointing to `target`.
///
/// This is idempotent and does not alter any content already existing at `linkpath`.
/// It returns `true` if the symlink has been created, `false` otherwise.
#[context("Symlinking '/{}' to empty directory '/{}'", linkpath, target)]
fn ensure_symlink(rootfs: &openat::Dir, target: &str, linkpath: &str) -> Result<bool> {
use openat::SimpleType;
if let Some(meta) = rootfs.metadata_optional(linkpath)? {
match meta.simple_type() {
SimpleType::Symlink => {
// We assume linkpath already points to the correct target,
// thus this short-circuits in an idempotent way.
return Ok(false);
}
SimpleType::Dir => rootfs.remove_dir(linkpath)?,
_ => bail!("Content already exists at link path"),
};
} else {
// For maximum compatibility, create parent directories too. This
// is necessary when we're doing layering on top of a base commit,
// and the /var will be empty. We should probably consider running
// systemd-tmpfiles to setup the temporary /var.
if let Some(parent) = Path::new(linkpath).parent() {
rootfs.ensure_dir_all(parent, 0o755)?;
}
}
rootfs.symlink(linkpath, target)?;
Ok(true)
}
pub fn workaround_selinux_cross_labeling(
rootfs_dfd: i32,
mut cancellable: Pin<&mut crate::FFIGCancellable>,
) -> CxxResult<()> {
let rootfs = crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
let cancellable = &cancellable.gobj_wrap();
tweak_selinux_timestamps(&rootfs, Some(cancellable))?;
Ok(())
}
/// Tweak timestamps on SELinux policy (workaround cross-host leak).
///
/// SELinux uses PCRE pre-compiled regexps for binary caches, which can
/// fail if the version of PCRE on the host differs from the version
/// which generated the cache (in the target root).
///
/// Note also this function is probably already broken in Fedora
/// 23+ from https://bugzilla.redhat.com/show_bug.cgi?id=1265406
#[context("Tweaking SELinux timestamps")]
fn tweak_selinux_timestamps(
rootfs: &openat::Dir,
cancellable: Option<&gio::Cancellable>,
) -> Result<()> {
// Handle the policy being in both /usr/etc and /etc since
// this function can be called at different points.
let policy_path = if rootfs.exists("usr/etc")? {
"usr/etc/selinux"
} else {
"etc/selinux"
};
if rootfs.exists(policy_path)? {
let mut prefix = policy_path.to_string();
workaround_selinux_cross_labeling_recurse(rootfs, &mut prefix, &cancellable)
.with_context(|| format!("Analyzing /{} content", prefix))?;
}
Ok(())
}
/// Recursively explore target directory and tweak SELinux policy timestamps.
///
/// `prefix` is updated at each recursive step, so that in case of errors it can be
/// used to pinpoint the faulty path.
fn workaround_selinux_cross_labeling_recurse(
rootfs: &openat::Dir,
prefix: &mut String,
cancellable: &Option<&gio::Cancellable>,
) -> Result<()> {
use openat::SimpleType;
let current_prefix = prefix.clone();
for subpath in rootfs.list_dir(¤t_prefix)? {
if cancellable.map(|c| c.is_cancelled()).unwrap_or_default() {
bail!("Cancelled");
};
let subpath = subpath?;
let full_path = {
let fname = subpath.file_name();
let path_name = fname
.to_str()
.ok_or_else(|| anyhow!("invalid non-UTF-8 path: {:?}", fname))?;
format!("{}/{}", ¤t_prefix, &path_name)
};
let path_type = subpath.simple_type().unwrap_or(SimpleType::Other);
if path_type == SimpleType::Dir {
// New subdirectory discovered, recurse into it.
*prefix = full_path.clone();
workaround_selinux_cross_labeling_recurse(rootfs, prefix, cancellable)?;
} else {
if let Some(nonbin_name) = full_path.strip_suffix(".bin") {
rootfs
.update_timestamps(nonbin_name)
.with_context(|| format!("Updating timestamps of /{}", nonbin_name))?;
}
}
}
Ok(())
}
pub fn prepare_rpmdb_base_location(
rootfs_dfd: i32,
mut cancellable: Pin<&mut crate::FFIGCancellable>,
) -> CxxResult<()> {
let rootfs = crate::ffiutil::ffi_view_openat_dir(rootfs_dfd);
let cancellable = &cancellable.gobj_wrap();
hardlink_rpmdb_base_location(&rootfs, Some(cancellable))?;
Ok(())
}
#[context("Hardlinking rpmdb to base location")]
fn hardlink_rpmdb_base_location(
rootfs: &openat::Dir,
cancellable: Option<&gio::Cancellable>,
) -> Result<bool> {
if !rootfs.exists(RPMOSTREE_RPMDB_LOCATION)? {
return Ok(false);
}
// Hardlink our own `/usr/lib/sysimage/rpm-ostree-base-db/` hierarchy
// to the well-known `/usr/share/rpm/`.
rootfs.ensure_dir_all(RPMOSTREE_BASE_RPMDB, 0o755)?;
rootfs.set_mode(RPMOSTREE_BASE_RPMDB, 0o755)?;
hardlink_hierarchy(
rootfs,
RPMOSTREE_RPMDB_LOCATION,
RPMOSTREE_BASE_RPMDB,
cancellable,
)?;
// And write a symlink from the proposed standard /usr/lib/sysimage/rpm
// to our /usr/share/rpm - eventually we will invert this.
rootfs.symlink(RPMOSTREE_SYSIMAGE_RPMDB, "../../share/rpm")?;
Ok(true)
}
/// Recursively hard-link `source` hierarchy to `target` directory.
///
/// Both directories must exist beforehand.
#[context("Hardlinking /{} to /{}", source, target)]
fn hardlink_hierarchy(
rootfs: &openat::Dir,
source: &str,
target: &str,
cancellable: Option<&gio::Cancellable>,
) -> Result<()> {
let mut prefix = "".to_string();
hardlink_recurse(rootfs, source, target, &mut prefix, &cancellable)
.with_context(|| format!("Analyzing /{}/{} content", source, prefix))?;
Ok(())
}
/// Recursively hard-link `source_prefix` to `dest_prefix.`
///
/// `relative_path` is updated at each recursive step, so that in case of errors
/// it can be used to pinpoint the faulty path.
fn hardlink_recurse(
rootfs: &openat::Dir,
source_prefix: &str,
dest_prefix: &str,
relative_path: &mut String,
cancellable: &Option<&gio::Cancellable>,
) -> Result<()> {
use openat::SimpleType;
let current_dir = relative_path.clone();
let current_source_dir = format!("{}/{}", source_prefix, relative_path);
for subpath in rootfs.list_dir(¤t_source_dir)? {
if cancellable.map(|c| c.is_cancelled()).unwrap_or_default() {
bail!("Cancelled");
};
let subpath = subpath?;
let full_path = {
let fname = subpath.file_name();
let path_name = fname
.to_str()
.ok_or_else(|| anyhow!("invalid non-UTF-8 path: {:?}", fname))?;
if !current_dir.is_empty() {
format!("{}/{}", current_dir, path_name)
} else {
path_name.to_string()
}
};
let source_path = format!("{}/{}", source_prefix, full_path);
let dest_path = format!("{}/{}", dest_prefix, full_path);
let path_type = subpath.simple_type().unwrap_or(SimpleType::Other);
if path_type == SimpleType::Dir {
// New subdirectory discovered, create it at the target.
let perms = rootfs.metadata(&source_path)?.stat().st_mode & !libc::S_IFMT;
rootfs.ensure_dir(&dest_path, perms)?;
rootfs.set_mode(&dest_path, perms)?;
// Recurse into the subdirectory.
*relative_path = full_path.clone();
hardlink_recurse(
rootfs,
source_prefix,
dest_prefix,
relative_path,
cancellable,
)?;
} else {
openat::hardlink(rootfs, source_path, rootfs, dest_path)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
#[test]
fn stripany() {
let s = "foo: bar";
assert!(strip_any_prefix(s, &[]).is_none());
assert_eq!(
strip_any_prefix(s, &["baz:", "foo:", "bar:"]).unwrap(),
("foo:", " bar")
);
}
#[test]
fn altfiles_replaced() {
let orig = r##"# blah blah nss stuff
# more blah blah
# passwd: db files
# shadow: db files
# shadow: db files
passwd: sss files systemd
shadow: files
group: sss files systemd
hosts: files resolve [!UNAVAIL=return] myhostname dns
automount: files sss
"##;
let expected = r##"# blah blah nss stuff
# more blah blah