From 20a0df54cb453cdb8682a9a30c7943431ba99e31 Mon Sep 17 00:00:00 2001 From: Powa458 Date: Sat, 5 Mar 2022 14:56:43 +0100 Subject: [PATCH] Add functionality to mount VHDX image to mount directory. Add simple listing of mount directory. Add functionality to unmount VHDX image. --- post-processor/create/post-processor.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/post-processor/create/post-processor.go b/post-processor/create/post-processor.go index 20c3ba3..b685902 100644 --- a/post-processor/create/post-processor.go +++ b/post-processor/create/post-processor.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "os" + "os/exec" "path/filepath" "strings" @@ -99,6 +100,30 @@ func (pp PostProcessor) PostProcess(context context.Context, ui packer.Ui, baseA ui.Message(fmt.Sprintf("Mount directory created: '%s'", mountDir)) //defer os.RemoveAll(mountDir) + // Mount VHDX image to mount directory. + err = exec.CommandContext(context, "cmd", "/c", "dism", "/mount-image", strings.Join([]string{"/imagefile", source}, ":"), "/Index:1", strings.Join([]string{"/mountdir", mountDir}, ":")).Run() + if err != nil { + ui.Message(fmt.Sprintf("Unable to mount image %s to mount dir: %s", source, mountDir)) + log.Fatal(err) + } + ui.Message(fmt.Sprintf("VHDX Image %s successfully mounted to: '%s'", source, mountDir)) + + // Simple listing of files in mount directory just for test. + out, err := exec.Command("cmd", "/c", "dir", mountDir).CombinedOutput() + if err != nil { + log.Fatal(err) + } else { + ui.Message(fmt.Sprintf("%s", out)) + } + + // Unmount VHDX image from mount directory if eveyrthing went well. + err = exec.CommandContext(context, "cmd", "/c", "dism", "/Unmount-image", strings.Join([]string{"/mountdir", mountDir}, ":"), "/Discard").Run() + if err != nil { + ui.Message(fmt.Sprintf("Failed to unmount image %s from mount dir: %s", source, mountDir)) + log.Fatal(err) + } + ui.Message(fmt.Sprintf("VHDX Image %s successfully unmounted from: '%s'", source, mountDir)) + // Final return. return newArtifact, keep, mustKeep, err }