Skip to content

Commit e4f227c

Browse files
committed
nydus-image: Adapt local cas to the latest version of nydus.
Original version from dragonflyoss#956. Signed-off-by: xwb1136021767 <1136021767@qq.com> Signed-off-by: mofishzz <jnhuang95@gmail.com>
1 parent 8b59e19 commit e4f227c

File tree

33 files changed

+1678
-76
lines changed

33 files changed

+1678
-76
lines changed

Cargo.lock

Lines changed: 172 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ serde = { version = "1.0.110", features = ["serde_derive", "rc"] }
5151
serde_json = "1.0.51"
5252
tar = "0.4.38"
5353
tokio = { version = "1.24", features = ["macros"] }
54+
toml = "0.5"
5455

5556
# Build static linked openssl library
5657
openssl = { version = "0.10.55", features = ["vendored"] }

api/src/config.rs

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct ConfigV2 {
2929
pub cache: Option<CacheConfigV2>,
3030
/// Configuration information for RAFS filesystem.
3131
pub rafs: Option<RafsConfigV2>,
32+
/// Configuration information for image deduplication.
33+
pub dedup: Option<DeduplicationConfigV2>,
3234
/// Internal runtime configuration.
3335
#[serde(skip)]
3436
pub internal: ConfigV2Internal,
@@ -42,6 +44,7 @@ impl Default for ConfigV2 {
4244
backend: None,
4345
cache: None,
4446
rafs: None,
47+
dedup: None,
4548
internal: ConfigV2Internal::default(),
4649
}
4750
}
@@ -56,6 +59,7 @@ impl ConfigV2 {
5659
backend: None,
5760
cache: None,
5861
rafs: None,
62+
dedup: None,
5963
internal: ConfigV2Internal::default(),
6064
}
6165
}
@@ -126,6 +130,16 @@ impl ConfigV2 {
126130
})
127131
}
128132

133+
/// Get configuration information for image deduplication.
134+
pub fn get_dedup_config(&self) -> Result<&DeduplicationConfigV2> {
135+
self.dedup.as_ref().ok_or_else(|| {
136+
Error::new(
137+
ErrorKind::InvalidInput,
138+
"no configuration information for deduplication",
139+
)
140+
})
141+
}
142+
129143
/// Get configuration information for cache subsystem.
130144
pub fn get_cache_config(&self) -> Result<&CacheConfigV2> {
131145
self.cache.as_ref().ok_or_else(|| {
@@ -962,6 +976,9 @@ pub struct BlobCacheEntryConfigV2 {
962976
/// Configuration information for local cache system.
963977
#[serde(default)]
964978
pub cache: CacheConfigV2,
979+
/// Configuration information for chunk deduplication.
980+
#[serde(default)]
981+
pub dedup: Option<DeduplicationConfigV2>,
965982
/// Optional file path for metadata blob.
966983
#[serde(default)]
967984
pub metadata_path: Option<String>,
@@ -1024,11 +1041,59 @@ impl From<&BlobCacheEntryConfigV2> for ConfigV2 {
10241041
backend: Some(c.backend.clone()),
10251042
cache: Some(c.cache.clone()),
10261043
rafs: None,
1044+
dedup: c.dedup.clone(),
10271045
internal: ConfigV2Internal::default(),
10281046
}
10291047
}
10301048
}
10311049

1050+
/// Configuration information for image deduplication.
1051+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
1052+
pub struct DeduplicationConfigV2 {
1053+
#[serde(default)]
1054+
pub enable: bool,
1055+
#[serde(default)]
1056+
pub work_dir: String,
1057+
}
1058+
1059+
impl DeduplicationConfigV2 {
1060+
/// Validate image deduplication configuration.
1061+
pub fn validate(&self) -> bool {
1062+
true
1063+
}
1064+
1065+
pub fn get_enable(&self) -> bool {
1066+
self.enable
1067+
}
1068+
pub fn get_work_dir(&self) -> Result<&str> {
1069+
let path = fs::metadata(&self.work_dir)
1070+
.or_else(|_| {
1071+
fs::create_dir_all(&self.work_dir)?;
1072+
fs::metadata(&self.work_dir)
1073+
})
1074+
.map_err(|e| {
1075+
log::error!(
1076+
"fail to stat deduplication work_dir {}: {}",
1077+
self.work_dir,
1078+
e
1079+
);
1080+
e
1081+
})?;
1082+
1083+
if path.is_dir() {
1084+
Ok(&self.work_dir)
1085+
} else {
1086+
Err(Error::new(
1087+
ErrorKind::NotFound,
1088+
format!(
1089+
"deduplication work_dir {} is not a directory",
1090+
self.work_dir
1091+
),
1092+
))
1093+
}
1094+
}
1095+
}
1096+
10321097
/// Internal runtime configuration.
10331098
#[derive(Clone, Debug)]
10341099
pub struct ConfigV2Internal {
@@ -1070,7 +1135,7 @@ pub const BLOB_CACHE_TYPE_META_BLOB: &str = "bootstrap";
10701135
pub const BLOB_CACHE_TYPE_DATA_BLOB: &str = "datablob";
10711136

10721137
/// Configuration information for a cached blob.
1073-
#[derive(Debug, Deserialize, Serialize)]
1138+
#[derive(Debug, Deserialize, Serialize, Clone)]
10741139
pub struct BlobCacheEntry {
10751140
/// Type of blob object, bootstrap or data blob.
10761141
#[serde(rename = "type")]
@@ -1325,6 +1390,28 @@ impl TryFrom<&CacheConfig> for CacheConfigV2 {
13251390
}
13261391
}
13271392

1393+
/// Configuration information for image deduplication.
1394+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
1395+
struct DeduplicationConfig {
1396+
/// Whether to enable image dedup
1397+
#[serde(default)]
1398+
pub enable: bool,
1399+
/// Work fir for image dedup
1400+
#[serde(default)]
1401+
pub work_dir: String,
1402+
}
1403+
1404+
impl TryFrom<&DeduplicationConfig> for DeduplicationConfigV2 {
1405+
type Error = std::io::Error;
1406+
1407+
fn try_from(v: &DeduplicationConfig) -> std::result::Result<Self, Self::Error> {
1408+
Ok(DeduplicationConfigV2 {
1409+
enable: v.enable,
1410+
work_dir: v.work_dir.clone(),
1411+
})
1412+
}
1413+
}
1414+
13281415
/// Configuration information to create blob cache manager.
13291416
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
13301417
struct FactoryConfig {
@@ -1336,6 +1423,9 @@ struct FactoryConfig {
13361423
/// Configuration for blob cache manager.
13371424
#[serde(default)]
13381425
pub cache: CacheConfig,
1426+
/// Configuration information for image deduplication.
1427+
#[serde(default)]
1428+
pub dedup: Option<DeduplicationConfig>,
13391429
}
13401430

13411431
/// Rafs storage backend configuration information.
@@ -1374,6 +1464,14 @@ impl TryFrom<RafsConfig> for ConfigV2 {
13741464
fn try_from(v: RafsConfig) -> std::result::Result<Self, Self::Error> {
13751465
let backend: BackendConfigV2 = (&v.device.backend).try_into()?;
13761466
let mut cache: CacheConfigV2 = (&v.device.cache).try_into()?;
1467+
let dedup: Option<DeduplicationConfigV2> = match &v.device.dedup {
1468+
Some(dedup) => {
1469+
let dedup_v2: DeduplicationConfigV2 = dedup.try_into()?;
1470+
Some(dedup_v2)
1471+
}
1472+
None => None,
1473+
};
1474+
// (&v.device.dedup).try_into()?;
13771475
let rafs = RafsConfigV2 {
13781476
mode: v.mode,
13791477
batch_size: v.amplify_io,
@@ -1394,6 +1492,7 @@ impl TryFrom<RafsConfig> for ConfigV2 {
13941492
backend: Some(backend),
13951493
cache: Some(cache),
13961494
rafs: Some(rafs),
1495+
dedup,
13971496
internal: ConfigV2Internal::default(),
13981497
})
13991498
}
@@ -1487,6 +1586,8 @@ pub(crate) struct BlobCacheEntryConfig {
14871586
///
14881587
/// Possible value: `FileCacheConfig`, `FsCacheConfig`.
14891588
cache_config: Value,
1589+
/// Configuration for chunk deduplication
1590+
dedup_config: Option<DeduplicationConfig>,
14901591
/// Configuration for data prefetch.
14911592
#[serde(default)]
14921593
prefetch_config: BlobPrefetchConfig,
@@ -1510,11 +1611,19 @@ impl TryFrom<&BlobCacheEntryConfig> for BlobCacheEntryConfigV2 {
15101611
cache_validate: false,
15111612
prefetch_config: v.prefetch_config.clone(),
15121613
};
1614+
let dedup_config = match &v.dedup_config {
1615+
Some(cfg) => {
1616+
let cfg_v2: DeduplicationConfigV2 = cfg.try_into()?;
1617+
Some(cfg_v2)
1618+
}
1619+
None => None,
1620+
};
15131621
Ok(BlobCacheEntryConfigV2 {
15141622
version: 2,
15151623
id: v.id.clone(),
15161624
backend: (&backend_config).try_into()?,
15171625
cache: (&cache_config).try_into()?,
1626+
dedup: dedup_config,
15181627
metadata_path: v.metadata_path.clone(),
15191628
})
15201629
}

builder/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ indexmap = "1"
1616
libc = "0.2"
1717
log = "0.4"
1818
nix = "0.24"
19-
serde = { version = "1.0.110", features = ["serde_derive", "rc"] }
19+
serde = { version = "1.0.137", features = ["derive", "rc"] }
2020
serde_json = "1.0.53"
21+
erased-serde = "0.3"
2122
sha2 = "0.10.2"
2223
tar = "0.4.38"
2324
vmm-sys-util = "0.11.0"
2425
xattr = "0.2.3"
26+
toml = "0.5"
27+
bitvec = { version="1", default-features = false, features = ["alloc",
28+
"atomic",
29+
"serde",
30+
"std",]}
2531

2632
nydus-api = { version = "0.3", path = "../api" }
2733
nydus-rafs = { version = "0.3", path = "../rafs" }

0 commit comments

Comments
 (0)