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

Update dependencies and refactor mount operations #329

Closed
wants to merge 11 commits into from
915 changes: 482 additions & 433 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ categories = ["os::unix-apis"]
keywords = ["linux", "distribution", "installer"]
authors = [
"Jeremy Soller <[email protected]>",
"Michael Aaron Murphy <[email protected]>"
"Michael Aaron Murphy <[email protected]>",
]
license = "LGPL-3.0"
readme = "README.md"
edition = "2018"

[workspace]
members = [
"cli",
"crates/*",
"ffi",
]
members = ["cli", "crates/*", "ffi"]

[lib]
name = "distinst"
Expand All @@ -30,7 +26,7 @@ pbr = "1.0.2"
[dependencies]
cascade = "1.0"
dirs = "3.0"
disk-types = { path = "crates/disk-types"}
disk-types = { path = "crates/disk-types" }
distinst-bootloader = { path = "crates/bootloader" }
distinst-chroot = { path = "crates/chroot" }
distinst-disks = { path = "crates/disks" }
Expand All @@ -55,7 +51,7 @@ os-release = "0.1.0"
partition-identity = "0.2.8"
proc-mounts = "0.2.4"
rayon = "1.3.0"
sys-mount = "1.2.1"
sys-mount = "^3.0.1"
tempdir = "0.3.7"
bitflags = "1.2.1"
err-derive = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion cli/src/configure/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) fn decrypt(disks: &mut Disks, decrypt: Option<Values>) -> Result<(),
let (device, pv) = (Path::new(values[0]), values[1].into());

let (mut pass, mut keydata) = (None, None);
parse_key(&values[2], &mut pass, &mut keydata)?;
parse_key(values[2], &mut pass, &mut keydata)?;

disks
.decrypt_partition(device, &LvmEncryption::new(pv, pass, keydata))
Expand Down
10 changes: 5 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ fn main() {
tzs_ = Timezones::new().expect("failed to get timzones");
let zone = tzs_
.zones()
.into_iter()
.iter()
.find(|z| z.name() == zone)
.expect(&format!("failed to find zone: {}", zone));
.unwrap_or_else(|| panic!("failed to find zone: {}", zone));
let region = zone
.regions()
.into_iter()
.iter()
.find(|r| r.name() == region)
.expect(&format!("failed to find region: {}", region));
.unwrap_or_else(|| panic!("failed to find region: {}", region));
Some(region.clone())
}
None => None,
Expand Down Expand Up @@ -559,5 +559,5 @@ fn find_partition_mut(
disk: &mut Disk,
partition: i32,
) -> Result<&mut PartitionInfo, DistinstError> {
disk.get_partition_mut(partition).ok_or_else(|| DistinstError::PartitionNotFound { partition })
disk.get_partition_mut(partition).ok_or(DistinstError::PartitionNotFound { partition })
}
9 changes: 6 additions & 3 deletions crates/chroot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
[package]
name = "distinst-chroot"
version = "0.1.0"
authors = ["Jeremy Soller <[email protected]>", "Michael Aaron Murphy <[email protected]>"]
authors = [
"Jeremy Soller <[email protected]>",
"Michael Aaron Murphy <[email protected]>",
]
description = "Convenience wrapper for executing external commands, with chroot support"
repository = "https://github.com/pop-os/distinst"
readme = "README.md"
license = "MIT"
keywords = ["chroot", "command", "process", "distinst"]
keywords = ["chroot", "command", "process", "distinst"]
categories = ["os", "os::unix-apis"]
edition = "2018"

[dependencies]
sys-mount = "1.2.1"
sys-mount = "^3.0.1"
cascade = "1.0"
log = "0.4.8"
libc = "0.2.68"
53 changes: 38 additions & 15 deletions crates/chroot/src/chroot.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
use crate::command::Command;
use std::{
ffi::OsStr,
io::Result,
path::{Path, PathBuf},
process::Stdio,
};
use sys_mount::*;
use crate::command::Command;

/// Defines the location where a `chroot` will be performed, as well as storing
/// handles to all of the binding mounts that the chroot requires.
pub struct Chroot<'a> {
pub path: PathBuf,
dev_mount: Mount,
pts_mount: Mount,
pub path: PathBuf,
dev_mount: Mount,
pts_mount: Mount,
proc_mount: Mount,
run_mount: Mount,
sys_mount: Mount,
run_mount: Mount,
sys_mount: Mount,
clear_envs: bool,
envs: Vec<(&'a str, &'a str)>,
envs: Vec<(&'a str, &'a str)>,
}

impl<'a> Chroot<'a> {
/// Performs binding mounts of all required paths to ensure that a chroot
/// is successful.
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
let path = path.as_ref().canonicalize()?;
let dev_mount = Mount::new("/dev", &path.join("dev"), "none", MountFlags::BIND, None)?;
let pts_mount =
Mount::new("/dev/pts", &path.join("dev").join("pts"), "none", MountFlags::BIND, None)?;
let proc_mount = Mount::new("/proc", &path.join("proc"), "none", MountFlags::BIND, None)?;
let run_mount = Mount::new("/run", &path.join("run"), "none", MountFlags::BIND, None)?;
let sys_mount = Mount::new("/sys", &path.join("sys"), "none", MountFlags::BIND, None)?;
let dev_mount = Mount::builder()
.fstype("none")
.flags(MountFlags::BIND)
.mount("/dev", path.join("dev"))?;

let pts_mount = Mount::builder()
.fstype("none")
.flags(MountFlags::BIND)
.mount("/dev/pts", path.join("dev").join("pts"))?;

let proc_mount = Mount::builder()
.fstype("none")
.flags(MountFlags::BIND)
.mount("/proc", path.join("proc"))?;

let run_mount = Mount::builder()
.fstype("none")
.flags(MountFlags::BIND)
.mount("/run", path.join("run"))?;

let sys_mount = Mount::builder()
.fstype("none")
.flags(MountFlags::BIND)
.mount("/sys", path.join("sys"))?;

Ok(Chroot {
path,
dev_mount,
Expand All @@ -44,10 +63,14 @@ impl<'a> Chroot<'a> {
}

/// Set an environment variable to define for this chroot.
pub fn env(&mut self, key: &'a str, value: &'a str) { self.envs.push((key, value)); }
pub fn env(&mut self, key: &'a str, value: &'a str) {
self.envs.push((key, value));
}

/// Clear all environment variables for this chroot.
pub fn clear_envs(&mut self, clear: bool) { self.clear_envs = clear; }
pub fn clear_envs(&mut self, clear: bool) {
self.clear_envs = clear;
}

/// Executes an external command with `chroot`.
pub fn command<S: AsRef<OsStr>, T: AsRef<OsStr>, I: IntoIterator<Item = T>>(
Expand Down
9 changes: 6 additions & 3 deletions crates/disk-ops/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[package]
name = "distinst-disk-ops"
version = "0.1.0"
authors = ["Jeremy Soller <[email protected]>", "Michael Aaron Murphy <[email protected]>"]
authors = [
"Jeremy Soller <[email protected]>",
"Michael Aaron Murphy <[email protected]>",
]
description = "Operations for adding, removing, moving, and resizing partitions on a disk."
repository = "https://github.com/pop-os/distinst"
readme = "README.md"
license = "MIT"
keywords = ["disk", "partition"]
keywords = ["disk", "partition"]
categories = ["filesystem", "os"]
edition = "2018"

Expand All @@ -17,7 +20,7 @@ disk-types = { path = "../disk-types" }
distinst-external-commands = { path = "../external" }
log = "0.4.8"
tempdir = "0.3.7"
sys-mount = "1.2.1"
sys-mount = "^3.0.1"
libparted = "0.1.4"
rayon = "1.3.0"
smart-default = "0.6.0"
2 changes: 1 addition & 1 deletion crates/disk-ops/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl BlockCoordinates {
pub fn resize_to(&mut self, new_len: u64) {
let offset = (self.end - self.start) as i64 - new_len as i64;
if offset < 0 {
self.end += offset.abs() as u64;
self.end += offset.unsigned_abs();
} else {
self.end -= offset as u64;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/disk-ops/src/mklabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ pub fn zero<P: AsRef<Path>>(device: P, sectors: u64, offset: u64) -> io::Result<
file.seek(SeekFrom::Start(512 * offset)).map(|_| ())?;
}

(0..sectors).map(|_| file.write(&zeroed_sector).map(|_| ())).collect()
(0..sectors).try_for_each(|_| file.write(&zeroed_sector).map(|_| ()))
})
}
2 changes: 1 addition & 1 deletion crates/disk-ops/src/mkpart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
{
// Create a new geometry from the start sector and length of the new partition.
let length = partition.get_sector_end() - partition.get_sector_start();
let geometry = Geometry::new(&device, partition.get_sector_start() as i64, length as i64)
let geometry = Geometry::new(device, partition.get_sector_start() as i64, length as i64)
.map_err(|why| io::Error::new(why.kind(), format!("failed to create geometry: {}", why)))?;

// Convert our internal partition type enum into libparted's variant.
Expand Down
2 changes: 1 addition & 1 deletion crates/disk-ops/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<'a> CreatePartitions<'a> {
// Attempt to sync three times before returning an error.
for attempt in 0..3 {
::std::thread::sleep(::std::time::Duration::from_secs(1));
let result = blockdev(self.device_path, &["--flushbufs", "--rereadpt"]);
let result = blockdev(self.device_path, ["--flushbufs", "--rereadpt"]);
if result.is_err() && attempt == 2 {
result.map_err(|why| {
io::Error::new(why.kind(), format!("failed to synchronize disk: {}", why))
Expand Down
2 changes: 1 addition & 1 deletion crates/disk-ops/src/parted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn open_disk<'a>(device: &'a mut Device) -> io::Result<PedDisk<'a>> {
why.kind(),
format!(
"failed to create new partition table on {:?}: {}",
(&*device).path(),
(*device).path(),
why
),
)
Expand Down
10 changes: 5 additions & 5 deletions crates/disk-ops/src/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn resize_partition<P: AsRef<Path>>(
args: &[&str],
size: &str,
path: P,
fs: &str,
_fs: &str,
options: u8,
) -> io::Result<()> {
info!("resizing {} to {}", path.as_ref().display(), size);
Expand All @@ -116,7 +116,7 @@ pub fn resize_partition<P: AsRef<Path>>(
// Attempt to sync three times before returning an error.
for attempt in 0..3 {
::std::thread::sleep(::std::time::Duration::from_secs(1));
let result = blockdev(&path, &["--flushbufs"]);
let result = blockdev(&path, ["--flushbufs"]);
if result.is_err() && attempt == 2 {
result?;
} else {
Expand All @@ -138,7 +138,7 @@ pub fn resize_partition<P: AsRef<Path>>(
let (npath, _mount) = if options & (BTRFS | XFS) != 0 {
let temp = TempDir::new("distinst")?;
info!("temporarily mounting {} to {}", path.as_ref().display(), temp.path().display());
let mount = Mount::new(path.as_ref(), temp.path(), fs, MountFlags::empty(), None)?;
let mount = Mount::new(path.as_ref(), temp.path())?;
let mount = mount.into_unmount_drop(UnmountFlags::DETACH);
(temp.path().to_path_buf(), Some((mount, temp)))
} else {
Expand Down Expand Up @@ -388,7 +388,7 @@ where

fn ntfs_dry_run(path: &Path, size: &str) -> io::Result<()> {
let mut consistency_check = Command::new("ntfsresize");
consistency_check.args(&["-f", "-f", "--no-action", "-s"]).arg(size).arg(path);
consistency_check.args(["-f", "-f", "--no-action", "-s"]).arg(size).arg(path);

info!("executing {:?}", consistency_check);
let mut child = consistency_check.stdin(Stdio::piped()).spawn()?;
Expand All @@ -404,7 +404,7 @@ fn ntfs_dry_run(path: &Path, size: &str) -> io::Result<()> {

fn ntfs_consistency_check(path: &Path) -> io::Result<()> {
let mut consistency_check = Command::new("ntfsresize");
consistency_check.args(&["-i", "-f"]).arg(path);
consistency_check.args(["-i", "-f"]).arg(path);

info!("executing {:?}", consistency_check);
let mut child = consistency_check.stdin(Stdio::piped()).spawn()?;
Expand Down
11 changes: 7 additions & 4 deletions crates/disk-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
[package]
name = "disk-types"
version = "0.1.5"
authors = ["Jeremy Soller <[email protected]>", "Michael Aaron Murphy <[email protected]>"]
authors = [
"Jeremy Soller <[email protected]>",
"Michael Aaron Murphy <[email protected]>",
]
description = "Common traits and types for handling block devices, partitions, file systems, etc."
keywords = ["disk", "partition"]
keywords = ["disk", "partition"]
categories = ["filesystem", "os"]
edition = "2018"

[dependencies]
sys-mount = "1.2.1"
sys-mount = "^3.0.1"
tempdir = "0.3.7"
os-detect = { path = "../os-detect" }
sysfs-class = "0.1.2"
libparted = "0.1.4"
err-derive = "0.3"
log = "0.4"
log = "0.4"
4 changes: 2 additions & 2 deletions crates/disk-types/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{io, fs, fmt::Debug};

use std::path::{Path, PathBuf};
use std::str::FromStr;

use sysfs_class::{Block, SysClass};

/// Methods that all block devices share, whether they are partitions or disks.
Expand Down
6 changes: 3 additions & 3 deletions crates/disk-types/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ impl FromStr for FileSystem {
}
}

impl Into<&'static str> for FileSystem {
fn into(self) -> &'static str {
match self {
impl From<FileSystem> for &'static str {
fn from(val: FileSystem) -> Self {
match val {
FileSystem::Btrfs => "btrfs",
FileSystem::Exfat => "exfat",
FileSystem::Ext2 => "ext2",
Expand Down
Loading