Files
libguestfs/daemon/cryptsetup.ml
Richard W.M. Jones f9edfc9a18 Update common submodule
This pulls in the commits below, requiring us to replace all uses of
String.is_prefix and String.is_suffix.

Mostly done with Perl like this, and carefully checked by hand
afterwards since this doesn't get everything right:

  $ perl -pi.bak -e 's/String.is_prefix ([^[:space:]\)]+) ([^[:space:]\)]+)/String.starts_with \2 \1/g' -- `git ls-files`

  Richard W.M. Jones (3):
      mlstdutils: Fix comment that still referred to the old function names
      mldrivers: Link to gettext-stub if ocaml-gettext is enabled
      mlstdutils: Rename String.is_prefix -> starts_with, is_suffix -> ends_with
2025-05-11 21:29:23 +01:00

80 lines
2.8 KiB
OCaml

(* guestfs-inspection
* Copyright (C) 2009-2025 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)
open Printf
open Unix
open Std_utils
open Utils
let cryptsetup_open ?(readonly = false) ?crypttype ?cipher device key mapname =
(* Sanity check: /dev/mapper/mapname must not exist already. Note
* that the device-mapper control device (/dev/mapper/control) is
* always there, so you can't ever have mapname == "control".
*)
let devmapper = sprintf "/dev/mapper/%s" mapname in
if is_block_device devmapper then
failwithf "%s: device already exists" devmapper;
(* Heuristically determine the encryption type. *)
let crypttype =
match crypttype with
| Some s -> s
| None ->
let t = Blkid.vfs_type (Mountable.of_device device) in
match t with
| "crypto_LUKS" -> "luks"
| "BitLocker" -> "bitlk"
| _ ->
failwithf "%s: unknown encrypted device type" t in
(* Write the key to a temporary file. *)
let keyfile, chan = Filename.open_temp_file "crypt" ".key" in
output_string chan key;
close_out chan;
let args = ref [] in
List.push_back_list args ["-d"; keyfile];
if readonly then List.push_back args "--readonly";
List.push_back_list args ["open"; device; mapname; "--type"; crypttype];
Option.iter (fun s -> List.push_back_list args ["--cipher"; s]) cipher;
(* Make sure we always remove the temporary file. *)
Fun.protect (fun () -> ignore (command "cryptsetup" !args))
~finally:(fun () -> unlink keyfile);
udev_settle ()
let cryptsetup_close device =
(* Must be /dev/mapper/... *)
if not (String.starts_with "/dev/mapper/" device) then
failwithf "%s: you must call this on the /dev/mapper device created by cryptsetup-open" device;
let mapname = String.sub device 12 (String.length device - 12) in
ignore (command "cryptsetup" ["close"; mapname]);
udev_settle ()
(* Deprecated APIs for backwards compatibility. *)
let luks_open device key mapname =
cryptsetup_open ~crypttype:"luks" device key mapname
let luks_open_ro device key mapname =
cryptsetup_open ~crypttype:"luks" ~readonly:true device key mapname
let luks_close = cryptsetup_close