Skip to content

Commit 106b103

Browse files
author
Kaifeng Zhu
committed
CP-12323: Integrate mock camldm with xenvm
Update the code to cope with the new camldm module hierarchy. Add an argument `--mock-devmapper' to all subcommands of xenvm to use the mock devmapper interfaces. Signed-off-by: Kaifeng Zhu <[email protected]>
1 parent cc0f129 commit 106b103

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

test/test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ let file_exists filename =
103103
true
104104
with Unix.Unix_error(Unix.ENOENT, _, _) -> false
105105

106-
let dm_exists name = match Devmapper.stat name with
106+
let dm_exists name = match Devmapper.Linux.stat name with
107107
| None -> false
108108
| Some _ -> true
109109

xenvm-local-allocator/local_allocator.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ end
244244
(* Return the virtual size of the volume *)
245245
let sizeof volume =
246246
let open Devmapper in
247+
let open Devmapper.Linux in
247248
let ends = List.map (fun t -> Int64.add t.Target.start t.Target.size) volume.targets in
248249
List.fold_left max 0L ends
249250

@@ -255,6 +256,7 @@ let extend_volume device vg lv extents =
255256
let next_sector = sizeof lv in
256257
let _, segments, targets =
257258
let open Devmapper in
259+
let open Devmapper.Linux in
258260
List.fold_left
259261
(fun (next_sector, segments, targets) (pvname, (psegment, size)) ->
260262
try
@@ -281,7 +283,7 @@ let extend_volume device vg lv extents =
281283
List.rev segments, List.rev targets
282284

283285
let stat x =
284-
match Devmapper.stat x with
286+
match Devmapper.Linux.stat x with
285287
| Some x -> return (`Ok x)
286288
| None ->
287289
error "The device mapper device %s has disappeared." x;
@@ -355,13 +357,13 @@ let main config daemon socket journal fromLVM toLVM =
355357
fatal_error msg (return x)
356358
>>= fun to_device ->
357359
(* Append the physical blocks to toLV *)
358-
let to_targets = to_device.Devmapper.targets @ t.device.ExpandDevice.targets in
359-
Devmapper.suspend t.device.ExpandDevice.device;
360+
let to_targets = to_device.Devmapper.Linux.targets @ t.device.ExpandDevice.targets in
361+
Devmapper.Linux.suspend t.device.ExpandDevice.device;
360362
print_endline "Suspend local dm device";
361-
Devmapper.reload t.device.ExpandDevice.device to_targets;
363+
Devmapper.Linux.reload t.device.ExpandDevice.device to_targets;
362364
ToLVM.advance tolvm position
363365
>>= fun () ->
364-
Devmapper.resume t.device.ExpandDevice.device;
366+
Devmapper.Linux.resume t.device.ExpandDevice.device;
365367
print_endline "Resume local dm device";
366368
return ()
367369
) ops
@@ -402,7 +404,7 @@ let main config daemon socket journal fromLVM toLVM =
402404
Lwt_mutex.with_lock m
403405
(fun () ->
404406
(* We may need to enlarge in multiple chunks if the free pool is depleted *)
405-
let rec expand action = match Devmapper.stat device with
407+
let rec expand action = match Devmapper.Linux.stat device with
406408
| None ->
407409
(* Log this kind of error. This tapdisk may block but at least
408410
others will keep going *)
@@ -446,7 +448,7 @@ let main config daemon socket journal fromLVM toLVM =
446448
expand action
447449
) in
448450

449-
let ls = Devmapper.ls () in
451+
let ls = Devmapper.Linux.ls () in
450452
debug "Visible device mapper devices: [ %s ]\n%!" (String.concat "; " ls);
451453

452454
let rec stdin () =

xenvm/lvchange.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let dev_path_of vg_name lv_name =
1010
Printf.sprintf "/dev/%s/%s" vg_name lv_name
1111

1212
let activate vg lv local_device =
13+
let module Devmapper = (val !Xenvm_common.dm: Devmapper.S.DEVMAPPER) in
1314
let path = dev_path_of vg.Lvm.Vg.name lv.Lvm.Lv.name in
1415
Lwt.catch (fun () -> Lwt_unix.mkdir (Filename.dirname path) 0x755) (fun _ -> Lwt.return ()) >>= fun () ->
1516
Mapper.read [ local_device ]
@@ -41,6 +42,7 @@ let lvchange_activate copts vg_name lv_name physical_device =
4142
)
4243

4344
let deactivate vg lv =
45+
let module Devmapper = (val !Xenvm_common.dm : Devmapper.S.DEVMAPPER) in
4446
let open Xenvm_common in
4547
let name = Mapper.name_of vg lv in
4648
(* This can fail with an EBUSY *)
@@ -68,6 +70,7 @@ let deactivate vg lv =
6870
Client.flush ~name:lv.Lvm.Lv.name
6971

7072
let reload vg lv local_device =
73+
let module Devmapper = (val !Xenvm_common.dm : Devmapper.S.DEVMAPPER) in
7174
let open Xenvm_common in
7275
Mapper.read [ local_device ]
7376
>>= fun devices ->

xenvm/lvrename.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open Cmdliner
44
open Lwt
55

66
let lvrename copts (vg_name,lv_opt) newname physical_device =
7+
let module Devmapper = (val !Xenvm_common.dm : Devmapper.S.DEVMAPPER) in
78
let lv_name = match lv_opt with | Some l -> l | None -> failwith "Need an LV name" in
89
(* It seems you can say "vg/lv" or "lv" *)
910
let newname = match newname with

xenvm/lvresize.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open Cmdliner
44
open Lwt
55

66
let lvresize copts live (vg_name,lv_opt) real_size percent_size =
7+
let module Devmapper = (val !Xenvm_common.dm : Devmapper.S.DEVMAPPER) in
78
let lv_name = match lv_opt with | Some l -> l | None -> failwith "Need an LV name" in
89
let open Xenvm_common in
910
let size = match parse_size real_size percent_size with

xenvm/xenvm.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ let create config name size =
8484
Client.create ~name ~size:size_in_bytes ~tags:[])
8585

8686
let activate config lvname path pv =
87+
let module Devmapper = (val !Xenvm_common.dm : Devmapper.S.DEVMAPPER) in
8788
set_uri config None;
8889
Lwt_main.run
8990
(Client.get_lv ~name:lvname

xenvm/xenvm_common.ml

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ open Cmdliner
33
open Lwt
44
open Errors
55

6+
let dm = ref (module Devmapper.Linux : Devmapper.S.DEVMAPPER)
7+
68
let syslog = Lwt_log.syslog ~facility:`Daemon ()
79

810
let stdout fmt = Printf.ksprintf (fun s ->
@@ -40,24 +42,28 @@ type field = { key: string; name: string; fn:fieldfn }
4042
(* see https://git.fedorahosted.org/cgit/lvm2.git/tree/lib/metadata/lv.c?id=v2_02_117#n643
4143
for canonical description of this field. *)
4244

43-
(* Since we're single-shot, cache the active devices here *)
44-
let devmapper_ls =
45-
let cache = ref None in
46-
fun () ->
47-
match !cache with
48-
| None ->
49-
let ls = Devmapper.ls () in
50-
cache := Some ls;
51-
ls
52-
| Some ls ->
53-
ls
45+
let attr_of_lv vg lv =
46+
let module Devmapper = (val !dm: Devmapper.S.DEVMAPPER) in
47+
48+
(* Since we're single-shot, cache the active devices here *)
49+
let devmapper_ls =
50+
let cache = ref None in
51+
fun () ->
52+
match !cache with
53+
| None ->
54+
let ls = Devmapper.ls () in
55+
cache := Some ls;
56+
ls
57+
| Some ls ->
58+
ls
59+
in
5460

55-
let devmapper_stat name =
56-
if List.mem name (devmapper_ls ())
57-
then Devmapper.stat name
58-
else None
61+
let devmapper_stat name =
62+
if List.mem name (devmapper_ls ())
63+
then Devmapper.stat name
64+
else None
65+
in
5966

60-
let attr_of_lv vg lv =
6167
let name = Mapper.name_of vg lv in
6268
let info = devmapper_stat name in
6369
Printf.sprintf "%c%c%c%c%c%c%c%c%c%c"
@@ -208,7 +214,9 @@ type copts_t = {
208214
config : string;
209215
}
210216

211-
let make_copts config uri_override sockpath_override = {uri_override; config; sockpath_override }
217+
let make_copts config uri_override sockpath_override mock_dm =
218+
dm := if mock_dm then (module Devmapper.Mock : Devmapper.S.DEVMAPPER) else (module Devmapper.Linux : Devmapper.S.DEVMAPPER);
219+
{ uri_override; config; sockpath_override }
212220

213221
let config =
214222
let doc = "Path to the config directory" in
@@ -295,8 +303,12 @@ let output_arg default_fields =
295303
let a = Arg.(value & opt (some string) None & info ["o";"options"] ~doc) in
296304
Term.(pure (parse_output default_fields) $ a)
297305

306+
let mock_dm_arg =
307+
let doc = "Enable mock interfaces on device mapper." in
308+
Arg.(value & flag & info ["mock-devmapper"] ~doc)
309+
298310
let copts_t =
299-
Term.(pure make_copts $ config $ uri_arg $ sock_path_arg)
311+
Term.(pure make_copts $ config $ uri_arg $ sock_path_arg $ mock_dm_arg)
300312

301313
let kib = 1024L
302314
let sectors = 512L
@@ -382,8 +394,8 @@ let set_vg_info_t copts uri local_device local_allocator_path unix_domain_sock_p
382394
exit 1
383395
|e -> Lwt.fail e)
384396

385-
let run_set_vg_info_t config uri local_allocator_path local_device unix_domain_sock_path vg_name =
386-
let copts = make_copts config (Some uri) unix_domain_sock_path in
397+
let run_set_vg_info_t config uri local_allocator_path local_device unix_domain_sock_path vg_name mock_dm =
398+
let copts = make_copts config (Some uri) unix_domain_sock_path mock_dm in
387399
Lwt_main.run (set_vg_info_t copts uri local_device local_allocator_path unix_domain_sock_path vg_name)
388400

389401
let get_vg_info_t copts vg_name =
@@ -405,7 +417,7 @@ let set_vg_info_cmd =
405417
`P "This command takes a physical device path and a URI, and will write these to the
406418
filesystem. Subsequent xenvm commands will use these as defaults.";
407419
] in
408-
Term.(pure run_set_vg_info_t $ config $ uri_arg_required $ local_allocator_path $ physical_device_arg_required $ sock_path_arg $ name_arg),
420+
Term.(pure run_set_vg_info_t $ config $ uri_arg_required $ local_allocator_path $ physical_device_arg_required $ sock_path_arg $ name_arg $ mock_dm_arg),
409421
Term.info "set-vg-info" ~sdocs:copts_sect ~doc ~man
410422

411423

0 commit comments

Comments
 (0)