Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install/baseline: Drop separate /dev mount #607

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct Device {
pub(crate) label: Option<String>,
pub(crate) fstype: Option<String>,
pub(crate) children: Option<Vec<Device>>,
pub(crate) size: Option<String>,
}

impl Device {
Expand All @@ -53,7 +54,7 @@ pub(crate) fn wipefs(dev: &Utf8Path) -> Result<()> {

fn list_impl(dev: Option<&Utf8Path>) -> Result<Vec<Device>> {
let o = Command::new("lsblk")
.args(["-J", "-o", "NAME,SERIAL,MODEL,LABEL,FSTYPE"])
.args(["-J", "-o", "NAME,SERIAL,MODEL,LABEL,FSTYPE,SIZE"])
.args(dev)
.output()?;
if !o.status.success() {
Expand Down
39 changes: 15 additions & 24 deletions lib/src/install/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ fn mkfs<'a>(
label: &str,
opts: impl IntoIterator<Item = &'a str>,
) -> Result<uuid::Uuid> {
let devinfo = crate::blockdev::list_dev(dev.into())?;
let size = devinfo.size.as_deref().unwrap_or("(unknown)");
let u = uuid::Uuid::new_v4();
let mut t = Task::new(
&format!("Creating {label} filesystem ({fs})"),
&format!("Creating {label} filesystem ({fs}) on device {dev} (size={size})"),
format!("mkfs.{fs}"),
);
match fs {
Expand Down Expand Up @@ -170,6 +172,8 @@ pub(crate) fn install_create_rootfs(
// Verify that the target is empty (if not already wiped in particular, but it's
// also good to verify that the wipe worked)
let device = crate::blockdev::list_dev(&opts.device)?;
// Canonicalize devpath
let devpath: Utf8PathBuf = device.path().into();

// Handle wiping any existing data
if opts.wipe {
Expand All @@ -193,20 +197,6 @@ pub(crate) fn install_create_rootfs(
if mntdir.exists() {
std::fs::remove_dir_all(&mntdir)?;
}
let devdir = mntdir.join("dev");
std::fs::create_dir_all(&devdir)?;
Task::new("Mounting devtmpfs", "mount")
.args(["devtmpfs", "-t", "devtmpfs", devdir.as_str()])
.quiet()
.run()?;

// Now at this point, our /dev is a stale snapshot because we don't have udev running.
// So from hereon after, we prefix devices with our temporary devtmpfs mount.
let reldevice = opts
.device
.strip_prefix("/dev/")
.context("Absolute device path in /dev/ required")?;
let device = devdir.join(reldevice);

// Use the install configuration to find the block setup, if we have one
let block_setup = if let Some(config) = state.install_config.as_ref() {
Expand Down Expand Up @@ -245,7 +235,7 @@ pub(crate) fn install_create_rootfs(
// sgdisk is too verbose
sgdisk.cmd.stdout(Stdio::null());
sgdisk.cmd.arg("-Z");
sgdisk.cmd.arg(&device);
sgdisk.cmd.arg(device.path());
sgdisk.cmd.args(["-U", "R"]);
#[allow(unused_assignments)]
if cfg!(target_arch = "x86_64") {
Expand Down Expand Up @@ -316,27 +306,28 @@ pub(crate) fn install_create_rootfs(
{
let mut f = std::fs::OpenOptions::new()
.write(true)
.open(&device)
.with_context(|| format!("opening {device}"))?;
.open(&devpath)
.with_context(|| format!("opening {devpath}"))?;
crate::blockdev::reread_partition_table(&mut f, true)
.context("Rereading partition table")?;
}

// Full udev sync; it'd obviously be better to await just the devices
// we're targeting, but this is a simple coarse hammer.
crate::blockdev::udev_settle()?;

// Now inspect the partitioned device again so we can find the names of the child devices.
let device_partitions = crate::blockdev::list_dev(&device)?
let device_partitions = crate::blockdev::list_dev(&devpath)?
.children
.ok_or_else(|| anyhow::anyhow!("Failed to find children after partitioning"))?;
// Given a partition number, return the path to its device.
let findpart = |idx: u32| -> Result<String> {
// checked_sub is here because our partition numbers start at 1, but the vec starts at 0
let devname = device_partitions
let devpath = device_partitions
.get(idx.checked_sub(1).unwrap() as usize)
.ok_or_else(|| anyhow::anyhow!("Missing partition for index {idx}"))?
.name
.as_str();
Ok(devdir.join(devname).to_string())
.path();
Ok(devpath)
};

let base_rootdev = findpart(rootpn)?;
Expand Down Expand Up @@ -448,7 +439,7 @@ pub(crate) fn install_create_rootfs(
};
Ok(RootSetup {
luks_device,
device,
device: devpath,
rootfs,
rootfs_fd,
rootfs_uuid: Some(root_uuid.to_string()),
Expand Down
Loading