Skip to content

Commit 87b3885

Browse files
committed
Merge pull request #117 from xapi-project/safety-label
Refuse to open a device if it has the wrong label
2 parents cd7ae54 + cd0704b commit 87b3885

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

test/test.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,29 @@ let vgs_offline =
4747
)
4848
)
4949

50+
let pvremove =
51+
"pvremove <device>: check that we can make a PV unrecognisable" >::
52+
(fun () ->
53+
with_temp_file (fun filename ->
54+
xenvm [ "vgcreate"; vg; filename ] |> ignore_string;
55+
mkdir_rec "/tmp/xenvm.d" 0o0755;
56+
xenvm [ "set-vg-info"; "--pvpath"; filename; "-S"; "/tmp/xenvmd"; vg; "--local-allocator-path"; "/tmp/xenvm-local-allocator"; "--uri"; "file://local/services/xenvmd/"^vg ] |> ignore_string;
57+
xenvm [ "vgs"; vg ] |> ignore_string;
58+
xenvm [ "pvremove"; filename ] |> ignore_string;
59+
begin
60+
try
61+
xenvm [ "vgs"; vg ] |> ignore_string;
62+
failwith "pvremove failed to hide a VG from vgs"
63+
with Bad_exit(1, _, _, _, _) ->
64+
()
65+
end
66+
)
67+
)
68+
5069
let no_xenvmd_suite = "Commands which should work without xenvmd" >::: [
5170
vgcreate;
5271
vgs_offline;
72+
pvremove;
5373
]
5474

5575
let assert_lv_exists ?expected_size_in_extents name =

xenvm/pvremove.ml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ open Lwt
77
open Lvm
88
module Pv_IO = Pv.Make(Block)
99

10-
let (>>*=) m f = match m with
11-
| `Error (`Msg e) -> fail (Failure e)
12-
| `Ok x -> f x
13-
14-
let (>>|=) m f = m >>= fun x -> x >>*= f
15-
16-
let with_block filename f =
17-
let open Lwt in
18-
Block.connect filename
19-
>>= function
20-
| `Error _ -> fail (Failure (Printf.sprintf "Unable to read %s" filename))
21-
| `Ok x ->
22-
Lwt.catch (fun () -> f x) (fun e -> Block.disconnect x >>= fun () -> fail e)
23-
2410
let pvremove copts undo filenames =
2511
let open Xenvm_common in
2612
Lwt_main.run (

xenvmd/xenvmd.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ module VolumeManager = struct
146146
| `Ok x -> return x
147147
) devices'
148148
>>= fun devices' ->
149+
let module Label_IO = Lvm.Label.Make(Block) in
150+
Lwt_list.iter_s
151+
(fun device ->
152+
Label_IO.read device >>= function
153+
| `Error (`Msg m) ->
154+
error "Failed to read PV label from device: %s" m;
155+
fail (Failure "Failed to read PV label from device")
156+
| `Ok label ->
157+
begin match Lvm.Label.Label_header.magic_of label.Lvm.Label.label_header with
158+
| Some `Lvm ->
159+
error "Device has normal LVM PV label. I will only open devices with the new PV label.";
160+
fail (Failure "Device has wrong LVM PV label")
161+
| Some `Journalled ->
162+
return ()
163+
| _ ->
164+
error "Device has an unrecognised LVM PV label. I will only open devices with the new PV label.";
165+
fail (Failure "Device has wrong PV label")
166+
end
167+
) devices'
168+
>>= fun () ->
149169
Vg_IO.connect ~flush_interval:5. devices' `RW >>|= fun vg ->
150170
Lwt.wakeup_later myvg_u vg;
151171
return ()

0 commit comments

Comments
 (0)