|
| 1 | +(* LVM compatible bits and pieces *) |
| 2 | + |
| 3 | +open Cmdliner |
| 4 | +open Lwt |
| 5 | +open Xenvm_common |
| 6 | +open Errors |
| 7 | + |
| 8 | +module Vg_IO = Lvm.Vg.Make(Log)(Block)(Time)(Clock) |
| 9 | + |
| 10 | +let to_file (vg_name, lv_name) local_device oc = |
| 11 | + with_block local_device |
| 12 | + (fun x -> |
| 13 | + Vg_IO.connect [ x ] `RO >>|= fun vg -> |
| 14 | + match Vg_IO.find vg lv_name with |
| 15 | + | None -> failwith (Printf.sprintf "Failed to find LV %s" lv_name) |
| 16 | + | Some vol -> |
| 17 | + Vg_IO.Volume.connect vol |
| 18 | + >>= function |
| 19 | + | `Error _ -> fail (Failure (Printf.sprintf "Failed to open %s" lv_name)) |
| 20 | + | `Ok disk -> |
| 21 | + Vg_IO.Volume.get_info disk |
| 22 | + >>= fun info -> |
| 23 | + let buffer = Io_page.(to_cstruct (get 1024)) in |
| 24 | + let nsectors = Cstruct.len buffer / info.Vg_IO.Volume.sector_size in |
| 25 | + let rec loop = function |
| 26 | + | n when n = info.Vg_IO.Volume.size_sectors -> return () |
| 27 | + | n -> |
| 28 | + let remaining = Int64.sub info.Vg_IO.Volume.size_sectors n in |
| 29 | + let toread = min (Int64.to_int remaining) nsectors in |
| 30 | + let buffer' = Cstruct.sub buffer 0 (toread * info.Vg_IO.Volume.sector_size) in |
| 31 | + Vg_IO.Volume.read disk n [ buffer' ] |
| 32 | + >>= function |
| 33 | + | `Ok () -> |
| 34 | + Lwt_io.write oc (Cstruct.to_string buffer') |
| 35 | + >>= fun () -> |
| 36 | + loop (Int64.(add n (of_int toread))) |
| 37 | + | _ -> failwith (Printf.sprintf "Failed to read sector %Ld" n) in |
| 38 | + loop 0L |
| 39 | + ) |
| 40 | + |
| 41 | +let lvdump copts (vg_name, lv_name_opt) physical_device : unit = |
| 42 | + let open Xenvm_common in |
| 43 | + let lv_name = match lv_name_opt with Some l -> l | None -> failwith "Need LV name" in |
| 44 | + Lwt_main.run ( |
| 45 | + get_vg_info_t copts vg_name >>= fun info -> |
| 46 | + set_uri copts info; |
| 47 | + let local_device : string = match (info,physical_device) with |
| 48 | + | _, Some d -> d (* cmdline overrides default for the VG *) |
| 49 | + | Some info, None -> info.local_device (* If we've got a default, use that *) |
| 50 | + | None, None -> failwith "Need to know the local device!" in |
| 51 | + to_file (vg_name, lv_name) local_device Lwt_io.stdout |
| 52 | + ) |
| 53 | + |
| 54 | +let lvdump_cmd = |
| 55 | + let doc = "Dump the physical contents of a logical volume" in |
| 56 | + let man = [ |
| 57 | + `S "DESCRIPTION"; |
| 58 | + `P "lvdump allows you to dump the physical contents of a logical volume to stdout." |
| 59 | + ] in |
| 60 | + Term.(pure lvdump $ Xenvm_common.copts_t $ Xenvm_common.name_arg $ Xenvm_common.physical_device_arg), |
| 61 | + Term.info "lvdump" ~sdocs:"COMMON OPTIONS" ~doc ~man |
0 commit comments