Skip to content

Commit

Permalink
Add root and boot option for osmet image
Browse files Browse the repository at this point in the history
 - OSBuild does not allow udev rules in loop
devices, for this reason can not rely on labels on
the partitions to create the osmet image, we
need to skip this part and pass only the fstype
for the it to be mounted.

Signed-off-by: Renata Ravanelli <[email protected]>
  • Loading branch information
ravanelli committed Aug 13, 2024
1 parent 85ba425 commit a94fa42
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
65 changes: 47 additions & 18 deletions src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,37 @@ impl Disk {
Ok(Disk { path: canon_path })
}

pub fn mount_partition_by_label(&self, label: &str, flags: mount::MsFlags) -> Result<Mount> {
// get partition list
let partitions = self.get_partitions()?;
if partitions.is_empty() {
bail!("couldn't find any partitions on {}", self.path);
}

// find the partition with the matching label
let matching_partitions = partitions
pub fn mount_partition_by_label(&self, label: &str, scan_partition: bool, flags: mount::MsFlags) -> Result<Mount> {
let part: &Partition;
let partitions;
let fstype;
if scan_partition {
// get partition list
partitions = self.get_partitions()?;
if partitions.is_empty() {
bail!("couldn't find any partitions on {}", self.path);
}
// find the partition with the matching label
let matching_partitions = partitions
.iter()
.filter(|d| d.label.as_ref().unwrap_or(&"".to_string()) == label)
.collect::<Vec<&Partition>>();
let part = match matching_partitions.len() {
0 => bail!("couldn't find {} device for {}", label, self.path),
1 => matching_partitions[0],
_ => bail!(
"found multiple devices on {} with label \"{}\"",
self.path,
label
),
};
part = match matching_partitions.len() {
0 => bail!("couldn't find {} device for {}", label, self.path),
1 => matching_partitions[0],
_ => bail!(
"found multiple devices on {} with label \"{}\"",
self.path,
label
),
};
} else {
// Udev rules do not work in OSBuild.
// Get only the fstype that is required
// in OSBuild in order to mount it.
fstype = self.get_fstype(label)?;
part = &fstype
}

// mount it
match &part.fstype {
Expand All @@ -99,6 +109,25 @@ impl Disk {
}
}

fn get_fstype(&self, path: &str) -> Result<Partition> {
let mut cmd = Command::new("blkid");
cmd.arg(path);
let output = cmd_output(&mut cmd)?;
let mut _result: Vec<HashMap<String, String>> = Vec::new();
for line in output.lines() {
_result.push(split_blkid_line(line));
}
let result = Partition {
path: path.to_string(),
fstype: _result[0].get("TYPE").map(<_>::to_string),
swap: false,
parent: "".to_string(),
mountpoint: None,
label: Some("".to_string()),
};
Ok(result)
}

fn get_partitions(&self) -> Result<Vec<Partition>> {
// walk each device in the output
let mut result: Vec<Partition> = Vec::new();
Expand Down
6 changes: 6 additions & 0 deletions src/cmdline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ pub struct PackOsmetConfig {
/// Use worse compression, for development builds
#[arg(long)]
pub fast: bool,
/// Boot device path
#[arg(long, default_value_t)]
pub boot: String,
#[arg(long, default_value_t)]
/// Root device path
pub root: String,
/// Source device
#[arg(value_name = "DEV")]
pub device: String,
Expand Down
2 changes: 1 addition & 1 deletion src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ fn write_disk(
|| network_config.is_some()
|| cfg!(target_arch = "s390x")
{
let mount = Disk::new(device)?.mount_partition_by_label("boot", mount::MsFlags::empty())?;
let mount = Disk::new(device)?.mount_partition_by_label("boot", true, mount::MsFlags::empty())?;
if let Some(ignition) = ignition.as_ref() {
write_ignition(mount.mountpoint(), &config.ignition_hash, ignition)
.context("writing Ignition configuration")?;
Expand Down
13 changes: 10 additions & 3 deletions src/osmet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,17 @@ pub fn pack_osmet(config: PackOsmetConfig) -> Result<()> {
// First, mount the two main partitions we want to suck out data from: / and /boot. Note
// MS_RDONLY; this also ensures that the partition isn't already mounted rw elsewhere.
let disk = Disk::new(&config.device)?;
let boot = disk.mount_partition_by_label("boot", mount::MsFlags::MS_RDONLY)?;
let root = disk.mount_partition_by_label("root", mount::MsFlags::MS_RDONLY)?;

// now, we do a first scan of the boot partition and pick up files over a certain size
let boot;
let root;
if !config.root.trim().is_empty() && !config.boot.trim().is_empty() {
boot = disk.mount_partition_by_label(&config.boot, false, mount::MsFlags::empty())?;
root = disk.mount_partition_by_label(&config.root, false, mount::MsFlags::empty())?;
} else {
boot = disk.mount_partition_by_label("boot", true, mount::MsFlags::MS_RDONLY)?;
root = disk.mount_partition_by_label("root", true, mount::MsFlags::MS_RDONLY)?;
}

let boot_files = prescan_boot_partition(&boot)?;

// generate the primary OSTree object <--> disk block mappings, and also try to match up boot
Expand Down

0 comments on commit a94fa42

Please sign in to comment.