Skip to content

Commit 02d5596

Browse files
author
David Scott
committed
local allocator: support the same --mock-devmapper argument as xenvm
This will allow us to write test cases involving the local allocator too. Signed-off-by: David Scott <[email protected]>
1 parent 8cff047 commit 02d5596

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

xenvm-local-allocator/local_allocator.ml

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module Time = struct
1919
let sleep = Lwt_unix.sleep
2020
end
2121

22+
let dm = ref (module Devmapper.Linux : Devmapper.S.DEVMAPPER)
23+
2224
let journal_size = Int64.(mul 4L (mul 1024L 1024L))
2325

2426
let rec try_forever msg f =
@@ -242,21 +244,19 @@ module Op = struct
242244
end
243245

244246
(* Return the virtual size of the volume *)
245-
let sizeof volume =
247+
let sizeof dm_targets =
246248
let open Devmapper in
247-
let open Devmapper.Linux in
248-
let ends = List.map (fun t -> Int64.add t.Target.start t.Target.size) volume.targets in
249+
let ends = List.map (fun t -> Int64.add t.Target.start t.Target.size) dm_targets in
249250
List.fold_left max 0L ends
250251

251252
(* Compute the new segments and device mapper targets if [extents] are appended onto [lv] *)
252-
let extend_volume device vg lv extents =
253+
let extend_volume device vg existing_targets extents =
253254
let open Lvm in
254255
let to_sector pv segment = Int64.(add pv.Pv.pe_start (mul segment vg.Vg.extent_size)) in
255256
(* We will extend the LV, so find the next 'virtual segment' *)
256-
let next_sector = sizeof lv in
257+
let next_sector = sizeof existing_targets in
257258
let _, segments, targets =
258259
let open Devmapper in
259-
let open Devmapper.Linux in
260260
List.fold_left
261261
(fun (next_sector, segments, targets) (pvname, (psegment, size)) ->
262262
try
@@ -282,14 +282,15 @@ let extend_volume device vg lv extents =
282282
) (next_sector, [], []) extents in
283283
List.rev segments, List.rev targets
284284

285-
let stat x =
286-
match Devmapper.Linux.stat x with
287-
| Some x -> return (`Ok x)
285+
let targets_of x =
286+
let module D = (val !dm: Devmapper.S.DEVMAPPER) in
287+
match D.stat x with
288+
| Some x -> return (`Ok x.D.targets)
288289
| None ->
289290
error "The device mapper device %s has disappeared." x;
290291
return (`Error `Retry)
291292

292-
let main config daemon socket journal fromLVM toLVM =
293+
let main use_mock config daemon socket journal fromLVM toLVM =
293294
let config = Config.t_of_sexp (Sexplib.Sexp.load_sexp config) in
294295
let config = { config with
295296
Config.socket = (match socket with None -> config.Config.socket | Some x -> x);
@@ -298,6 +299,10 @@ let main config daemon socket journal fromLVM toLVM =
298299
fromLVM = (match fromLVM with None -> config.Config.fromLVM | Some x -> x);
299300
} in
300301
debug "Loaded configuration: %s" (Sexplib.Sexp.to_string_hum (Config.sexp_of_t config));
302+
dm := if use_mock then (module Devmapper.Mock: Devmapper.S.DEVMAPPER) else (module Devmapper.Linux: Devmapper.S.DEVMAPPER);
303+
304+
let module D = (val !dm: Devmapper.S.DEVMAPPER) in
305+
301306
if daemon then Lwt_daemon.daemonize ();
302307

303308
Pidfile.write_pid (config.Config.socket ^ ".lock");
@@ -352,18 +357,18 @@ let main config daemon socket journal fromLVM toLVM =
352357
ToLVM.push tolvm t.volume
353358
>>= fun position ->
354359
let msg = Printf.sprintf "Querying dm device %s" t.device.ExpandDevice.device in
355-
try_forever msg (fun () -> stat t.device.ExpandDevice.device)
360+
try_forever msg (fun () -> targets_of t.device.ExpandDevice.device)
356361
>>= fun x ->
357362
fatal_error msg (return x)
358-
>>= fun to_device ->
363+
>>= fun to_device_targets ->
359364
(* Append the physical blocks to toLV *)
360-
let to_targets = to_device.Devmapper.Linux.targets @ t.device.ExpandDevice.targets in
361-
Devmapper.Linux.suspend t.device.ExpandDevice.device;
365+
let to_targets = to_device_targets @ t.device.ExpandDevice.targets in
366+
D.suspend t.device.ExpandDevice.device;
362367
print_endline "Suspend local dm device";
363-
Devmapper.Linux.reload t.device.ExpandDevice.device to_targets;
368+
D.reload t.device.ExpandDevice.device to_targets;
364369
ToLVM.advance tolvm position
365370
>>= fun () ->
366-
Devmapper.Linux.resume t.device.ExpandDevice.device;
371+
D.resume t.device.ExpandDevice.device;
367372
print_endline "Resume local dm device";
368373
return ()
369374
) ops
@@ -404,15 +409,15 @@ let main config daemon socket journal fromLVM toLVM =
404409
Lwt_mutex.with_lock m
405410
(fun () ->
406411
(* We may need to enlarge in multiple chunks if the free pool is depleted *)
407-
let rec expand action = match Devmapper.Linux.stat device with
412+
let rec expand action = match D.stat device with
408413
| None ->
409414
(* Log this kind of error. This tapdisk may block but at least
410415
others will keep going *)
411416
error "Couldn't find device mapper device: %s" device;
412417
return (ResizeResponse.Device_mapper_device_does_not_exist device)
413418
| Some data_volume ->
414419
let sector_size = Int64.of_int sector_size in
415-
let current = Int64.mul sector_size (sizeof data_volume) in
420+
let current = Int64.mul sector_size (sizeof data_volume.D.targets) in
416421
let extent_b = Int64.mul sector_size extent_size in
417422
(* NB: make sure we round up to the next extent *)
418423
let nr_extents = match action with
@@ -428,7 +433,7 @@ let main config daemon socket journal fromLVM toLVM =
428433
>>= fun extents ->
429434
(* This may have allocated short *)
430435
let nr_extents' = Lvm.Pv.Allocator.size extents in
431-
let segments, targets = extend_volume vg_device metadata data_volume extents in
436+
let segments, targets = extend_volume vg_device metadata data_volume.D.targets extents in
432437
let _, volume = Mapper.vg_lv_of_name device in
433438
let volume = { ExpandVolume.volume; segments } in
434439
let device = { ExpandDevice.extents; device; targets } in
@@ -448,7 +453,7 @@ let main config daemon socket journal fromLVM toLVM =
448453
expand action
449454
) in
450455

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

454459
let rec stdin () =
@@ -533,8 +538,12 @@ let fromLVM =
533538
let doc = "Path to the device or file which contains new free blocks from LVM" in
534539
Arg.(value & opt (some file) None & info [ "fromLVM" ] ~docv:"FROMLVM" ~doc)
535540

541+
let mock_dm_arg =
542+
let doc = "Enable mock interfaces on device mapper." in
543+
Arg.(value & flag & info ["mock-devmapper"] ~doc)
544+
536545
let () =
537-
let t = Term.(pure main $ config $ daemon $ socket $ journal $ fromLVM $ toLVM) in
546+
let t = Term.(pure main $ mock_dm_arg $ config $ daemon $ socket $ journal $ fromLVM $ toLVM) in
538547
match Term.eval (t, info) with
539548
| `Error _ -> exit 1
540549
| _ -> exit 0

0 commit comments

Comments
 (0)