diff --git a/Cargo.toml b/Cargo.toml index 874d9bb..741a6d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "loopdev" description = "Setup and control loop devices" -version = "0.1.3" +version = "0.2.0" authors = ["Michael Daffin "] license = "MIT" documentation = "https://docs.rs/loopdev" diff --git a/README.md b/README.md index c0a65d8..1cd431e 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ use loopdev::LoopControl; let lc = LoopControl::open().unwrap(); let ld = lc.next_free().unwrap(); -println!("{}", ld.get_path().unwrap().display()); +println!("{}", ld.path().unwrap().display()); -ld.attach("test.img", 0).unwrap(); +ld.attach_file("test.img").unwrap(); // ... ld.detach().unwrap(); ``` diff --git a/losetup/Cargo.lock b/losetup/Cargo.lock index 4d63c30..7435716 100644 --- a/losetup/Cargo.lock +++ b/losetup/Cargo.lock @@ -1,11 +1,3 @@ -[root] -name = "losetup" -version = "0.1.3" -dependencies = [ - "clap 2.28.0 (registry+https://github.com/rust-lang/crates.io-index)", - "loopdev 0.1.3", -] - [[package]] name = "ansi_term" version = "0.10.2" @@ -29,7 +21,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "clap" -version = "2.28.0" +version = "2.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -67,12 +59,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "loopdev" -version = "0.1.3" +version = "0.2.0" dependencies = [ "errno 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "losetup" +version = "0.2.0" +dependencies = [ + "clap 2.29.0 (registry+https://github.com/rust-lang/crates.io-index)", + "loopdev 0.2.0", +] + [[package]] name = "redox_syscall" version = "0.1.32" @@ -133,7 +133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b3568b48b7cefa6b8ce125f9bb4989e52fbcc29ebea88df04cc7c5f12f70455" "checksum atty 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "21e50800ec991574876040fff8ee46b136a53e985286fbe6a3bdfe6421b78860" "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" -"checksum clap 2.28.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dc34bf7d5d66268b466b9852bca925ec1d2650654dab4da081e63fd230145c2e" +"checksum clap 2.29.0 (registry+https://github.com/rust-lang/crates.io-index)" = "110d43e343eb29f4f51c1db31beb879d546db27998577e5715270a54bcf41d3f" "checksum errno 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b2c858c42ac0b88532f48fca88b0ed947cad4f1f64d904bcd6c9f138f7b95d70" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" diff --git a/losetup/Cargo.toml b/losetup/Cargo.toml index 72faa5b..cf1a676 100644 --- a/losetup/Cargo.toml +++ b/losetup/Cargo.toml @@ -1,7 +1,8 @@ [package] authors = ["Michael Daffin "] name = "losetup" -version = "0.1.3" +version = "0.2.0" +description = "Setup and control loopback devices" [dependencies] clap = "2.23.3" diff --git a/losetup/src/main.rs b/losetup/src/main.rs index c57ab41..ce71ea0 100644 --- a/losetup/src/main.rs +++ b/losetup/src/main.rs @@ -2,87 +2,91 @@ extern crate clap; extern crate loopdev; -use std::io::Write; +use std::io::{self, Write}; use std::process::exit; use loopdev::{LoopControl, LoopDevice}; -macro_rules! exit_on_error { - ($e:expr) => ({match $e { - Ok(d) => d, - Err(err) => { - writeln!(&mut std::io::stderr(), "{}", err).unwrap(); - exit(1) - }, - } - }) +fn find() -> io::Result<()> { + let loopdev = LoopControl::open()?.next_free()?; + println!("{}", loopdev.path().unwrap().display()); + Ok(()) } -fn find() { - match LoopControl::open().and_then(|lc| lc.next_free()) { - Ok(ld) => println!("{}", ld.get_path().unwrap().display()), - Err(err) => { - writeln!(&mut std::io::stderr(), "{}", err).unwrap(); - exit(1) - } +fn attach(matches: &clap::ArgMatches) -> io::Result<()> { + let quite = matches.is_present("quite"); + let image = matches.value_of("image").unwrap(); + let offset = value_t!(matches.value_of("offset"), u64).unwrap_or(0); + let sizelimit = value_t!(matches.value_of("sizelimit"), u64).unwrap_or(0); + let loopdev = match matches.value_of("loopdev") { + Some(loopdev) => LoopDevice::open(&loopdev)?, + None => LoopControl::open().and_then(|lc| lc.next_free())?, + }; + loopdev.attach_with_sizelimit(&image, offset, sizelimit)?; + if !quite { + println!("{}", loopdev.path().unwrap().display()); } + Ok(()) } -fn attach(image: &str, loopdev: Option<&str>, offset: u64) { - exit_on_error!( - match loopdev { - None => LoopControl::open().and_then(|lc| lc.next_free()), - Some(dev) => LoopDevice::open(&dev), - }.and_then(|ld| ld.attach(&image, offset, None)) - ) +fn detach(matches: &clap::ArgMatches) -> io::Result<()> { + let loopdev = matches.value_of("file").unwrap(); + LoopDevice::open(loopdev)?.detach() } -fn detach(dev: &str) { - exit_on_error!(LoopDevice::open(dev).and_then(|ld| ld.detach())) +fn set_capacity(matches: &clap::ArgMatches) -> io::Result<()> { + let loopdev = matches.value_of("file").unwrap(); + LoopDevice::open(loopdev)?.set_capacity() } -fn list(_free: bool, _used: bool) { +fn list(matches: Option<&clap::ArgMatches>) -> io::Result<()> { + let (_free, _used) = match matches { + Some(matches) => (matches.is_present("free"), matches.is_present("used")), + None => (false, false), + }; unimplemented!(); } fn main() { let matches = clap_app!(losetup => - (version: "0.1.2") - (author: "Michael Daffin ") - (about: "Setup and control loop devices") + (version: crate_version!()) + (author: crate_authors!()) + (about: crate_description!()) (@subcommand find => (about: "find the next free loop device") - ) + ) (@subcommand attach => (about: "attach the loop device to a backing file") - (@arg image: +required "the backing file to attach") - (@arg loopdev: "the loop device to attach") + (@arg image: +required "the backing file to attach") + (@arg loopdev: "the loop device to attach") (@arg offset: -o --offset +takes_value "the offset within the file to start at") - ) + (@arg sizelimit: -s --sizelimit +takes_value "the file is limited to this size") + (@arg quite: -q --quite "don't print the device name") + ) (@subcommand detach => (about: "detach the loop device from the backing file") - (@arg file: +required "The file to detach") - ) + (@arg file: +required "The file to detach") + ) + (@subcommand setcapacity => + (about: "inform the loop driver of a change in size of the backing file") + (@arg file: +required "The file to set the capacity of") + ) (@subcommand list => (about: "list the available loop devices") (@arg free: -f --free "find free devices") (@arg used: -u --used "find used devices") - ) + ) ).get_matches(); - if let Some(_) = matches.subcommand_matches("find") { - find(); - } else if let Some(matches) = matches.subcommand_matches("attach") { - let image = matches.value_of("image").unwrap(); - let loopdev = matches.value_of("loopdev"); - attach( - image, - loopdev, - value_t!(matches.value_of("offset"), u64).unwrap_or(0), - ); - } else if let Some(matches) = matches.subcommand_matches("detach") { - let file = matches.value_of("file").unwrap(); - detach(file); - } else { - list(matches.is_present("free"), matches.is_present("used")); + let result = match matches.subcommand() { + ("find", _) => find(), + ("attach", Some(matches)) => attach(matches), + ("detach", Some(matches)) => detach(matches), + ("setcapacity", Some(matches)) => set_capacity(matches), + (_, matches) => list(matches), + }; + + if let Err(err) = result { + writeln!(&mut std::io::stderr(), "{}", err).unwrap(); + exit(1); } } diff --git a/src/lib.rs b/src/lib.rs index d7200ef..37e3523 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,9 +9,9 @@ //! let lc = LoopControl::open().unwrap(); //! let ld = lc.next_free().unwrap(); //! -//! println!("{}", ld.get_path().unwrap().display()); +//! println!("{}", ld.path().unwrap().display()); //! -//! ld.attach("test.img", 0, None).unwrap(); +//! ld.attach_file("test.img").unwrap(); //! // ... //! ld.detach().unwrap(); //! ``` @@ -27,9 +27,17 @@ use std::path::{Path, PathBuf}; use libc::{c_int, ioctl, uint8_t, uint32_t, uint64_t}; use std::default::Default; +// TODO support missing operations const LOOP_SET_FD: u16 = 0x4C00; const LOOP_CLR_FD: u16 = 0x4C01; const LOOP_SET_STATUS64: u16 = 0x4C04; +//const LOOP_GET_STATUS64: u16 = 0x4C05; +const LOOP_SET_CAPACITY: u16 = 0x4C07; +//const LOOP_SET_DIRECT_IO: u16 = 0x4C08; +//const LOOP_SET_BLOCK_SIZE: u16 = 0x4C09; + +//const LOOP_CTL_ADD: u16 = 0x4C80; +//const LOOP_CTL_REMOVE: u16 = 0x4C81; const LOOP_CTL_GET_FREE: u16 = 0x4C82; const LOOP_CONTROL: &'static str = "/dev/loop-control"; @@ -57,7 +65,7 @@ impl LoopControl { /// use loopdev::LoopControl; /// let lc = LoopControl::open().unwrap(); /// let ld = lc.next_free().unwrap(); - /// println!("{}", ld.get_path().unwrap().display()); + /// println!("{}", ld.path().unwrap().display()); /// ``` pub fn next_free(&self) -> io::Result { let result; @@ -88,6 +96,28 @@ impl LoopDevice { Ok(LoopDevice { device: f }) } + /// Attach the loop device to a file starting at offset into the file. + #[deprecated(since = "0.2.0", note = "use `attach_file` or `attach_with_offset` instead")] + pub fn attach>(&self, backing_file: P, offset: u64) -> io::Result<()> { + self.attach_with_sizelimit(backing_file, offset, 0) + } + + /// Attach the loop device to a file that maps to the whole file. + /// + /// # Examples + /// + /// Attach the device to a file. + /// + /// ```rust + /// use loopdev::LoopDevice; + /// let ld = LoopDevice::open("/dev/loop4").unwrap(); + /// ld.attach_file("test.img").unwrap(); + /// # ld.detach().unwrap(); + /// ``` + pub fn attach_file>(&self, backing_file: P) -> io::Result<()> { + self.attach_with_sizelimit(backing_file, 0, 0) + } + /// Attach the loop device to a file starting at offset into the file. /// /// # Examples @@ -96,11 +126,36 @@ impl LoopDevice { /// /// ```rust /// use loopdev::LoopDevice; - /// let ld = LoopDevice::open("/dev/loop4").unwrap(); - /// ld.attach("test.img", 0, None).unwrap(); + /// let ld = LoopDevice::open("/dev/loop5").unwrap(); + /// ld.attach_with_offset("test.img", 0).unwrap(); /// # ld.detach().unwrap(); /// ``` - pub fn attach>(&self, backing_file: P, offset: u64, size: Option) -> io::Result<()> { + pub fn attach_with_offset>( + &self, + backing_file: P, + offset: u64, + ) -> io::Result<()> { + self.attach_with_sizelimit(backing_file, offset, 0) + } + + /// Attach the loop device to a file starting at offset into the file and a the given sizelimit. + /// + /// # Examples + /// + /// Attach the device to the start of a file with a maximum size of 1024 bytes. + /// + /// ```rust + /// use loopdev::LoopDevice; + /// let ld = LoopDevice::open("/dev/loop6").unwrap(); + /// ld.attach_with_sizelimit("test.img", 0, 1024).unwrap(); + /// # ld.detach().unwrap(); + /// ``` + pub fn attach_with_sizelimit>( + &self, + backing_file: P, + offset: u64, + sizelimit: u64, + ) -> io::Result<()> { let bf = try!(OpenOptions::new().read(true).write(true).open(backing_file)); // Attach the file @@ -118,9 +173,7 @@ impl LoopDevice { // Set offset for backing_file let mut info: loop_info64 = Default::default(); info.lo_offset = offset; - if let Some(size) = size { - info.lo_sizelimit = size; - } + info.lo_sizelimit = sizelimit; unsafe { if ioctl( self.device.as_raw_fd() as c_int, @@ -136,7 +189,13 @@ impl LoopDevice { } /// Get the path of the loop device. + #[deprecated(since = "0.2.0", note = "use `path` instead")] pub fn get_path(&self) -> Option { + self.path() + } + + /// Get the path of the loop device. + pub fn path(&self) -> Option { let mut p = PathBuf::from("/proc/self/fd"); p.push(self.device.as_raw_fd().to_string()); std::fs::read_link(&p).ok() @@ -148,17 +207,37 @@ impl LoopDevice { /// /// ```rust /// use loopdev::LoopDevice; - /// let ld = LoopDevice::open("/dev/loop6").unwrap(); - /// # ld.attach("test.img", 0, None).unwrap(); + /// let ld = LoopDevice::open("/dev/loop7").unwrap(); + /// # ld.attach_file("test.img").unwrap(); /// ld.detach().unwrap(); /// ``` pub fn detach(&self) -> io::Result<()> { unsafe { if ioctl(self.device.as_raw_fd() as c_int, LOOP_CLR_FD.into(), 0) < 0 { - return Err(io::Error::last_os_error()); + Err(io::Error::last_os_error()) + } else { + Ok(()) + } + } + } + + /// Resize a live loop device. If the size of the backing file changes this can be called to + /// inform the loop driver about the new size. + pub fn set_capacity(&self) -> io::Result<()> { + println!("running set_capacity"); + unsafe { + if ioctl( + self.device.as_raw_fd() as c_int, + LOOP_SET_CAPACITY.into(), + 0, + ) < 0 + { + Err(io::Error::last_os_error()) + } else { + println!("ok"); + Ok(()) } } - Ok(()) } }