Skip to content

Commit ab20161

Browse files
committed
feat(rootfs, config): add support for old builders
1 parent f6eea8e commit ab20161

File tree

12 files changed

+918
-32
lines changed

12 files changed

+918
-32
lines changed

dadk-config/src/manifest.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ fn check_used_default() -> bool {
5858
pub struct Metadata {
5959
/// Target processor architecture
6060
pub arch: TargetArch,
61+
62+
/// DADK builder version for isolating unstable features
63+
#[serde(default = "default_dadk_builder_version", rename = "builder-version")]
64+
pub builder_version: String,
65+
6166
/// Rootfs configuration file path
6267
#[serde(default = "default_rootfs_config_path", rename = "rootfs-config")]
6368
pub rootfs_config: PathBuf,
@@ -88,6 +93,10 @@ pub struct Metadata {
8893
pub user_config_dir: PathBuf,
8994
}
9095

96+
fn default_dadk_builder_version() -> String {
97+
"v1".to_string()
98+
}
99+
91100
/// Returns the default path for the rootfs configuration file.
92101
fn default_rootfs_config_path() -> PathBuf {
93102
set_used_default();

dadk/src/actions/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ pub fn run(ctx: DADKExecContext) {
1010
unimplemented!("kernel command has not implemented for run yet.")
1111
}
1212
crate::console::Action::Rootfs(rootfs_command) => {
13-
rootfs::run(&ctx, rootfs_command).expect("Run rootfs action error.")
13+
match ctx.manifest().metadata.builder_version.as_str() {
14+
"v2" => {
15+
// v2版本的rootfs命令
16+
rootfs::v2::run(&ctx, rootfs_command).expect("Run rootfs action error.")
17+
}
18+
"v1" | _ => {
19+
// v1版本的rootfs命令
20+
rootfs::v1::run(&ctx, rootfs_command).expect("Run rootfs action error.")
21+
}
22+
}
1423
}
1524
crate::console::Action::User(user_command) => {
1625
user::run(&ctx, user_command).expect("Run user action error.")

dadk/src/actions/rootfs/mod.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,2 @@
1-
use crate::{console::rootfs::RootFSCommand, context::DADKExecContext};
2-
use anyhow::Result;
3-
4-
mod disk_img;
5-
mod loopdev;
6-
mod sysroot;
7-
8-
pub(super) fn run(ctx: &DADKExecContext, rootfs_cmd: &RootFSCommand) -> Result<()> {
9-
match rootfs_cmd {
10-
RootFSCommand::Create(param) => disk_img::create(ctx, param.skip_if_exists),
11-
RootFSCommand::Delete => disk_img::delete(ctx, false),
12-
RootFSCommand::DeleteSysroot => sysroot::delete(ctx),
13-
RootFSCommand::Mount => disk_img::mount(ctx),
14-
RootFSCommand::Umount => disk_img::umount(ctx),
15-
RootFSCommand::CheckDiskImageExists => disk_img::check_disk_image_exists(ctx),
16-
RootFSCommand::ShowMountPoint => disk_img::show_mount_point(ctx),
17-
RootFSCommand::ShowLoopDevice => disk_img::show_loop_device(ctx),
18-
}
19-
}
1+
pub mod v1;
2+
pub mod v2;

dadk/src/actions/rootfs/disk_img.rs renamed to dadk/src/actions/rootfs/v1/disk_img.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,10 @@ pub fn show_mount_point(ctx: &DADKExecContext) -> Result<()> {
261261

262262
pub fn show_loop_device(ctx: &DADKExecContext) -> Result<()> {
263263
let disk_image_path = ctx.disk_image_path();
264-
let mut loop_device = LoopDeviceBuilder::new().detach_on_drop(false).img_path(disk_image_path).build()?;
264+
let mut loop_device = LoopDeviceBuilder::new()
265+
.detach_on_drop(false)
266+
.img_path(disk_image_path)
267+
.build()?;
265268
if let Err(e) = loop_device.attach_by_exists() {
266269
log::error!("Failed to attach loop device: {}", e);
267270
} else {

dadk/src/actions/rootfs/loopdev.rs renamed to dadk/src/actions/rootfs/v1/loopdev.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ impl LoopDevice {
115115
if new_path.exists() {
116116
return Ok(new_path);
117117
}
118-
log::error!("Both {} and {} not exist!", direct_path.display(), new_path.display());
118+
log::error!(
119+
"Both {} and {} not exist!",
120+
direct_path.display(),
121+
new_path.display()
122+
);
119123
return Err(anyhow!("Unable to find partition path {}", nth));
120124
}
121125
Ok(direct_path)
@@ -139,13 +143,14 @@ impl LoopDevice {
139143
self.mapper = false;
140144
}
141145

142-
let output = Command::new("losetup")
143-
.arg("-d")
144-
.arg(&loop_device)
145-
.output();
146+
let output = Command::new("losetup").arg("-d").arg(&loop_device).output();
146147

147148
if !output.is_ok() {
148-
log::error!("losetup failed to detach loop device [{}]: {}", &loop_device, output.unwrap_err());
149+
log::error!(
150+
"losetup failed to detach loop device [{}]: {}",
151+
&loop_device,
152+
output.unwrap_err()
153+
);
149154
return;
150155
}
151156

@@ -159,7 +164,6 @@ impl LoopDevice {
159164
str::from_utf8(output.stderr.as_slice()).unwrap_or("<Unknown>")
160165
);
161166
}
162-
163167
}
164168

165169
#[allow(dead_code)]
@@ -182,9 +186,9 @@ impl Drop for LoopDevice {
182186
}
183187

184188
mod mapper {
185-
use std::process::Command;
186-
use anyhow::Result;
187189
use anyhow::anyhow;
190+
use anyhow::Result;
191+
use std::process::Command;
188192

189193
pub(super) fn create_mapper(dev_path: &str) -> Result<()> {
190194
let output = Command::new("kpartx")
@@ -218,7 +222,11 @@ mod mapper {
218222
);
219223
}
220224
} else {
221-
log::error!("Failed to detach mapper device [{}]: {}", dev_path, output.unwrap_err());
225+
log::error!(
226+
"Failed to detach mapper device [{}]: {}",
227+
dev_path,
228+
output.unwrap_err()
229+
);
222230
}
223231
}
224232
}
@@ -254,7 +262,7 @@ impl LoopDeviceBuilder {
254262
img_path: self.img_path,
255263
loop_device_path: self.loop_device_path,
256264
detach_on_drop: self.detach_on_drop,
257-
mapper: false
265+
mapper: false,
258266
};
259267

260268
Ok(loop_dev)

dadk/src/actions/rootfs/v1/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::{console::rootfs::RootFSCommand, context::DADKExecContext};
2+
use anyhow::Result;
3+
4+
mod disk_img;
5+
mod loopdev;
6+
mod sysroot;
7+
8+
pub fn run(ctx: &DADKExecContext, rootfs_cmd: &RootFSCommand) -> Result<()> {
9+
match rootfs_cmd {
10+
RootFSCommand::Create(param) => disk_img::create(ctx, param.skip_if_exists),
11+
RootFSCommand::Delete => disk_img::delete(ctx, false),
12+
RootFSCommand::DeleteSysroot => sysroot::delete(ctx),
13+
RootFSCommand::Mount => disk_img::mount(ctx),
14+
RootFSCommand::Umount => disk_img::umount(ctx),
15+
RootFSCommand::CheckDiskImageExists => disk_img::check_disk_image_exists(ctx),
16+
RootFSCommand::ShowMountPoint => disk_img::show_mount_point(ctx),
17+
RootFSCommand::ShowLoopDevice => disk_img::show_loop_device(ctx),
18+
}
19+
}
File renamed without changes.

0 commit comments

Comments
 (0)