Skip to content

Commit

Permalink
blockdev: Use -b for lsblk, hard require size property
Browse files Browse the repository at this point in the history
I noticed that the JSON format inconsistently uses human-readable
sizes for some properties, but not others. `size` happens to
be one of the ones were it outputs human readable. But I think
we will often want to operate on the raw byte value on our
own, so let's tell lsblk to just give us raw numbers and
do formatting where needed.

While we're here, just hard require the `size` property.
I don't think any block devices can be missing this.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Jul 13, 2024
1 parent 61feba4 commit b9dcd45
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) struct Device {
pub(crate) model: Option<String>,
pub(crate) partlabel: Option<String>,
pub(crate) children: Option<Vec<Device>>,
pub(crate) size: Option<String>,
pub(crate) size: u64,
#[serde(rename = "maj:min")]
pub(crate) maj_min: Option<String>,
// NOTE this one is not available on older util-linux
Expand Down Expand Up @@ -91,7 +91,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"])
.args(["-J", "-b", "-O"])
.args(dev)
.output()?;
if !o.status.success() {
Expand Down
8 changes: 2 additions & 6 deletions lib/src/install/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn mkfs<'a>(
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 size = ostree_ext::glib::format_size(devinfo.size);
let u = uuid::Uuid::new_v4();
let mut t = Task::new(
&format!("Creating {label} filesystem ({fs}) on device {dev} (size={size})"),
Expand Down Expand Up @@ -210,12 +210,8 @@ pub(crate) fn install_create_rootfs(
};
let serial = device.serial.as_deref().unwrap_or("<unknown>");
let model = device.model.as_deref().unwrap_or("<unknown>");
let size = device
.size
.as_deref()
.ok_or_else(|| anyhow::anyhow!("Missing size for blockdev"))?;
println!("Block setup: {block_setup}");
println!(" Size: {size}",);
println!(" Size: {}", device.size);
println!(" Serial: {serial}");
println!(" Model: {model}");

Expand Down

0 comments on commit b9dcd45

Please sign in to comment.