Skip to content

Commit

Permalink
Merge pull request #5 from Powa458/add-functionality-to-create-wim-image
Browse files Browse the repository at this point in the history
Add functionality to create wim image
  • Loading branch information
marmold authored Oct 2, 2022
2 parents b693a5c + afb4a26 commit f906148
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
3 changes: 2 additions & 1 deletion example.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
{
"type": "wim",
"image_name": "test-image",
"image_description": "This is a test description"
"image_description": "This is a test description",
"image_compression": "ultra"
}
]
}
3 changes: 2 additions & 1 deletion post-processor/create/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func (w *WimArtifact) State(name string) interface{} {
}

func (w *WimArtifact) Destroy() error {
return os.Remove(w.Path)
os.Remove(w.Path)
return nil
}
59 changes: 35 additions & 24 deletions post-processor/create/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,27 @@ func (pp *PostProcessor) Configure(raws ...interface{}) error {

// Set any defaults if needed.
if pp.config.ImageName == "" {
pp.config.ImageName = "" // Add deafult name later.
pp.config.ImageName = "test-wim-name"
}

// Return no errors if everything is good.
return nil
}

func (pp PostProcessor) PostProcess(context context.Context, ui packer.Ui, baseArtifact packer.Artifact) (newArtifact packer.Artifact, keep, mustKeep bool, err error) {
func (pp PostProcessor) PostProcess(context context.Context, ui packer.Ui, baseArtifact packer.Artifact) (packer.Artifact, bool, bool, error) {

// Get current working directory.
currentDir, err := os.Getwd()
if err != nil {
return nil, false, false, fmt.Errorf("Unabel to get current working directory path")
}
ui.Message(fmt.Sprintf("Current directory: '%s'", currentDir))

// Declare new final artifact
newArtifact := &WimArtifact{
Path: strings.Join([]string{currentDir, "wim"}, "\\"),
Name: pp.config.ImageName,
}

// Check if the source file is VHDX (VHD also?) format.
source := ""
Expand All @@ -77,53 +90,51 @@ func (pp PostProcessor) PostProcess(context context.Context, ui packer.Ui, baseA
}
}

// Get current working directory.
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
ui.Message(fmt.Sprintf("Current directory: '%s'", currentDir))

// Create base directory to be used as workspace for artifact creation.
baseDir := strings.Join([]string{currentDir, "wim"}, "\\")
err = os.Mkdir(baseDir, 0777)
// Should the directory be ereased with all content within?
err = os.MkdirAll(newArtifact.Path, 0777)
if err != nil {
log.Fatal(err)
return nil, false, false, fmt.Errorf("Unable to create final directory for opeartions: '%s'.", newArtifact.Path)
}
ui.Message(fmt.Sprintf("Base directory for artifact created: '%s'", baseDir))
ui.Message(fmt.Sprintf("Base directory for artifact created: '%s'", newArtifact.Path))

// Create temp mount directory. The directory will be created with random number suffix.
mountDir, err := os.MkdirTemp(baseDir, "mount_")
mountDir, err := os.MkdirTemp(newArtifact.Path, "mount_")
if err != nil {
log.Fatal(err)
}
ui.Message(fmt.Sprintf("Mount directory created: '%s'", mountDir))
//defer os.RemoveAll(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("Unable to mount image %s to mount dir: %s", source, mountDir))
return nil, false, false, fmt.Errorf("Unable to mount image %s to mount dir: %s", source, mountDir)
}
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()
// Create WIM image from mounted directory.
wimPath := newArtifact.Path + "\\" + newArtifact.Name + ".wim"
ui.Message(fmt.Sprintf("Creating new WIM image under %s", wimPath))
err = exec.CommandContext(context, "cmd", "/c", "dism", "/Capture-Image", strings.Join([]string{"/ImageFile", wimPath}, ":"), strings.Join([]string{"/CaptureDir", mountDir}, ":"), strings.Join([]string{"/Name", "Test"}, ":")).Run()
if err != nil {
log.Fatal(err)
} else {
ui.Message(fmt.Sprintf("%s", out))
//ui.Message(fmt.Sprintf("Failed to create WIM image from mount dir: %s. Unmounting ...", mountDir))
exec.Command("cmd", "/c", "dism", "/Unmount-image", strings.Join([]string{"/mountdir", mountDir}, ":"), "/Discard").Run()
return nil, false, false, fmt.Errorf("Failed to create WIM image from mount dir: %s. Unmounting ...", mountDir)
}
ui.Message(fmt.Sprintf("WIM Image %s successfully created from: '%s'", wimPath, mountDir))

// 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)
// log.Fatal(err)
return nil, false, false, err
}
ui.Message(fmt.Sprintf("VHDX Image %s successfully unmounted from: '%s'", source, mountDir))

// Final return.
return newArtifact, keep, mustKeep, err
//TODO: Should we also add support for Keep and MustKeep paramter here? Currnetly both set to false.
return newArtifact, false, false, err
}

0 comments on commit f906148

Please sign in to comment.