Skip to content

Commit ec83343

Browse files
committed
Merge pull request #91 from xapi-project/more-tunnelling
xenvm: log command-lines and output to syslog
2 parents 1f1c9aa + 37a64be commit ec83343

File tree

9 files changed

+74
-65
lines changed

9 files changed

+74
-65
lines changed

xenvm/lvchange.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ let deactivate vg lv =
4949
let result =
5050
try
5151
if List.mem name all then Devmapper.remove name;
52-
`Ok ()
52+
return (`Ok ())
5353
with e ->
54-
Printf.fprintf stderr "Caught %s while removing dm device %s\n%!" (Printexc.to_string e) name;
54+
stderr "Caught %s while removing dm device %s" (Printexc.to_string e) name
55+
>>= fun () ->
5556
if n = 0 then raise e;
56-
`Retry in
57-
match result with
58-
| `Ok () -> ()
57+
return `Retry in
58+
result >>= function
59+
| `Ok () -> return ()
5960
| `Retry ->
6061
Unix.sleep 1;
6162
retry (n - 1) in
62-
retry 30;
63+
retry 30
64+
>>= fun () ->
6365
(* Delete the device node *)
6466
let path = dev_path_of vg.Lvm.Vg.name lv.Lvm.Lv.name in
6567
Lwt.catch (fun () -> Lwt_unix.unlink path) (fun _ -> Lwt.return ()) >>= fun () ->

xenvm/lvdisplay.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let print_verbose vg lv =
3434
*)
3535
"";
3636
] in
37-
List.iter (fun line -> Printf.printf " %s\n" line) lines
37+
Lwt_list.iter_s (fun line -> stdout " %s" line) lines
3838

3939
let print_colon vg lv =
4040
let sectors = Int64.mul vg.Lvm.Vg.extent_size (Lvm.Lv.size_in_extents lv) in
@@ -53,7 +53,7 @@ let print_colon vg lv =
5353
"?"; (* major *)
5454
"?"; (* minor *)
5555
] in
56-
Printf.printf " %s\n" (String.concat ":" parts)
56+
stdout " %s" (String.concat ":" parts)
5757

5858
let lvdisplay copts colon (vg_name,lv_display_opt) =
5959
let open Xenvm_common in
@@ -63,17 +63,17 @@ let lvdisplay copts colon (vg_name,lv_display_opt) =
6363
set_uri copts info;
6464
Client.get () >>= fun vg ->
6565
let print = if colon then print_colon else print_verbose in
66-
let success = ref false in
67-
Lvm.Vg.LVs.iter (fun _ lv -> match lv_display_opt with
68-
| None ->
69-
print vg lv;
70-
success := true
71-
| Some lv' when lv.Lvm.Lv.name = lv' ->
72-
print vg lv;
73-
success := true
74-
| Some _ -> ()
75-
) vg.Lvm.Vg.lvs;
76-
if not !success then failwith "Failed to find any matching logical volumes";
66+
let to_print =
67+
Lvm.Vg.LVs.bindings vg.Lvm.Vg.lvs
68+
|> List.map snd (* only interested in the LV, not the id *)
69+
|> List.filter (function { Lvm.Lv.name } -> match lv_display_opt with
70+
| None -> true
71+
| Some lv' when lv' = name -> true
72+
| _ -> false
73+
) in
74+
Lwt_list.iter_s (print vg) to_print
75+
>>= fun () ->
76+
if to_print = [] then failwith "Failed to find any matching logical volumes";
7777
Lwt.return () in
7878
Lwt_main.run t
7979

xenvm/lvresize.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ let lvresize copts live (vg_name,lv_opt) real_size percent_size =
6262
>>= fun () ->
6363
match resp with
6464
| ResizeResponse.Device_mapper_device_does_not_exist name ->
65-
Printf.fprintf stderr "Device mapper device does not exist: %s\n%!" name;
65+
stderr "Device mapper device does not exist: %s" name
66+
>>= fun () ->
6667
exit 1
6768
| ResizeResponse.Request_for_no_segments nr ->
68-
Printf.fprintf stderr "Request for an illegal number of segments: %Ld\n%!" nr;
69+
stderr "Request for an illegal number of segments: %Ld" nr
70+
>>= fun () ->
6971
exit 2
7072
| ResizeResponse.Success ->
7173
return () in

xenvm/lvs.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ let lvs copts noheadings nosuffix units fields (vg_name,lv_name_opt) =
4040
Lwt.catch
4141
(Client.get)
4242
(fun _ ->
43-
Printf.fprintf stderr " Volume group \"%s\" not found\n" vg_name;
44-
Printf.fprintf stderr " Skipping volume group %s\n%!" vg_name;
43+
stderr " Volume group \"%s\" not found" vg_name
44+
>>= fun () ->
45+
stderr " Skipping volume group %s" vg_name
46+
>>= fun () ->
4547
exit 1)
4648
>>= fun vg ->
4749

@@ -54,8 +56,8 @@ let lvs copts noheadings nosuffix units fields (vg_name,lv_name_opt) =
5456
let lv = List.find (fun lv -> lv.Lvm.Lv.name = lv_name) lvs in
5557
do_row dev vg lv
5658
in
57-
print_table noheadings (" "::headings) (List.map (fun r -> " "::r) rows);
58-
Lwt.return ()
59+
let lines = print_table noheadings (" "::headings) (List.map (fun r -> " "::r) rows) in
60+
Lwt_list.iter_s (fun x -> stdout "%s" x) lines
5961
)
6062

6163
let lvs_cmd =

xenvm/pvcreate.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ module Pv_IO = Pv.Make(Block)
99

1010
let pvcreate copts ff y metadatasize filenames =
1111
let open Xenvm_common in
12-
Printf.fprintf stderr "NOTE: pvcreate is currently a no-op\n%!";
13-
()
12+
Lwt_main.run (
13+
stderr "NOTE: pvcreate is currently a no-op"
14+
)
1415

1516
let f =
1617
let doc = "Force the command and override safety-checks" in

xenvm/pvs.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ let pvs copts noheadings nosuffix units fields devices =
3232
vg.Lvm.Vg.pvs in
3333
let headings = headings_of fields in
3434
let rows = List.concat (List.map (fun vg -> do_row (Vg_IO.metadata_of vg)) vgs) in
35-
print_table noheadings (" "::headings) (List.map (fun r -> " "::r) rows);
36-
Lwt.return ()
35+
let lines = print_table noheadings (" "::headings) (List.map (fun r -> " "::r) rows) in
36+
Lwt_list.iter_s (fun x -> stdout "%s" x) lines
3737
)
3838

3939
let pvs_cmd =

xenvm/vgs.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ let vgs copts noheadings nosuffix units fields vg_names =
5050
)
5151
)
5252
(fun _ ->
53-
Printf.fprintf stderr " Volume group \"%s\" not found\n" vg_name;
54-
Printf.fprintf stderr " Skipping volume group %s\n%!" vg_name;
53+
stderr " Volume group \"%s\" not found" vg_name
54+
>>= fun () ->
55+
stderr " Skipping volume group %s" vg_name
56+
>>= fun () ->
5557
exit 1)
5658
>>= fun vg ->
5759
Lwt.return (info,vg)) vg_names >>= fun vgs ->
5860
let rows = List.concat (List.map do_row vgs) in
59-
print_table noheadings (" "::headings) (List.map (fun r -> " "::r) rows);
60-
Lwt.return ()
61+
let lines = print_table noheadings (" "::headings) (List.map (fun r -> " "::r) rows) in
62+
Lwt_list.iter_s (fun x -> stdout "%s" x) lines
6163
)
6264

6365
let vgs_cmd =

xenvm/xenvm.ml

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ let table_of_vg vg =
4747
[ "free_space"; Int64.to_string (Pv.Allocator.size vg.Vg.free_space) ];
4848
]
4949

50-
let lvs config =
51-
set_uri config None;
52-
Lwt_main.run
53-
(Client.get () >>= fun vg ->
54-
print_table true [ "key"; "value" ] (table_of_vg vg);
55-
Lwt.return ())
56-
5750
let format config name filenames =
5851
let t =
5952
let module Vg_IO = Vg.Make(Log)(Block)(Time)(Clock) in
@@ -145,8 +138,8 @@ let host_list copts (vg_name,_) =
145138
fromLVM @ toLVM @ [ [ "freeExtents"; Int64.to_string h.freeExtents ] ] in
146139
List.map (fun h -> add_prefix h.name (table_of_host h)) hosts
147140
|> List.concat
148-
|> print_table true [ "key"; "value" ];
149-
return () in
141+
|> print_table true [ "key"; "value" ]
142+
|> Lwt_list.iter_s (fun x -> stdout "%s" x) in
150143
Lwt_main.run t
151144

152145
let shutdown copts (vg_name,_) =
@@ -162,7 +155,8 @@ let shutdown copts (vg_name,_) =
162155
(fun () ->
163156
Client.Host.all ()
164157
>>= fun _ ->
165-
Printf.fprintf stderr "Xenvmd is still alive: will sleep 5s and try again\n%!";
158+
stderr "Xenvmd is still alive: will sleep 5s and try again"
159+
>>= fun () ->
166160
Lwt_unix.sleep 5.
167161
) (fun _ ->
168162
finished := true;
@@ -184,8 +178,9 @@ let benchmark copts (vg_name,_) =
184178
| n ->
185179
f n
186180
>>= fun () ->
187-
if ((n * 100) / number) <> (((n + 1) * 100) / number)
188-
then Printf.fprintf stderr "%s %d %% complete\n%!" test_name (100 - (n * 100) / number);
181+
( if ((n * 100) / number) <> (((n + 1) * 100) / number)
182+
then stderr "%s %d %% complete\n%!" test_name (100 - (n * 100) / number)
183+
else return () ) >>= fun () ->
189184
fori test_name ((number - n, Unix.gettimeofday () -. start) :: acc) f (n - 1) in
190185
fori "Creating volumes" [] (fun i -> Client.create ~name:(Printf.sprintf "test-lv-%d" i) ~size:mib ~tags:[]) number
191186
>>= fun creates ->
@@ -205,8 +200,9 @@ let benchmark copts (vg_name,_) =
205200
Lwt_main.run t
206201

207202
let help config =
208-
Printf.printf "help - %s %s\n" config.config (match config.uri_override with | Some u -> u | None -> "URI unset")
209-
203+
Lwt_main.run (
204+
stdout "help - %s %s" config.config (match config.uri_override with | Some u -> u | None -> "URI unset")
205+
)
210206

211207
open Cmdliner
212208
let info =
@@ -242,17 +238,6 @@ let size =
242238
let doc = "Size of the LV in megs" in
243239
Arg.(value & opt int64 4L & info ["size"] ~docv:"SIZE" ~doc)
244240

245-
246-
let lvs_cmd =
247-
let doc = "List the logical volumes in the VG" in
248-
let man = [
249-
`S "DESCRIPTION";
250-
`P "Contacts the XenVM LVM daemon and retreives a list of
251-
all of the currently defined logical volumes";
252-
] in
253-
Term.(pure lvs $ copts_t),
254-
Term.info "lvs" ~sdocs:copts_sect ~doc ~man
255-
256241
let format_cmd =
257242
let doc = "Format the specified file as a VG" in
258243
let man = [
@@ -377,6 +362,8 @@ let cmds = [
377362

378363
let () =
379364
Random.self_init ();
365+
Lwt_main.run (
366+
Lwt_log.log ~logger:syslog ~level:Lwt_log.Notice (String.concat " " (Array.to_list Sys.argv))
367+
);
380368
match Term.eval_choice default_cmd cmds with
381369
| `Error _ -> exit 1 | _ -> exit 0
382-

xenvm/xenvm_common.ml

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

6+
let syslog = Lwt_log.syslog ~facility:`Daemon ()
7+
8+
let stdout fmt = Printf.ksprintf (fun s ->
9+
Printf.printf "%s\n%!" s;
10+
Lwt_log.log ~logger:syslog ~level:Lwt_log.Notice ("stdout:" ^ s)
11+
) fmt
12+
let stderr fmt = Printf.ksprintf (fun s ->
13+
Printf.fprintf stderr "%s\n%!" s;
14+
Lwt_log.log ~logger:syslog ~level:Lwt_log.Notice ("stderr:" ^ s)
15+
) fmt
16+
617
module Time = struct
718
type 'a io = 'a Lwt.t
819
let sleep = Lwt_unix.sleep
@@ -355,10 +366,12 @@ let set_vg_info_t copts uri local_device local_allocator_path unix_domain_sock_p
355366
Lwt_io.fprintf f "%s" s))
356367
(function
357368
| Unix.Unix_error(Unix.ENOENT, _, s) ->
358-
Printf.fprintf stderr "Unable to open file: Does the config dir '%s' exist?\n" copts.config;
369+
stderr "Unable to open file: Does the config dir '%s' exist?" copts.config
370+
>>= fun () ->
359371
exit 1
360372
| Unix.Unix_error(Unix.EACCES, _, _) ->
361-
Printf.fprintf stderr "Permission denied. You may need to rerun with 'sudo'\n";
373+
stderr "Permission denied. You may need to rerun with 'sudo'"
374+
>>= fun () ->
362375
exit 1
363376
|e -> Lwt.fail e)
364377

@@ -422,10 +435,10 @@ let print_table noheadings header rows =
422435
List.fold_left max 0 widths in
423436
let widths = List.rev (snd(List.fold_left (fun (i, acc) _ -> (i + 1, (width_of_column i) :: acc)) (0, []) header)) in
424437
let print_row row =
425-
List.iter (fun (n, s) -> Printf.printf "%s " (padto ' ' n s)) (List.combine widths row);
426-
Printf.printf "\n" in
427-
if not noheadings then print_row header;
428-
List.iter print_row rows
438+
String.concat "" (List.map (fun (n, s) -> Printf.sprintf "%s " (padto ' ' n s)) (List.combine widths row)) in
439+
if noheadings
440+
then List.map print_row rows
441+
else print_row header :: (List.map print_row rows)
429442

430443
let (>>*=) m f = match m with
431444
| `Error (`Msg e) -> fail (Failure e)

0 commit comments

Comments
 (0)