From 5b83355ceaf9d1f7f5e0061572d5658f3adcb3e3 Mon Sep 17 00:00:00 2001 From: Liran Rotenberg Date: Thu, 7 Dec 2023 17:52:24 +0200 Subject: [PATCH] Virt-v2v: read LUKS keys This patch will read the given LUKS key provided to the container and add them as arguments to the virt-v2v command. Since virt-v2v supports this feature only since 2.2, it applies only for cold migrations. It uses the `all` selector to each passphrase. Signed-off-by: Liran Rotenberg --- virt-v2v/cold/entrypoint.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/virt-v2v/cold/entrypoint.go b/virt-v2v/cold/entrypoint.go index 2a053b3f7..98af0ca4f 100644 --- a/virt-v2v/cold/entrypoint.go +++ b/virt-v2v/cold/entrypoint.go @@ -21,6 +21,7 @@ const ( FS = "/mnt/disks/disk[0-9]*" Block = "/dev/block[0-9]*" VDDK = "/opt/vmware-vix-disklib-distrib" + LUKSDIR = "/etc/luks" ) var ( @@ -128,6 +129,23 @@ func buildCommand() []string { virtV2vArgs = append(virtV2vArgs, "--mac", macToIp) } } + // Adds LUKS keys, if exist. + if _, err := os.Stat(LUKSDIR); os.IsNotExist(err) { + // do nothing + } else { + if err != nil { + fmt.Println("Error accessing the LUKS directory ", err) + os.Exit(1) + } + files, err := getFilesInPath(LUKSDIR) + if err != nil { + fmt.Println("Error reading files in LUKS directory ", err) + os.Exit(1) + } + for _, file := range files { + virtV2vArgs = append(virtV2vArgs, "--key", fmt.Sprintf("all:file:%s", file)) + } + } if info, err := os.Stat(VDDK); err == nil && info.IsDir() { virtV2vArgs = append(virtV2vArgs, @@ -141,6 +159,20 @@ func buildCommand() []string { return virtV2vArgs } +func getFilesInPath(rootPath string) (paths []string, err error) { + files, err := os.ReadDir(rootPath) + if err != nil { + fmt.Println("Error reading the files in the directory ", err) + return + } + for _, file := range files { + if !file.IsDir() && !strings.HasPrefix(file.Name(), "..") { + paths = append(paths, fmt.Sprintf("%s/%s", rootPath, file.Name())) + } + } + return +} + func checkEnvVariablesSet(envVars ...string) bool { for _, v := range envVars { if os.Getenv(v) == "" {