Skip to content

Commit

Permalink
Merge pull request #838 from nanjj/x86
Browse files Browse the repository at this point in the history
Support x86 architecture and refactor the detecting logic
  • Loading branch information
stgraber authored Apr 12, 2024
2 parents 87df3c9 + b237c6c commit 13d4645
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 45 deletions.
20 changes: 8 additions & 12 deletions distrobuilder/main_repack-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ type cmdRepackWindows struct {
flagWindowsVersion string
flagWindowsArchitecture string

defaultDrivers string
supportedVersions []string
supportedArchitectures []string
umounts []string
defaultDrivers string
umounts []string
}

func init() {
Expand Down Expand Up @@ -101,13 +99,11 @@ func (c *cmdRepackWindows) command() *cobra.Command {
}

c.defaultDrivers = "virtio-win.iso"
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", "",
"Windows version to repack, must be one of ["+strings.Join(c.supportedVersions, ", ")+"]``")
"Windows version to repack, must be one of ["+strings.Join(shared.SupportedWindowsVersions, ", ")+"]``")
cmd.Flags().StringVar(&c.flagWindowsArchitecture, "windows-arch", "",
"Windows architecture to repack, must be one of ["+strings.Join(c.supportedArchitectures, ", ")+"]``")
"Windows architecture to repack, must be one of ["+strings.Join(shared.SupportedWindowsArchitectures, ", ")+"]``")

return cmd
}
Expand All @@ -119,16 +115,16 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
if c.flagWindowsVersion == "" {
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 !slices.Contains(shared.SupportedWindowsVersions, c.flagWindowsVersion) {
return fmt.Errorf("Version must be one of %v", shared.SupportedWindowsVersions)
}
}

if c.flagWindowsArchitecture == "" {
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)
if !slices.Contains(shared.SupportedWindowsArchitectures, c.flagWindowsArchitecture) {
return fmt.Errorf("Architecture must be one of %v", shared.SupportedWindowsArchitectures)
}
}

Expand Down
159 changes: 159 additions & 0 deletions shared/testdata/w7_install_wim_info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
WIM Information:
----------------
Path: mnt/sources/install.wim
GUID: 0x4ff490bcaa35fe489fa00b6c63c6b4d7
Version: 68864
Image Count: 5
Compression: LZX
Chunk Size: 32768 bytes
Part Number: 1/1
Boot Index: 0
Size: 2241959924 bytes
Attributes: Integrity info, Relative path junction

Available Images:
-----------------
Index: 1
Name: Windows 7 STARTER
Description: Windows 7 STARTER
Display Name: Windows 7 简易版
Display Description: Windows 7 简易版
Directory Count: 9550
File Count: 47318
Total Bytes: 7983637109
Hard Link Bytes: 3045021372
Creation Time: Tue Jul 14 05:24:44 2009 UTC
Last Modification Time: Tue Jul 14 09:14:17 2009 UTC
Architecture: x86
Product Name: Microsoft® Windows® Operating System
Edition ID: Starter
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7600
Service Pack Build: 16385
Service Pack Level: 0
Flags: Starter
WIMBoot compatible: no

Index: 2
Name: Windows 7 HOMEBASIC
Description: Windows 7 HOMEBASIC
Display Name: Windows 7 家庭普通版
Display Description: Windows 7 家庭普通版
Directory Count: 9561
File Count: 47403
Total Bytes: 8003795881
Hard Link Bytes: 3060203459
Creation Time: Tue Jul 14 05:24:44 2009 UTC
Last Modification Time: Tue Jul 14 09:15:04 2009 UTC
Architecture: x86
Product Name: Microsoft® Windows® Operating System
Edition ID: HomeBasic
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7600
Service Pack Build: 16385
Service Pack Level: 0
Flags: HomeBasic
WIMBoot compatible: no

Index: 3
Name: Windows 7 HOMEPREMIUM
Description: Windows 7 HOMEPREMIUM
Display Name: Windows 7 家庭高级版
Display Description: Windows 7 家庭高级版
Directory Count: 9779
File Count: 48416
Total Bytes: 8445655979
Hard Link Bytes: 3439427655
Creation Time: Tue Jul 14 05:24:44 2009 UTC
Last Modification Time: Tue Jul 14 09:15:50 2009 UTC
Architecture: x86
Product Name: Microsoft® Windows® Operating System
Edition ID: HomePremium
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7600
Service Pack Build: 16385
Service Pack Level: 0
Flags: HomePremium
WIMBoot compatible: no

Index: 4
Name: Windows 7 PROFESSIONAL
Description: Windows 7 PROFESSIONAL
Display Name: Windows 7 专业版
Display Description: Windows 7 专业版
Directory Count: 9836
File Count: 48866
Total Bytes: 8326968857
Hard Link Bytes: 3305882953
Creation Time: Tue Jul 14 05:24:44 2009 UTC
Last Modification Time: Tue Jul 14 09:16:14 2009 UTC
Architecture: x86
Product Name: Microsoft® Windows® Operating System
Edition ID: Professional
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7600
Service Pack Build: 16385
Service Pack Level: 0
Flags: Professional
WIMBoot compatible: no

Index: 5
Name: Windows 7 ULTIMATE
Description: Windows 7 ULTIMATE
Display Name: Windows 7 旗舰版
Display Description: Windows 7 旗舰版
Directory Count: 9866
File Count: 49019
Total Bytes: 8485352280
Hard Link Bytes: 3463057728
Creation Time: Tue Jul 14 05:24:44 2009 UTC
Last Modification Time: Tue Jul 14 09:16:39 2009 UTC
Architecture: x86
Product Name: Microsoft® Windows® Operating System
Edition ID: Ultimate
Installation Type: Client
HAL: acpiapic
Product Type: WinNT
Product Suite: Terminal Server
Languages: zh-CN
Default Language: zh-CN
System Root: WINDOWS
Major Version: 6
Minor Version: 1
Build: 7600
Service Pack Build: 16385
Service Pack Level: 0
Flags: Ultimate
WIMBoot compatible: no
70 changes: 37 additions & 33 deletions shared/wiminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"strings"
)

var (
SupportedWindowsVersions = []string{
"w11", "w10", "w8", "w7", "2k19", "2k12", "2k16",
"2k22", "2k3", "2k8", "xp", "2k12r2", "2k8r2", "w8.1"}
SupportedWindowsArchitectures = []string{
"amd64", "ARM64", "x86"}
)

type WimInfo map[int]map[string]string

func (info WimInfo) ImageCount() int {
Expand All @@ -27,6 +35,20 @@ func (info WimInfo) Architecture(index int) string {
return info[index]["Architecture"]
}

type Aliases map[string][]string

func (as Aliases) MatchString(desc string) string {
for k, v := range as {
for _, a := range v {
if regexp.MustCompile(fmt.Sprintf("(?i)%s", a)).MatchString(desc) {
return k
}
}
}

return ""
}

func ParseWimInfo(r io.Reader) (info WimInfo, err error) {
scanner := bufio.NewScanner(r)
nextSection := func() (sect map[string]string) {
Expand Down Expand Up @@ -84,22 +106,17 @@ func ParseWimInfo(r io.Reader) (info WimInfo, err error) {
return
}

func DetectWindowsVersion(desc string) string {
aliases := map[string][]string{
func DetectWindowsVersion(desc string) (version string) {
version = Aliases{
"2k12r2": {"2k12r2", "w2k12r2", "win2k12r2", "windows.?server.?2012?.r2"},
"2k8r2": {"2k8r2", "w2k8r2", "win2k8r2", "windows.?server.?2008?.r2"},
"w8.1": {"w8.1", "win8.1", "windows.?8.1"},
}.MatchString(desc)
if version != "" {
return
}

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

aliases = map[string][]string{
return Aliases{
"w11": {"w11", "win11", "windows.?11"},
"w10": {"w10", "win10", "windows.?10"},
"w8": {"w8", "win8", "windows.?8"},
Expand All @@ -111,32 +128,19 @@ func DetectWindowsVersion(desc string) string {
"2k3": {"2k3", "w2k3", "win2k3", "windows.?server.?2003"},
"2k8": {"2k8", "w2k8", "win2k8", "windows.?server.?2008"},
"xp": {"xp", "wxp", "winxp", "windows.?xp"},
}

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

return ""
}.MatchString(desc)
}

func DetectWindowsArchitecture(desc string) string {
aliases := map[string][]string{
func DetectWindowsArchitecture(desc string) (arch string) {
arch = Aliases{
"amd64": {"amd64", "x64", "x86_64"},
"ARM64": {"arm64", "aarch64"},
}.MatchString(desc)
if arch != "" {
return
}

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

return ""
return Aliases{
"x86": {"x86_32", "x86"},
}.MatchString(desc)
}
11 changes: 11 additions & 0 deletions shared/wiminfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestDetectWindowsVersion(t *testing.T) {
{"testdata/2k19_install_wim_info.txt", "2k19", "amd64"},
{"testdata/w8_install_wim_info.txt", "w8", "amd64"},
{"testdata/2k12r2_install_wim_info.txt", "2k12r2", "amd64"},
{"testdata/w7_install_wim_info.txt", "w7", "x86"},
}

for _, tc := range tcs {
Expand Down Expand Up @@ -185,6 +186,11 @@ func TestDetectWindowsVersionFromFilename(t *testing.T) {
args{"Windows_Server_2k12.iso"},
"2k12",
},
{
"Windows 7",
args{"cn_windows_7_professional_x86_dvd_x15-65790.iso"},
"w7",
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -217,6 +223,11 @@ func TestDetectWindowsArchitectureFromFilename(t *testing.T) {
args{"Win10_22H2_English_arm64.iso"},
"ARM64",
},
{
"Windows 7",
args{"cn_windows_7_professional_x86_dvd_x15-65790.iso"},
"x86",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 13d4645

Please sign in to comment.