Skip to content

Commit

Permalink
Virt-v2v: read LUKS keys
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
liranr23 committed May 29, 2024
1 parent 3729a9a commit 7ba639b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -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) == "" {
Expand Down

0 comments on commit 7ba639b

Please sign in to comment.