forked from bootc-dev/bootc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
903 lines (847 loc) · 29.3 KB
/
config.rs
File metadata and controls
903 lines (847 loc) · 29.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
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
//! # Configuration for `bootc install`
//!
//! This module handles the TOML configuration file for `bootc install`.
use crate::spec::Bootloader;
use anyhow::{Context, Result};
use clap::ValueEnum;
use fn_error_context::context;
use serde::{Deserialize, Serialize};
#[cfg(feature = "install-to-disk")]
use super::baseline::BlockSetup;
/// Properties of the environment, such as the system architecture
/// Left open for future properties such as `platform.id`
pub(crate) struct EnvProperties {
pub(crate) sys_arch: String,
}
/// A well known filesystem type.
#[derive(clap::ValueEnum, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum Filesystem {
Xfs,
Ext4,
Btrfs,
}
impl std::fmt::Display for Filesystem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value().unwrap().get_name().fmt(f)
}
}
impl TryFrom<&str> for Filesystem {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"xfs" => Ok(Self::Xfs),
"ext4" => Ok(Self::Ext4),
"btrfs" => Ok(Self::Btrfs),
other => anyhow::bail!("Unknown filesystem: {}", other),
}
}
}
impl Filesystem {
pub(crate) fn supports_fsverity(&self) -> bool {
matches!(self, Self::Ext4 | Self::Btrfs)
}
}
/// The toplevel config entry for installation configs stored
/// in bootc/install (e.g. /etc/bootc/install/05-custom.toml)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub(crate) struct InstallConfigurationToplevel {
pub(crate) install: Option<InstallConfiguration>,
}
/// Configuration for a filesystem
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub(crate) struct RootFS {
#[serde(rename = "type")]
pub(crate) fstype: Option<Filesystem>,
}
/// This structure should only define "system" or "basic" filesystems; we are
/// not trying to generalize this into e.g. supporting `/var` or other ones.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub(crate) struct BasicFilesystems {
pub(crate) root: Option<RootFS>,
// TODO allow configuration of these other filesystems too
// pub(crate) xbootldr: Option<FilesystemCustomization>,
// pub(crate) esp: Option<FilesystemCustomization>,
}
/// Configuration for ostree repository
pub(crate) type OstreeRepoOpts = ostree_ext::repo_options::RepoOptions;
/// Configuration options for bootupd, responsible for setting up the bootloader.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub(crate) struct Bootupd {
/// Whether to skip writing the boot partition UUID to the bootloader configuration.
/// When true, bootupd is invoked with `--with-static-configs` instead of `--write-uuid`.
/// Defaults to false (UUIDs are written by default).
pub(crate) skip_boot_uuid: Option<bool>,
}
/// The serialized `[install]` section
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename = "install", rename_all = "kebab-case", deny_unknown_fields)]
pub(crate) struct InstallConfiguration {
/// Root filesystem type
pub(crate) root_fs_type: Option<Filesystem>,
/// Enabled block storage configurations
#[cfg(feature = "install-to-disk")]
pub(crate) block: Option<Vec<BlockSetup>>,
pub(crate) filesystem: Option<BasicFilesystems>,
/// Kernel arguments, applied at installation time
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) kargs: Option<Vec<String>>,
/// Deleting Kernel arguments, applied at installation time
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) karg_deletes: Option<Vec<String>>,
/// Supported architectures for this configuration
pub(crate) match_architectures: Option<Vec<String>>,
/// Ostree repository configuration
pub(crate) ostree: Option<OstreeRepoOpts>,
/// The stateroot name to use. Defaults to `default`
pub(crate) stateroot: Option<String>,
/// Source device specification for the root filesystem.
/// For example, `UUID=2e9f4241-229b-4202-8429-62d2302382e1` or `LABEL=rootfs`.
pub(crate) root_mount_spec: Option<String>,
/// Mount specification for the /boot filesystem.
pub(crate) boot_mount_spec: Option<String>,
/// Bootupd configuration
pub(crate) bootupd: Option<Bootupd>,
/// Bootloader to use (grub, systemd, none)
pub(crate) bootloader: Option<Bootloader>,
}
fn merge_basic<T>(s: &mut Option<T>, o: Option<T>, _env: &EnvProperties) {
if let Some(o) = o {
*s = Some(o);
}
}
trait Mergeable {
fn merge(&mut self, other: Self, env: &EnvProperties)
where
Self: Sized;
}
impl<T> Mergeable for Option<T>
where
T: Mergeable,
{
fn merge(&mut self, other: Self, env: &EnvProperties)
where
Self: Sized,
{
if let Some(other) = other {
if let Some(s) = self.as_mut() {
s.merge(other, env)
} else {
*self = Some(other);
}
}
}
}
impl Mergeable for RootFS {
/// Apply any values in other, overriding any existing values in `self`.
fn merge(&mut self, other: Self, env: &EnvProperties) {
merge_basic(&mut self.fstype, other.fstype, env)
}
}
impl Mergeable for BasicFilesystems {
/// Apply any values in other, overriding any existing values in `self`.
fn merge(&mut self, other: Self, env: &EnvProperties) {
self.root.merge(other.root, env)
}
}
impl Mergeable for OstreeRepoOpts {
/// Apply any values in other, overriding any existing values in `self`.
fn merge(&mut self, other: Self, env: &EnvProperties) {
merge_basic(
&mut self.bls_append_except_default,
other.bls_append_except_default,
env,
)
}
}
impl Mergeable for Bootupd {
/// Apply any values in other, overriding any existing values in `self`.
fn merge(&mut self, other: Self, env: &EnvProperties) {
merge_basic(&mut self.skip_boot_uuid, other.skip_boot_uuid, env)
}
}
impl Mergeable for InstallConfiguration {
/// Apply any values in other, overriding any existing values in `self`.
fn merge(&mut self, other: Self, env: &EnvProperties) {
// if arch is specified, only merge config if it matches the current arch
// if arch is not specified, merge config unconditionally
if other
.match_architectures
.map(|a| a.contains(&env.sys_arch))
.unwrap_or(true)
{
merge_basic(&mut self.root_fs_type, other.root_fs_type, env);
#[cfg(feature = "install-to-disk")]
merge_basic(&mut self.block, other.block, env);
self.filesystem.merge(other.filesystem, env);
self.ostree.merge(other.ostree, env);
merge_basic(&mut self.stateroot, other.stateroot, env);
merge_basic(&mut self.root_mount_spec, other.root_mount_spec, env);
merge_basic(&mut self.boot_mount_spec, other.boot_mount_spec, env);
self.bootupd.merge(other.bootupd, env);
merge_basic(&mut self.bootloader, other.bootloader, env);
if let Some(other_kargs) = other.kargs {
self.kargs
.get_or_insert_with(Default::default)
.extend(other_kargs)
}
if let Some(other_karg_deletes) = other.karg_deletes {
self.karg_deletes
.get_or_insert_with(Default::default)
.extend(other_karg_deletes)
}
}
}
}
impl InstallConfiguration {
/// Set defaults (e.g. `block`), and also handle fields that can be specified multiple ways
/// by synchronizing the values of the fields to ensure they're the same.
///
/// - install.root-fs-type is synchronized with install.filesystems.root.type; if
/// both are set, then the latter takes precedence
pub(crate) fn canonicalize(&mut self) {
// New canonical form wins.
if let Some(rootfs_type) = self.filesystem_root().and_then(|f| f.fstype.as_ref()) {
self.root_fs_type = Some(*rootfs_type)
} else if let Some(rootfs) = self.root_fs_type.as_ref() {
let fs = self.filesystem.get_or_insert_with(Default::default);
let root = fs.root.get_or_insert_with(Default::default);
root.fstype = Some(*rootfs);
}
#[cfg(feature = "install-to-disk")]
if self.block.is_none() {
self.block = Some(vec![BlockSetup::Direct]);
}
}
/// Convenience helper to access the root filesystem
pub(crate) fn filesystem_root(&self) -> Option<&RootFS> {
self.filesystem.as_ref().and_then(|fs| fs.root.as_ref())
}
// Remove all configuration which is handled by `install to-filesystem`.
pub(crate) fn filter_to_external(&mut self) {
self.kargs.take();
self.karg_deletes.take();
}
#[cfg(feature = "install-to-disk")]
pub(crate) fn get_block_setup(&self, default: Option<BlockSetup>) -> Result<BlockSetup> {
let valid_block_setups = self.block.as_deref().unwrap_or_default();
let default_block = valid_block_setups.iter().next().ok_or_else(|| {
anyhow::anyhow!("Empty block storage configuration in install configuration")
})?;
let block_setup = default.as_ref().unwrap_or(default_block);
if !valid_block_setups.contains(block_setup) {
anyhow::bail!("Block setup {block_setup:?} is not enabled in installation config");
}
Ok(*block_setup)
}
}
#[context("Loading configuration")]
/// Load the install configuration, merging all found configuration files.
pub(crate) fn load_config() -> Result<Option<InstallConfiguration>> {
let env = EnvProperties {
sys_arch: std::env::consts::ARCH.to_string(),
};
const SYSTEMD_CONVENTIONAL_BASES: &[&str] = &["/usr/lib", "/usr/local/lib", "/etc", "/run"];
let fragments = liboverdrop::scan(SYSTEMD_CONVENTIONAL_BASES, "bootc/install", &["toml"], true);
let mut config: Option<InstallConfiguration> = None;
for (_name, path) in fragments {
let buf = std::fs::read_to_string(&path)?;
let mut unused = std::collections::HashSet::new();
let de = toml::Deserializer::parse(&buf).with_context(|| format!("Parsing {path:?}"))?;
let mut c: InstallConfigurationToplevel = serde_ignored::deserialize(de, |path| {
unused.insert(path.to_string());
})
.with_context(|| format!("Parsing {path:?}"))?;
for key in unused {
eprintln!("warning: {path:?}: Unknown key {key}");
}
if let Some(config) = config.as_mut() {
if let Some(install) = c.install {
tracing::debug!("Merging install config: {install:?}");
config.merge(install, &env);
}
} else {
// Only set the config if it matches the current arch
// If no arch is specified, set the config unconditionally
if let Some(ref mut install) = c.install {
if install
.match_architectures
.as_ref()
.map(|a| a.contains(&env.sys_arch))
.unwrap_or(true)
{
config = c.install;
}
}
}
}
if let Some(config) = config.as_mut() {
config.canonicalize();
}
Ok(config)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
/// Verify that we can parse our default config file
fn test_parse_config() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
assert_eq!(install.root_fs_type.unwrap(), Filesystem::Xfs);
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
root_fs_type: Some(Filesystem::Ext4),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.root_fs_type.as_ref().copied().unwrap(),
Filesystem::Ext4
);
// This one shouldn't have been set
assert!(install.filesystem_root().is_none());
install.canonicalize();
assert_eq!(install.root_fs_type.as_ref().unwrap(), &Filesystem::Ext4);
assert_eq!(
install.filesystem_root().unwrap().fstype.unwrap(),
Filesystem::Ext4
);
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "ext4"
kargs = ["console=ttyS0", "foo=bar"]
karg-deletes = ["debug", "bar=baz"]
"##,
)
.unwrap();
let mut install = c.install.unwrap();
assert_eq!(install.root_fs_type.unwrap(), Filesystem::Ext4);
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
karg_deletes: Some(
["baz", "bar=baz"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(install.root_fs_type.unwrap(), Filesystem::Ext4);
assert_eq!(
install.kargs,
Some(
["console=ttyS0", "foo=bar", "console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
assert_eq!(
install.karg_deletes,
Some(
["debug", "bar=baz", "baz", "bar=baz"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
}
#[test]
fn test_parse_filesystems() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install.filesystem.root]
type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
assert_eq!(
install.filesystem_root().unwrap().fstype.unwrap(),
Filesystem::Xfs
);
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
filesystem: Some(BasicFilesystems {
root: Some(RootFS {
fstype: Some(Filesystem::Ext4),
}),
}),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.filesystem_root().unwrap().fstype.unwrap(),
Filesystem::Ext4
);
}
#[test]
fn test_parse_block() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install.filesystem.root]
type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
// Verify the default (but note canonicalization mutates)
{
let mut install = install.clone();
install.canonicalize();
assert_eq!(install.get_block_setup(None).unwrap(), BlockSetup::Direct);
}
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
block: Some(vec![]),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
// Should be set, but zero length
assert_eq!(install.block.as_ref().unwrap().len(), 0);
assert!(install.get_block_setup(None).is_err());
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
block = ["tpm2-luks"]"##,
)
.unwrap();
let mut install = c.install.unwrap();
install.canonicalize();
assert_eq!(install.block.as_ref().unwrap().len(), 1);
assert_eq!(install.get_block_setup(None).unwrap(), BlockSetup::Tpm2Luks);
// And verify passing a disallowed config is an error
assert!(install.get_block_setup(Some(BlockSetup::Direct)).is_err());
}
#[test]
/// Verify that kargs are only applied to supported architectures
fn test_arch() {
// no arch specified, kargs ensure that kargs are applied unconditionally
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.kargs,
Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
let env = EnvProperties {
sys_arch: "aarch64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.kargs,
Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
// one arch matches and one doesn't, ensure that kargs are only applied for the matching arch
let env = EnvProperties {
sys_arch: "aarch64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=ttyS0", "foo=bar"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
match_architectures: Some(["x86_64"].into_iter().map(ToOwned::to_owned).collect()),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(install.kargs, None);
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
match_architectures: Some(["aarch64"].into_iter().map(ToOwned::to_owned).collect()),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.kargs,
Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
// multiple arch specified, ensure that kargs are applied to both archs
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
match_architectures: Some(
["x86_64", "aarch64"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.kargs,
Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
let env = EnvProperties {
sys_arch: "aarch64".to_string(),
};
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
root-fs-type = "xfs"
"##,
)
.unwrap();
let mut install = c.install.unwrap();
let other = InstallConfigurationToplevel {
install: Some(InstallConfiguration {
kargs: Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
match_architectures: Some(
["x86_64", "aarch64"]
.into_iter()
.map(ToOwned::to_owned)
.collect(),
),
..Default::default()
}),
};
install.merge(other.install.unwrap(), &env);
assert_eq!(
install.kargs,
Some(
["console=tty0", "nosmt"]
.into_iter()
.map(ToOwned::to_owned)
.collect()
)
);
}
#[test]
fn test_parse_ostree() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
// Table-driven test cases for parsing bls-append-except-default
let parse_cases = [
("console=ttyS0", "console=ttyS0"),
("console=ttyS0,115200n8", "console=ttyS0,115200n8"),
("rd.lvm.lv=vg/root", "rd.lvm.lv=vg/root"),
];
for (input, expected) in parse_cases {
let toml_str = format!(
r#"[install.ostree]
bls-append-except-default = "{input}"
"#
);
let c: InstallConfigurationToplevel = toml::from_str(&toml_str).unwrap();
assert_eq!(
c.install
.unwrap()
.ostree
.unwrap()
.bls_append_except_default
.unwrap(),
expected
);
}
// Test merging: other config should override original
let mut install: InstallConfiguration = toml::from_str(
r#"[ostree]
bls-append-except-default = "console=ttyS0"
"#,
)
.unwrap();
let other = InstallConfiguration {
ostree: Some(OstreeRepoOpts {
bls_append_except_default: Some("console=tty0".to_string()),
..Default::default()
}),
..Default::default()
};
install.merge(other, &env);
assert_eq!(
install.ostree.unwrap().bls_append_except_default.unwrap(),
"console=tty0"
);
}
#[test]
fn test_parse_stateroot() {
let c: InstallConfigurationToplevel = toml::from_str(
r#"[install]
stateroot = "custom"
"#,
)
.unwrap();
assert_eq!(c.install.unwrap().stateroot.unwrap(), "custom");
}
#[test]
fn test_merge_stateroot() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let mut install: InstallConfiguration = toml::from_str(
r#"stateroot = "original"
"#,
)
.unwrap();
let other = InstallConfiguration {
stateroot: Some("newroot".to_string()),
..Default::default()
};
install.merge(other, &env);
assert_eq!(install.stateroot.unwrap(), "newroot");
}
#[test]
fn test_parse_mount_specs() {
let c: InstallConfigurationToplevel = toml::from_str(
r#"[install]
root-mount-spec = "LABEL=rootfs"
boot-mount-spec = "UUID=abcd-1234"
"#,
)
.unwrap();
let install = c.install.unwrap();
assert_eq!(install.root_mount_spec.unwrap(), "LABEL=rootfs");
assert_eq!(install.boot_mount_spec.unwrap(), "UUID=abcd-1234");
}
#[test]
fn test_merge_mount_specs() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let mut install: InstallConfiguration = toml::from_str(
r#"root-mount-spec = "UUID=old"
boot-mount-spec = "UUID=oldboot"
"#,
)
.unwrap();
let other = InstallConfiguration {
root_mount_spec: Some("LABEL=newroot".to_string()),
..Default::default()
};
install.merge(other, &env);
// root_mount_spec should be overridden
assert_eq!(install.root_mount_spec.as_deref().unwrap(), "LABEL=newroot");
// boot_mount_spec should remain unchanged
assert_eq!(install.boot_mount_spec.as_deref().unwrap(), "UUID=oldboot");
}
/// Empty mount specs are valid and signal to omit mount kargs entirely.
/// See https://github.com/bootc-dev/bootc/issues/1441
#[test]
fn test_parse_empty_mount_specs() {
let c: InstallConfigurationToplevel = toml::from_str(
r#"[install]
root-mount-spec = ""
boot-mount-spec = ""
"#,
)
.unwrap();
let install = c.install.unwrap();
assert_eq!(install.root_mount_spec.as_deref().unwrap(), "");
assert_eq!(install.boot_mount_spec.as_deref().unwrap(), "");
}
#[test]
fn test_parse_bootupd_skip_boot_uuid() {
// Test parsing true
let c: InstallConfigurationToplevel = toml::from_str(
r#"[install.bootupd]
skip-boot-uuid = true
"#,
)
.unwrap();
assert_eq!(
c.install.unwrap().bootupd.unwrap().skip_boot_uuid.unwrap(),
true
);
// Test parsing false
let c: InstallConfigurationToplevel = toml::from_str(
r#"[install.bootupd]
skip-boot-uuid = false
"#,
)
.unwrap();
assert_eq!(
c.install.unwrap().bootupd.unwrap().skip_boot_uuid.unwrap(),
false
);
// Test default (not specified) is None
let c: InstallConfigurationToplevel = toml::from_str(
r#"[install]
root-fs-type = "xfs"
"#,
)
.unwrap();
assert!(c.install.unwrap().bootupd.is_none());
}
#[test]
fn test_merge_bootupd_skip_boot_uuid() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
let mut install: InstallConfiguration = toml::from_str(
r#"[bootupd]
skip-boot-uuid = false
"#,
)
.unwrap();
let other = InstallConfiguration {
bootupd: Some(Bootupd {
skip_boot_uuid: Some(true),
}),
..Default::default()
};
install.merge(other, &env);
// skip_boot_uuid should be overridden to true
assert_eq!(install.bootupd.unwrap().skip_boot_uuid.unwrap(), true);
}
}
#[test]
fn test_parse_bootloader() {
let env = EnvProperties {
sys_arch: "x86_64".to_string(),
};
// 1. Test parsing "none"
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
bootloader = "none"
"##,
)
.unwrap();
assert_eq!(c.install.unwrap().bootloader, Some(Bootloader::None));
// 2. Test parsing "grub"
let c: InstallConfigurationToplevel = toml::from_str(
r##"[install]
bootloader = "grub"
"##,
)
.unwrap();
assert_eq!(c.install.unwrap().bootloader, Some(Bootloader::Grub));
// 3. Test merging
// Initial config has "systemd"
let mut install: InstallConfiguration = toml::from_str(
r#"bootloader = "systemd"
"#,
)
.unwrap();
// Incoming config has "none"
let other = InstallConfiguration {
bootloader: Some(Bootloader::None),
..Default::default()
};
// Merge should overwrite systemd with none
install.merge(other, &env);
assert_eq!(install.bootloader, Some(Bootloader::None));
}