Procházet zdrojové kódy

add kpartx to map loop device partitions for editing.

Samuka007 před 1 měsícem
rodič
revize
a3edc46275

+ 2 - 2
dadk/src/actions/rootfs/disk_img.rs

@@ -94,7 +94,7 @@ fn mount_partitioned_image(
 
     loop_device
         .attach()
-        .map_err(|e| anyhow!("Failed to attach loop device: {}", e))?;
+        .map_err(|e| anyhow!("mount: Failed to attach loop device: {}", e))?;
 
     let dev_path = loop_device.partition_path(1)?;
     mount_unpartitioned_image(ctx, &dev_path, disk_mount_path)?;
@@ -202,7 +202,7 @@ fn create_partitioned_image(ctx: &DADKExecContext, disk_image_path: &PathBuf) ->
         .map_err(|e| anyhow!("Failed to create loop device: {}", e))?;
     loop_device
         .attach()
-        .map_err(|e| anyhow!("Failed to attach loop device: {}", e))?;
+        .map_err(|e| anyhow!("creat: Failed to attach loop device: {}", e))?;
 
     let partition_path = loop_device.partition_path(1)?;
     let fs_type = ctx.rootfs().metadata.fs_type;

+ 18 - 3
dadk/src/actions/rootfs/loopdev.rs

@@ -100,12 +100,23 @@ impl LoopDevice {
             return Err(anyhow!("Loop device not attached"));
         }
         let s = format!("{}p{}", self.loop_device_path.as_ref().unwrap(), nth);
-        let s = PathBuf::from(s);
+        let direct_path = PathBuf::from(s);
         // 判断路径是否存在
-        if !s.exists() {
+        if !direct_path.exists() {
+            Command::new("kpartx")
+                .arg("-a")
+                .arg(self.loop_device_path.as_ref().unwrap())
+                .output()?;
+            let device_name = direct_path.file_name().unwrap();
+            let parent_path = direct_path.parent().unwrap();
+            let new_path = parent_path.join("mapper").join(device_name);
+            if new_path.exists() {
+                return Ok(new_path);
+            }
+            log::error!("Both {} and {} not exist!", direct_path.display(), new_path.display());
             return Err(anyhow!("Partition not exist"));
         }
-        Ok(s)
+        Ok(direct_path)
     }
 
     pub fn detach(&mut self) -> Result<()> {
@@ -119,6 +130,10 @@ impl LoopDevice {
             p.display(),
             p.exists()
         );
+        let kpart_detach = Command::new("kpartx")
+            .arg("-dv")
+            .arg(&loop_device)
+            .output()?;
         let output = Command::new("losetup")
             .arg("-d")
             .arg(loop_device)