Skip to content

Commit 1803667

Browse files
committed
Add 'create_options' to device new() and setup() methods
Add the ability for callers to specify the DmOptions value to use when creating or setting up lineardev, thindev, thinpooldev, and cachedev. The DmOptions value is wrapped in an Option which if set to None gives the previous devicemapper-rs default behaviour. This allows clients of the crate to opt in to controlling device visibility and other DmOptions/DmUdevFlags behaviour when creating or setting up devices.
1 parent 8a0376c commit 1803667

File tree

4 files changed

+116
-45
lines changed

4 files changed

+116
-45
lines changed

src/cachedev.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,19 @@ impl CacheDev {
566566
cache: LinearDev,
567567
origin: LinearDev,
568568
cache_block_size: Sectors,
569+
create_options: Option<DmOptions>,
569570
) -> DmResult<CacheDev> {
570571
if device_exists(dm, name)? {
571572
let err_msg = format!("cachedev {name} already exists");
572573
return Err(DmError::Dm(ErrorEnum::Invalid, err_msg));
573574
}
574575

576+
let options = match create_options {
577+
Some(options) => options,
578+
None => DmOptions::private(),
579+
};
575580
let table = CacheDev::gen_default_table(&meta, &cache, &origin, cache_block_size);
576-
let dev_info = device_create(dm, name, uuid, &table, DmOptions::private())?;
581+
let dev_info = device_create(dm, name, uuid, &table, options)?;
577582

578583
Ok(CacheDev {
579584
dev_info: Box::new(dev_info),
@@ -593,6 +598,7 @@ impl CacheDev {
593598
cache: LinearDev,
594599
origin: LinearDev,
595600
cache_block_size: Sectors,
601+
create_options: Option<DmOptions>,
596602
) -> DmResult<CacheDev> {
597603
let table = CacheDev::gen_default_table(&meta, &cache, &origin, cache_block_size);
598604
let dev = if device_exists(dm, name)? {
@@ -607,7 +613,11 @@ impl CacheDev {
607613
device_match(dm, &dev, uuid)?;
608614
dev
609615
} else {
610-
let dev_info = device_create(dm, name, uuid, &table, DmOptions::private())?;
616+
let options = match create_options {
617+
Some(options) => options,
618+
None => DmOptions::private(),
619+
};
620+
let dev_info = device_create(dm, name, uuid, &table, options)?;
611621
CacheDev {
612622
dev_info: Box::new(dev_info),
613623
meta_dev: meta,
@@ -774,7 +784,7 @@ pub fn minimal_cachedev(dm: &DM, paths: &[&Path]) -> CacheDev {
774784
meta_length,
775785
LinearDevTargetParams::Linear(meta_params),
776786
)];
777-
let meta = LinearDev::setup(dm, &meta_name, None, meta_table).unwrap();
787+
let meta = LinearDev::setup(dm, &meta_name, None, meta_table, None).unwrap();
778788

779789
let cache_name = test_name("cache-cache").expect("valid format");
780790
let cache_offset = meta_length;
@@ -785,7 +795,7 @@ pub fn minimal_cachedev(dm: &DM, paths: &[&Path]) -> CacheDev {
785795
cache_length,
786796
LinearDevTargetParams::Linear(cache_params),
787797
)];
788-
let cache = LinearDev::setup(dm, &cache_name, None, cache_table).unwrap();
798+
let cache = LinearDev::setup(dm, &cache_name, None, cache_table, None).unwrap();
789799

790800
let dev2_size = blkdev_size(&OpenOptions::new().read(true).open(paths[1]).unwrap()).sectors();
791801
let dev2 = Device::from(devnode_to_devno(paths[1]).unwrap().unwrap());
@@ -797,7 +807,7 @@ pub fn minimal_cachedev(dm: &DM, paths: &[&Path]) -> CacheDev {
797807
dev2_size,
798808
LinearDevTargetParams::Linear(origin_params),
799809
)];
800-
let origin = LinearDev::setup(dm, &origin_name, None, origin_table).unwrap();
810+
let origin = LinearDev::setup(dm, &origin_name, None, origin_table, None).unwrap();
801811

802812
CacheDev::new(
803813
dm,
@@ -807,6 +817,7 @@ pub fn minimal_cachedev(dm: &DM, paths: &[&Path]) -> CacheDev {
807817
cache,
808818
origin,
809819
MIN_CACHE_BLOCK_SIZE,
820+
None,
810821
)
811822
.unwrap()
812823
}

src/lineardev.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ impl LinearDev {
528528
name: &DmName,
529529
uuid: Option<&DmUuid>,
530530
table: Vec<TargetLine<LinearDevTargetParams>>,
531+
create_options: Option<DmOptions>,
531532
) -> DmResult<LinearDev> {
532533
let table = LinearDevTargetTable::new(table);
533534
let dev = if device_exists(dm, name)? {
@@ -539,7 +540,11 @@ impl LinearDev {
539540
device_match(dm, &dev, uuid)?;
540541
dev
541542
} else {
542-
let dev_info = device_create(dm, name, uuid, &table, DmOptions::private())?;
543+
let options = match create_options {
544+
Some(options) => options,
545+
None => DmOptions::private(),
546+
};
547+
let dev_info = device_create(dm, name, uuid, &table, options)?;
543548
LinearDev {
544549
dev_info: Box::new(dev_info),
545550
table,
@@ -596,6 +601,7 @@ mod tests {
596601
&test_name("new").expect("valid format"),
597602
None,
598603
vec![],
604+
None,
599605
),
600606
Err(_)
601607
);
@@ -614,7 +620,7 @@ mod tests {
614620
Sectors(1),
615621
LinearDevTargetParams::Linear(params),
616622
)];
617-
let mut ld = LinearDev::setup(&dm, &name, None, table).unwrap();
623+
let mut ld = LinearDev::setup(&dm, &name, None, table, None).unwrap();
618624

619625
assert_matches!(ld.set_table(&dm, vec![]), Err(_));
620626
ld.resume(&dm).unwrap();
@@ -634,7 +640,7 @@ mod tests {
634640
Sectors(1),
635641
LinearDevTargetParams::Linear(params),
636642
)];
637-
let mut ld = LinearDev::setup(&dm, &name, None, table).unwrap();
643+
let mut ld = LinearDev::setup(&dm, &name, None, table, None).unwrap();
638644

639645
ld.set_name(&dm, &name).unwrap();
640646
assert_eq!(ld.name(), &*name);
@@ -655,7 +661,7 @@ mod tests {
655661
Sectors(1),
656662
LinearDevTargetParams::Linear(params),
657663
)];
658-
let mut ld = LinearDev::setup(&dm, &name, None, table).unwrap();
664+
let mut ld = LinearDev::setup(&dm, &name, None, table, None).unwrap();
659665

660666
let new_name = test_name("new_name").expect("valid format");
661667
ld.set_name(&dm, &new_name).unwrap();
@@ -689,7 +695,7 @@ mod tests {
689695
];
690696
let range: Sectors = table.iter().map(|s| s.length).sum();
691697
let count = table.len();
692-
let mut ld = LinearDev::setup(&dm, &name, None, table).unwrap();
698+
let mut ld = LinearDev::setup(&dm, &name, None, table, None).unwrap();
693699

694700
let table = LinearDev::read_kernel_table(&dm, &DevId::Name(ld.name()))
695701
.unwrap()
@@ -723,7 +729,7 @@ mod tests {
723729
)
724730
})
725731
.collect::<Vec<_>>();
726-
let mut ld = LinearDev::setup(&dm, &name, None, table.clone()).unwrap();
732+
let mut ld = LinearDev::setup(&dm, &name, None, table.clone(), None).unwrap();
727733

728734
let loaded_table = LinearDev::read_kernel_table(&dm, &DevId::Name(ld.name())).unwrap();
729735
assert!(
@@ -747,15 +753,15 @@ mod tests {
747753
Sectors(1),
748754
LinearDevTargetParams::Linear(params),
749755
)];
750-
let mut ld = LinearDev::setup(&dm, &name, None, table.clone()).unwrap();
756+
let mut ld = LinearDev::setup(&dm, &name, None, table.clone(), None).unwrap();
751757
let params2 = LinearTargetParams::new(dev, Sectors(1));
752758
let table2 = vec![TargetLine::new(
753759
Sectors(0),
754760
Sectors(1),
755761
LinearDevTargetParams::Linear(params2),
756762
)];
757-
assert_matches!(LinearDev::setup(&dm, &name, None, table2), Err(_));
758-
assert_matches!(LinearDev::setup(&dm, &name, None, table), Ok(_));
763+
assert_matches!(LinearDev::setup(&dm, &name, None, table2, None), Err(_));
764+
assert_matches!(LinearDev::setup(&dm, &name, None, table, None), Ok(_));
759765
ld.teardown(&dm).unwrap();
760766
}
761767

@@ -773,8 +779,8 @@ mod tests {
773779
Sectors(1),
774780
LinearDevTargetParams::Linear(params),
775781
)];
776-
let mut ld = LinearDev::setup(&dm, &name, None, table.clone()).unwrap();
777-
let ld2 = LinearDev::setup(&dm, &ersatz, None, table);
782+
let mut ld = LinearDev::setup(&dm, &name, None, table.clone(), None).unwrap();
783+
let ld2 = LinearDev::setup(&dm, &ersatz, None, table, None);
778784
assert_matches!(ld2, Ok(_));
779785

780786
ld2.unwrap().teardown(&dm).unwrap();
@@ -794,7 +800,7 @@ mod tests {
794800
Sectors(1),
795801
LinearDevTargetParams::Linear(params),
796802
)];
797-
let mut ld = LinearDev::setup(&dm, &name, None, table).unwrap();
803+
let mut ld = LinearDev::setup(&dm, &name, None, table, None).unwrap();
798804

799805
ld.suspend(&dm, DmOptions::default().set_flags(DmFlags::DM_NOFLUSH))
800806
.unwrap();

0 commit comments

Comments
 (0)