From 403e41079763c8fd8aeeee80e27262c9f1404c6b Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 3 Dec 2022 17:47:10 +0100 Subject: [PATCH] repbuild r uses udisks --- repbuild/src/main.rs | 133 +++++++++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 55 deletions(-) diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs index 77660bed..323d9cc7 100644 --- a/repbuild/src/main.rs +++ b/repbuild/src/main.rs @@ -11,7 +11,10 @@ use std::{ os::fd::AsRawFd, process::Command, }; -use udisks::{filesystem::UnMountOptions, manager::LoopSetupOptions}; +use udisks::{ + filesystem::{MountOptions, UnMountOptions}, + manager::LoopSetupOptions, +}; struct Options { pub subcommand: Subcommand, @@ -257,40 +260,52 @@ fn main() -> Result<(), Box> { // Setup loopback device for disk.img, with partitions // FIXME: don't do ths if running without changes - Command::new("sudo") - .arg("losetup") - .args(["-P", "/dev/loop0", "disk.img"]) - .status() - .unwrap(); + // Setup loop device + let disk_img = File::options().read(true).write(true).open("disk.img")?; + let dbus_conn = zbus::blocking::Connection::system()?; + let loopdev = udisks::manager::UDisks2ManagerProxyBlocking::new(&dbus_conn)? + .loop_setup( + disk_img.as_raw_fd().into(), + LoopSetupOptions { + no_user_interaction: true, + offset: 0, + size: 0, + readonly: false, + no_part_scan: false, + }, + )?; - // mount the root fs to ./disk - Command::new("sudo") - .arg("mount") - .args(["/dev/loop0p1", "./disk"]) - .status() - .unwrap(); + let parts = udisks::partition::PartitionTableProxyBlocking::builder(&dbus_conn) + .destination("org.freedesktop.UDisks2")? + .path(loopdev)? + .build()? + .partitions()?; + + let fsobjpath = parts.get(0).ok_or("missing boot partition")?; + let mountpoint = + udisks::filesystem::FilesystemProxyBlocking::builder(&dbus_conn) + .path(fsobjpath)? + .build()? + .mount(MountOptions { + no_user_interaction: true, + fs_type: String::new(), + mount_options: String::new(), + })?; // copy the kernel over to ./disk/boot/kernel - Command::new("sudo") - .arg("cp") + Command::new("cp") .arg("./target/x86_64-ableos/debug/ableos") - .arg("./disk/boot/kernel") + .arg(format!("{mountpoint}/boot/kernel")) .status() .unwrap(); - // unmount the root fs - Command::new("sudo") - .arg("umount") - .arg("./disk") - .status() - .unwrap(); - - // remove loopback device - Command::new("sudo") - .arg("losetup") - .args(["-d", "/dev/loop0"]) - .status() - .unwrap(); + udisks::filesystem::FilesystemProxyBlocking::builder(&dbus_conn) + .path(fsobjpath)? + .build()? + .unmount(UnMountOptions { + no_user_interaction: true, + force: false, + })?; // run qemu with "-S", "-gdb", "tcp:9000" Command::new("qemu-system-x86_64") @@ -324,40 +339,48 @@ fn main() -> Result<(), Box> { // Setup loopback device for disk.img, with partitions // FIXME: don't do ths if running without changes - Command::new("sudo") - .arg("losetup") - .args(["-P", "/dev/loop0", "disk.img"]) - .status() - .unwrap(); + let disk_img = File::options().read(true).write(true).open("disk.img")?; + let dbus_conn = zbus::blocking::Connection::system()?; + let loopdev = udisks::manager::UDisks2ManagerProxyBlocking::new(&dbus_conn)? + .loop_setup( + disk_img.as_raw_fd().into(), + LoopSetupOptions { + no_user_interaction: true, + offset: 0, + size: 0, + readonly: false, + no_part_scan: false, + }, + )?; - // mount the root fs to ./disk - Command::new("sudo") - .arg("mount") - .args(["/dev/loop0p1", "./disk"]) - .status() - .unwrap(); + let parts = udisks::partition::PartitionTableProxyBlocking::builder(&dbus_conn) + .destination("org.freedesktop.UDisks2")? + .path(loopdev)? + .build()? + .partitions()?; + + let fsproxy = udisks::filesystem::FilesystemProxyBlocking::builder(&dbus_conn) + .path(&parts[0])? + .build()?; + + // Obtain mountpoint + let mountpoint = loop { + if let Some(m) = fsproxy.mount_points()?.get(0) { + break m.to_string(); + } + }; // copy the kernel over to ./disk/boot/kernel - Command::new("sudo") - .arg("cp") + Command::new("cp") .arg("./target/x86_64-ableos/release/ableos") - .arg("./disk/boot/kernel") + .arg(format!("{mountpoint}/boot/kernel")) .status() .unwrap(); - // unmount the root fs - Command::new("sudo") - .arg("umount") - .arg("./disk") - .status() - .unwrap(); - - // remove loopback device - Command::new("sudo") - .arg("losetup") - .args(["-d", "/dev/loop0"]) - .status() - .unwrap(); + fsproxy.unmount(UnMountOptions { + no_user_interaction: true, + force: false, + })?; // run qemu Command::new("qemu-system-x86_64")