Skip to content

Commit 64ea4ed

Browse files
Merge pull request #141 from simonjbeaumont/cp-13469
CP-13469: upgrade/downgrade: Accept `--pvpath` argument and use `flock()`
2 parents 96b643d + a39afb5 commit 64ea4ed

File tree

8 files changed

+55
-26
lines changed

8 files changed

+55
-26
lines changed

.travis.oasis

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Library xenvmidl
2020
Path: idl
2121
Findlibname: xenvmidl
2222
Modules: Xenvm_interface, Xenvm_client, Log, Result, Errors, ResizeRequest, ResizeResponse, Pidfile
23-
BuildDepends: rpclib, rpclib.syntax, sexplib, sexplib.syntax, lvm, cohttp.lwt, threads, mirage-block-unix, devmapper, bisect
23+
BuildDepends: rpclib, rpclib.syntax, sexplib, sexplib.syntax, lvm, cohttp.lwt, threads, mirage-block-unix, devmapper, core, bisect
2424

2525
Executable "xenvmd"
2626
ByteOpt: -warn-error +a

_oasis

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Library xenvmidl
2020
Path: idl
2121
Findlibname: xenvmidl
2222
Modules: Xenvm_interface, Xenvm_client, Log, Result, Errors, ResizeRequest, ResizeResponse, Pidfile
23-
BuildDepends: rpclib, rpclib.syntax, sexplib, sexplib.syntax, lvm, cohttp.lwt, threads, mirage-block-unix, devmapper
23+
BuildDepends: rpclib, rpclib.syntax, sexplib, sexplib.syntax, lvm, cohttp.lwt, threads, mirage-block-unix, devmapper, core
2424

2525
Executable "xenvmd"
2626
ByteOpt: -warn-error +a

_tags

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# OASIS_START
2-
# DO NOT EDIT (digest: 2190fe0a8a00a6b8cc32c1b1dff4f3d1)
2+
# DO NOT EDIT (digest: 00f88924b9b0edd9fbbc611789a0ccdc)
33
# Ignore VCS directories, you can use the same kind of rule outside
44
# OASIS_START/STOP if you want to exclude directories that contains
55
# useless stuff for the build process
@@ -21,6 +21,7 @@ true: annot, bin_annot
2121
<idl/xenvmidl.{cma,cmxa}>: oasis_library_xenvmidl_native
2222
<idl/*.ml{,i,y}>: oasis_library_xenvmidl_native
2323
<idl/*.ml{,i,y}>: pkg_cohttp.lwt
24+
<idl/*.ml{,i,y}>: pkg_core
2425
<idl/*.ml{,i,y}>: pkg_devmapper
2526
<idl/*.ml{,i,y}>: pkg_lvm
2627
<idl/*.ml{,i,y}>: pkg_mirage-block-unix
@@ -36,6 +37,7 @@ true: annot, bin_annot
3637
<xenvmd/*.ml{,i,y}>: oasis_executable_xenvmd_native
3738
<xenvmd/xenvmd.{native,byte}>: pkg_cmdliner
3839
<xenvmd/xenvmd.{native,byte}>: pkg_cohttp.lwt
40+
<xenvmd/xenvmd.{native,byte}>: pkg_core
3941
<xenvmd/xenvmd.{native,byte}>: pkg_cstruct
4042
<xenvmd/xenvmd.{native,byte}>: pkg_devmapper
4143
<xenvmd/xenvmd.{native,byte}>: pkg_io-page
@@ -57,6 +59,7 @@ true: annot, bin_annot
5759
<xenvmd/xenvmd.{native,byte}>: use_xenvmidl
5860
<xenvmd/*.ml{,i,y}>: pkg_cmdliner
5961
<xenvmd/*.ml{,i,y}>: pkg_cohttp.lwt
62+
<xenvmd/*.ml{,i,y}>: pkg_core
6063
<xenvmd/*.ml{,i,y}>: pkg_cstruct
6164
<xenvmd/*.ml{,i,y}>: pkg_devmapper
6265
<xenvmd/*.ml{,i,y}>: pkg_io-page
@@ -84,6 +87,7 @@ true: annot, bin_annot
8487
<xenvm/*.ml{,i,y}>: oasis_executable_xenvm_native
8588
<xenvm/xenvm.{native,byte}>: pkg_cmdliner
8689
<xenvm/xenvm.{native,byte}>: pkg_cohttp.lwt
90+
<xenvm/xenvm.{native,byte}>: pkg_core
8791
<xenvm/xenvm.{native,byte}>: pkg_devmapper
8892
<xenvm/xenvm.{native,byte}>: pkg_io-page.unix
8993
<xenvm/xenvm.{native,byte}>: pkg_lvm
@@ -100,6 +104,7 @@ true: annot, bin_annot
100104
<xenvm/xenvm.{native,byte}>: use_xenvmidl
101105
<xenvm/*.ml{,i,y}>: pkg_cmdliner
102106
<xenvm/*.ml{,i,y}>: pkg_cohttp.lwt
107+
<xenvm/*.ml{,i,y}>: pkg_core
103108
<xenvm/*.ml{,i,y}>: pkg_devmapper
104109
<xenvm/*.ml{,i,y}>: pkg_io-page.unix
105110
<xenvm/*.ml{,i,y}>: pkg_lvm
@@ -122,6 +127,7 @@ true: annot, bin_annot
122127
<xenvm-local-allocator/*.ml{,i,y}>: oasis_executable_xenvm_local_allocator_native
123128
<xenvm-local-allocator/local_allocator.{native,byte}>: pkg_cmdliner
124129
<xenvm-local-allocator/local_allocator.{native,byte}>: pkg_cohttp.lwt
130+
<xenvm-local-allocator/local_allocator.{native,byte}>: pkg_core
125131
<xenvm-local-allocator/local_allocator.{native,byte}>: pkg_cstruct
126132
<xenvm-local-allocator/local_allocator.{native,byte}>: pkg_devmapper
127133
<xenvm-local-allocator/local_allocator.{native,byte}>: pkg_io-page
@@ -142,6 +148,7 @@ true: annot, bin_annot
142148
<xenvm-local-allocator/local_allocator.{native,byte}>: use_xenvmidl
143149
<xenvm-local-allocator/*.ml{,i,y}>: pkg_cmdliner
144150
<xenvm-local-allocator/*.ml{,i,y}>: pkg_cohttp.lwt
151+
<xenvm-local-allocator/*.ml{,i,y}>: pkg_core
145152
<xenvm-local-allocator/*.ml{,i,y}>: pkg_cstruct
146153
<xenvm-local-allocator/*.ml{,i,y}>: pkg_devmapper
147154
<xenvm-local-allocator/*.ml{,i,y}>: pkg_io-page
@@ -168,6 +175,7 @@ true: annot, bin_annot
168175
<test/*.ml{,i,y}>: oasis_executable_test_native
169176
<test/test.{native,byte}>: pkg_cmdliner
170177
<test/test.{native,byte}>: pkg_cohttp.lwt
178+
<test/test.{native,byte}>: pkg_core
171179
<test/test.{native,byte}>: pkg_cstruct
172180
<test/test.{native,byte}>: pkg_devmapper
173181
<test/test.{native,byte}>: pkg_io-page
@@ -188,6 +196,7 @@ true: annot, bin_annot
188196
<test/test.{native,byte}>: use_xenvmidl
189197
<test/*.ml{,i,y}>: pkg_cmdliner
190198
<test/*.ml{,i,y}>: pkg_cohttp.lwt
199+
<test/*.ml{,i,y}>: pkg_core
191200
<test/*.ml{,i,y}>: pkg_cstruct
192201
<test/*.ml{,i,y}>: pkg_devmapper
193202
<test/*.ml{,i,y}>: pkg_io-page

idl/META

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# OASIS_START
2-
# DO NOT EDIT (digest: c3cccf80e07baf5d9f5c7f24da44c61d)
2+
# DO NOT EDIT (digest: 363ecfeb8dd4c7e3e2f68952ef269e7c)
33
version = "0.1"
44
description = "LVM-like volume manager supporting thinly provisioned LVs"
55
requires =
6-
"rpclib rpclib.syntax sexplib sexplib.syntax lvm cohttp.lwt threads mirage-block-unix devmapper"
6+
"rpclib rpclib.syntax sexplib sexplib.syntax lvm cohttp.lwt threads mirage-block-unix devmapper core"
77
archive(byte) = "xenvmidl.cma"
88
archive(byte, plugin) = "xenvmidl.cma"
99
archive(native) = "xenvmidl.cmxa"

idl/flock.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(*
2+
* Copyright (C) 2015 Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
open Core.Std
15+
16+
exception Could_not_obtain_lock of string
17+
18+
let with_flock path f =
19+
Unix.mkdir_p ~perm:0o700 (Filename.dirname path);
20+
let fd = Unix.(openfile path ~mode:[O_WRONLY; O_CREAT] ~perm:0o644) in
21+
let rec try_lock tries =
22+
if tries > 0 then
23+
if Unix.(flock fd Flock_command.lock_exclusive) then ()
24+
else begin Unix.sleep 1; try_lock (tries - 1) end
25+
else raise (Could_not_obtain_lock path)
26+
in
27+
try_lock 5;
28+
protect ~f ~finally:(fun () -> Unix.(ignore (flock fd Flock_command.unlock)))

opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ remove: [
1313
depends: [
1414
"cstruct" {>= "0.7.1"}
1515
"lwt"
16+
"core"
1617
"ocamlfind"
1718
"shared-block-ring"
1819
"mirage-block-volume" {>= "0.9.2"}

test/test.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ let upgrade =
114114
~printer:string_of_int 2 (LVs.cardinal (Vg_IO.metadata_of vg).Vg.lvs);
115115

116116
(* Upgrade the volume to journalled *)
117-
xenvm [ "upgrade"; "vg" ] |> ignore_string;
117+
xenvm [ "upgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
118118
(* Check it's idempotent *)
119-
xenvm [ "upgrade"; "vg" ] |> ignore_string;
119+
xenvm [ "upgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
120120

121121
(* check the changing of the magic persisted *)
122122
let module Label_IO = Label.Make(Block) in
@@ -140,9 +140,9 @@ let upgrade =
140140
);
141141

142142
(* Downgrade the volume to lvm2 *)
143-
xenvm [ "downgrade"; "vg" ] |> ignore_string;
143+
xenvm [ "downgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
144144
(* Check it's idempotent *)
145-
xenvm [ "downgrade"; "vg" ] |> ignore_string;
145+
xenvm [ "downgrade"; "--pvpath"; "vg"; "vg" ] |> ignore_string;
146146

147147
(* check the changing of the magic persisted *)
148148
Label_IO.read block >>:= fun label ->

xenvm/xenvm.ml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,8 @@ let format config name filenames =
8282
(* LVM locking code can be seen here:
8383
* https://git.fedorahosted.org/cgit/lvm2.git/tree/lib/misc/lvm-flock.c#n141 *)
8484
let with_lvm_lock vg_name f =
85-
let lock_dir = "/run/lock/lvm" in
86-
let lock_path = Filename.concat lock_dir ("V_" ^ vg_name ^ ":aux") in
87-
Lwt.catch (fun () ->
88-
mkdir_rec lock_dir 0o0700;
89-
Lwt_unix.(openfile lock_path [O_CREAT; O_TRUNC; O_RDWR] 0o644)
90-
>>= fun fd ->
91-
Lwt_unix.(lockf fd F_LOCK) 0;
92-
) (function _ -> fail (Failure "Could_not_obtain_lvm_lock")) >>= fun () ->
93-
Lwt.finalize f (fun () -> Lwt_unix.unlink lock_path)
85+
let lock_path = Filename.concat "/run/lock/lvm" ("V_" ^ vg_name ^ ":aux") in
86+
Flock.with_flock lock_path f
9487

9588
(* Change the label on the PV and revert it if the function [f] fails *)
9689
let with_label_change block magic f =
@@ -103,12 +96,11 @@ let with_label_change block magic f =
10396
Lwt.catch f (fun exn -> Label_IO.write block orig_label >>:= fun () -> fail exn)
10497

10598

106-
let upgrade config filename =
99+
let upgrade config physical_device (vg_name, _) =
107100
let module Vg_IO = Vg.Make(Log)(Block)(Time)(Clock) in
108101
let t =
109-
let (vg_name, _) = parse_name filename in
110102
with_lvm_lock vg_name (fun () ->
111-
with_block filename (fun block ->
103+
with_block physical_device (fun block ->
112104
(* Change the label magic to be Journalled to hide from LVM *)
113105
with_label_change block `Journalled (fun () ->
114106
(* Create the redo log, first reconnect to pick up the label changes *)
@@ -129,12 +121,11 @@ let upgrade config filename =
129121
) in
130122
Lwt_main.run t
131123

132-
let downgrade config filename =
124+
let downgrade config physical_device (vg_name, _) =
133125
let module Vg_IO = Vg.Make(Log)(Block)(Time)(Clock) in
134126
let t =
135-
let (vg_name, _) = parse_name filename in
136127
with_lvm_lock vg_name (fun () ->
137-
with_block filename (fun block ->
128+
with_block physical_device (fun block ->
138129
(* Change the label magic to be LVM to hide from XenVM *)
139130
with_label_change block `Lvm (fun () ->
140131
(* Destroy the redo log, first reconnect to pick up the label changes *)
@@ -375,7 +366,7 @@ let upgrade_cmd =
375366
`S "DESCRIPTION";
376367
`P "Upgrades the volume group metadata so that it will be usable by xenvmd and hidden from lvm."
377368
] in
378-
Term.(pure upgrade $ copts_t $ filename),
369+
Term.(pure upgrade $ copts_t $ physical_device_arg_required $ name_arg),
379370
Term.info "upgrade" ~sdocs:copts_sect ~doc ~man
380371

381372
let downgrade_cmd =
@@ -384,7 +375,7 @@ let downgrade_cmd =
384375
`S "DESCRIPTION";
385376
`P "Downgrades the volume group metadata so that it will be usable by lvm and hidden from xenvm."
386377
] in
387-
Term.(pure downgrade $ copts_t $ filename),
378+
Term.(pure downgrade $ copts_t $ physical_device_arg_required $ name_arg),
388379
Term.info "downgrade" ~sdocs:copts_sect ~doc ~man
389380

390381
let host_connect_cmd =

0 commit comments

Comments
 (0)