Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor WIM info parsing #837

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 65 additions & 86 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *cmdRepackWindows) command() *cobra.Command {
}

c.defaultDrivers = "virtio-win.iso"
c.supportedVersions = []string{"w11", "w10", "2k19", "2k12", "2k16", "2k22"}
c.supportedVersions = []string{"w11", "w10", "w8", "w7", "2k19", "2k12", "2k16", "2k22", "2k3", "2k8", "xp", "2k12r2", "2k8r2", "w8.1"}
c.supportedArchitectures = []string{"amd64", "ARM64"}
cmd.Flags().StringVar(&c.flagDrivers, "drivers", c.defaultDrivers, "Path to virtio windowns drivers ISO file"+"``")
cmd.Flags().StringVar(&c.flagWindowsVersion, "windows-version", "",
Expand All @@ -117,27 +117,15 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
logger := c.global.logger

if c.flagWindowsVersion == "" {
detectedVersion := detectWindowsVersion(filepath.Base(args[0]))

if detectedVersion == "" {
return errors.New("Failed to detect Windows version. Please provide the version using the --windows-version flag")
}

c.flagWindowsVersion = detectedVersion
c.flagWindowsVersion = shared.DetectWindowsVersion(filepath.Base(args[0]))
} else {
if !slices.Contains(c.supportedVersions, c.flagWindowsVersion) {
return fmt.Errorf("Version must be one of %v", c.supportedVersions)
}
}

if c.flagWindowsArchitecture == "" {
detectedArchitecture := detectWindowsArchitecture(filepath.Base(args[0]))

if detectedArchitecture == "" {
return errors.New("Failed to detect Windows architecture. Please provide the architecture using the --windows-arch flag")
}

c.flagWindowsArchitecture = detectedArchitecture
c.flagWindowsArchitecture = shared.DetectWindowsArchitecture(filepath.Base(args[0]))
} else {
if !slices.Contains(c.supportedArchitectures, c.flagWindowsArchitecture) {
return fmt.Errorf("Architecture must be one of %v", c.supportedArchitectures)
Expand Down Expand Up @@ -276,43 +264,42 @@ func (c *cmdRepackWindows) run(cmd *cobra.Command, args []string, overlayDir str
return fmt.Errorf("Unable to find install.wim: %w", err)
}

var buf bytes.Buffer

err = shared.RunCommand(c.global.ctx, nil, &buf, "wimlib-imagex", "info", installWim)
bootWimInfo, err := c.getWimInfo(bootWim)
if err != nil {
return fmt.Errorf("Failed to retrieve wim file information: %w", err)
return fmt.Errorf("Failed to get boot wim info: %w", err)
}

indexes := []int{}
scanner := bufio.NewScanner(&buf)
installWimInfo, err := c.getWimInfo(installWim)
if err != nil {
return fmt.Errorf("Failed to get install wim info: %w", err)
}

for scanner.Scan() {
text := scanner.Text()
if c.flagWindowsVersion == "" {
c.flagWindowsVersion = shared.DetectWindowsVersion(installWimInfo.Name(1))
}

if strings.HasPrefix(text, "Index") {
fields := strings.Split(text, " ")
if c.flagWindowsArchitecture == "" {
c.flagWindowsArchitecture = shared.DetectWindowsArchitecture(installWimInfo.Architecture(1))
}

index, err := strconv.Atoi(fields[len(fields)-1])
if err != nil {
return fmt.Errorf("Failed to determine wim file indexes: %w", err)
}
if c.flagWindowsVersion == "" {
return errors.New("Failed to detect Windows version. Please provide the version using the --windows-version flag")
}

indexes = append(indexes, index)
}
if c.flagWindowsArchitecture == "" {
return errors.New("Failed to detect Windows architecture. Please provide the architecture using the --windows-arch flag")
}

// This injects the drivers into the installation process
err = c.modifyWim(bootWim, 2)
err = c.modifyWim(bootWim, bootWimInfo)
if err != nil {
return fmt.Errorf("Failed to modify index 2 of %q: %w", filepath.Base(bootWim), err)
return fmt.Errorf("Failed to modify wim %q: %w", filepath.Base(bootWim), err)
}

// This injects the drivers into the final OS
for _, idx := range indexes {
err = c.modifyWim(installWim, idx)
if err != nil {
return fmt.Errorf("Failed to modify index %d of %q: %w", idx, filepath.Base(installWim), err)
}
err = c.modifyWim(installWim, installWimInfo)
if err != nil {
return fmt.Errorf("Failed to modify wim %q: %w", filepath.Base(installWim), err)
}

logger.Info("Generating new ISO")
Expand Down Expand Up @@ -350,14 +337,43 @@ func (c *cmdRepackWindows) run(cmd *cobra.Command, args []string, overlayDir str
return nil
}

func (c *cmdRepackWindows) modifyWim(path string, index int) error {
logger := c.global.logger
func (c *cmdRepackWindows) getWimInfo(wimFile string) (info shared.WimInfo, err error) {
wimName := filepath.Base(wimFile)
var buf bytes.Buffer
err = shared.RunCommand(c.global.ctx, nil, &buf, "wimlib-imagex", "info", wimFile)
if err != nil {
err = fmt.Errorf("Failed to retrieve wim %q information: %w", wimName, err)
return
}

// Mount wim file
wimFile := filepath.Join(path)
info, err = shared.ParseWimInfo(&buf)
if err != nil {
err = fmt.Errorf("Failed to parse wim info %s: %w", wimFile, err)
return
}

return
}

func (c *cmdRepackWindows) modifyWim(wimFile string, info shared.WimInfo) (err error) {
wimName := filepath.Base(wimFile)
// Injects the drivers
for idx := 1; idx <= info.ImageCount(); idx++ {
name := info.Name(idx)
err = c.modifyWimIndex(wimFile, idx, name)
if err != nil {
return fmt.Errorf("Failed to modify index %d=%s of %q: %w", idx, name, wimName, err)
}
}
return
}

func (c *cmdRepackWindows) modifyWimIndex(wimFile string, index int, name string) error {
wimIndex := strconv.Itoa(index)
wimPath := filepath.Join(c.global.flagCacheDir, "wim", wimIndex)

wimName := filepath.Base(wimFile)
logger := c.global.logger.WithFields(logrus.Fields{"wim": strings.TrimSuffix(wimName, ".wim"),
"idx": wimIndex + ":" + name})
if !incus.PathExists(wimPath) {
err := os.MkdirAll(wimPath, 0755)
if err != nil {
Expand All @@ -366,10 +382,11 @@ func (c *cmdRepackWindows) modifyWim(path string, index int) error {
}

success := false

logger.Info("Mounting")
// Mount wim file
err := shared.RunCommand(c.global.ctx, nil, nil, "wimlib-imagex", "mountrw", wimFile, wimIndex, wimPath, "--allow-other")
if err != nil {
return fmt.Errorf("Failed to mount %q: %w", filepath.Base(wimFile), err)
return fmt.Errorf("Failed to mount %q: %w", wimName, err)
}

defer func() {
Expand All @@ -383,17 +400,17 @@ func (c *cmdRepackWindows) modifyWim(path string, index int) error {
return fmt.Errorf("Failed to get required windows directories: %w", err)
}

logger.WithFields(logrus.Fields{"file": filepath.Base(path), "index": index}).Info("Modifying WIM file")

logger.Info("Modifying")
// Create registry entries and copy files
err = c.injectDrivers(dirs)
if err != nil {
return fmt.Errorf("Failed to inject drivers: %w", err)
}

logger.Info("Unmounting")
err = shared.RunCommand(c.global.ctx, nil, nil, "wimlib-imagex", "unmount", wimPath, "--commit")
if err != nil {
return fmt.Errorf("Failed to unmount WIM image: %w", err)
return fmt.Errorf("Failed to unmount WIM image %q: %w", wimName, err)
}

success = true
Expand Down Expand Up @@ -605,44 +622,6 @@ func (c *cmdRepackWindows) injectDrivers(dirs map[string]string) error {
return nil
}

func detectWindowsVersion(fileName string) string {
aliases := map[string][]string{
"w11": {"w11", "win11", "windows.?11"},
"w10": {"w10", "win10", "windows.?10"},
"2k19": {"2k19", "w2k19", "win2k19", "windows.?server.?2019"},
"2k12": {"2k12", "w2k12", "win2k12", "windows.?server.?2012"},
"2k16": {"2k16", "w2k16", "win2k16", "windows.?server.?2016"},
"2k22": {"2k22", "w2k22", "win2k22", "windows.?server.?2022"},
}

for k, v := range aliases {
for _, alias := range v {
if regexp.MustCompile(fmt.Sprintf("(?i)%s", alias)).MatchString(fileName) {
return k
}
}
}

return ""
}

func detectWindowsArchitecture(fileName string) string {
aliases := map[string][]string{
"amd64": {"amd64", "x64"},
"ARM64": {"arm64"},
}

for k, v := range aliases {
for _, alias := range v {
if regexp.MustCompile(fmt.Sprintf("(?i)%s", alias)).MatchString(fileName) {
return k
}
}
}

return ""
}

// toHex is a pongo2 filter which converts the provided value to a hex value understood by the Windows registry.
func toHex(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
dst := make([]byte, hex.EncodedLen(len(in.String())))
Expand Down
136 changes: 0 additions & 136 deletions distrobuilder/main_repack-windows_test.go
Original file line number Diff line number Diff line change
@@ -1,137 +1 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_detectWindowsVersion(t *testing.T) {
type args struct {
fileName string
}

tests := []struct {
name string
args args
want string
}{
{
"Windows 11 (1)",
args{"Windows 11.iso"},
"w11",
},
{
"Windows 11 (2)",
args{"Windows11.iso"},
"w11",
},
{
"Windows 11 (3)",
args{"Win11.iso"},
"w11",
},
{
"Windows 11 (4)",
args{"Windows_11.iso"},
"w11",
},
{
"Windows 10 (1)",
args{"Windows 10.iso"},
"w10",
},
{
"Windows 10 (2)",
args{"Windows10.iso"},
"w10",
},
{
"Windows 10 (3)",
args{"Win10.iso"},
"w10",
},
{
"Windows 10 (4)",
args{"Windows_10.iso"},
"w10",
},
{
"Windows Server 2019 (1)",
args{"Windows_Server_2019.iso"},
"2k19",
},
{
"Windows Server 2019 (2)",
args{"Windows Server 2019.iso"},
"2k19",
},
{
"Windows Server 2019 (3)",
args{"WindowsServer2019.iso"},
"2k19",
},
{
"Windows Server 2019 (4)",
args{"Windows_Server_2k19.iso"},
"2k19",
},
{
"Windows Server 2012 (1)",
args{"Windows_Server_2012.iso"},
"2k12",
},
{
"Windows Server 2012 (2)",
args{"Windows Server 2012.iso"},
"2k12",
},
{
"Windows Server 2012 (3)",
args{"WindowsServer2012.iso"},
"2k12",
},
{
"Windows Server 2012 (4)",
args{"Windows_Server_2k12.iso"},
"2k12",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := detectWindowsVersion(tt.args.fileName)
assert.Equal(t, tt.want, got)
})
}
}

func Test_detectWindowsArchitecture(t *testing.T) {
type args struct {
fileName string
}

tests := []struct {
name string
args args
want string
}{
{
"Windows 11 (1)",
args{"Win10_22H2_English_x64.iso"},
"amd64",
},
{
"Windows 11 (2)",
args{"Win10_22H2_English_arm64.iso"},
"ARM64",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := detectWindowsArchitecture(tt.args.fileName)
assert.Equal(t, tt.want, got)
})
}
}
Loading
Loading