diff --git a/virt-v2v/cold/entrypoint.go b/virt-v2v/cold/entrypoint.go index 6692ba4fc..a28a7b368 100644 --- a/virt-v2v/cold/entrypoint.go +++ b/virt-v2v/cold/entrypoint.go @@ -20,6 +20,7 @@ const ( FS = "/mnt/disks/disk[0-9]*" Block = "/dev/block[0-9]*" VDDK = "/opt/vmware-vix-disklib-distrib" + LUKSDIR = "/etc/luks" ) var ( @@ -127,6 +128,25 @@ 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 && !os.IsNotExist(err) { + 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)) + } + } + // fallback to clevis, harmless + virtV2vArgs = append(virtV2vArgs, "--key", "all:clevis") if info, err := os.Stat(VDDK); err == nil && info.IsDir() { virtV2vArgs = append(virtV2vArgs, @@ -149,6 +169,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) == "" {