1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
package windows
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/flosch/pongo2"
incus "github.com/lxc/incus/v6/shared/util"
"github.com/sirupsen/logrus"
"github.com/lxc/distrobuilder/shared"
)
type RepackUtil struct {
cacheDir string
ctx context.Context
logger *logrus.Logger
windowsVersion string
windowsArchitecture string
}
// NewRepackUtil returns a new RepackUtil object.
func NewRepackUtil(cacheDir string, ctx context.Context, logger *logrus.Logger) RepackUtil {
return RepackUtil{
cacheDir: cacheDir,
ctx: ctx,
logger: logger,
}
}
// SetWindowsVersionArchitecture is a helper function for setting the specific Windows version and architecture.
func (r *RepackUtil) SetWindowsVersionArchitecture(windowsVersion string, windowsArchitecture string) {
r.windowsVersion = windowsVersion
r.windowsArchitecture = windowsArchitecture
}
// GetWimInfo returns information about the specified wim file.
func (r *RepackUtil) GetWimInfo(wimFile string) (info WimInfo, err error) {
wimName := filepath.Base(wimFile)
var buf bytes.Buffer
err = shared.RunCommand(r.ctx, nil, &buf, "wimlib-imagex", "info", wimFile)
if err != nil {
err = fmt.Errorf("Failed to retrieve wim %q information: %w", wimName, err)
return
}
info, err = ParseWimInfo(&buf)
if err != nil {
err = fmt.Errorf("Failed to parse wim info %s: %w", wimFile, err)
return
}
return
}
// InjectDriversIntoWim will inject drivers into the specified wim file.
func (r *RepackUtil) InjectDriversIntoWim(wimFile string, info WimInfo, driverPath string) (err error) {
wimName := filepath.Base(wimFile)
// Injects the drivers
for idx := 1; idx <= info.ImageCount(); idx++ {
name := info.Name(idx)
err = r.modifyWimIndex(wimFile, idx, name, driverPath)
if err != nil {
return fmt.Errorf("Failed to modify index %d=%s of %q: %w", idx, name, wimName, err)
}
}
return
}
// InjectDrivers injects drivers from driverPath into the windowsRootPath.
func (r *RepackUtil) InjectDrivers(windowsRootPath string, driverPath string) error {
dirs, err := r.getWindowsDirectories(windowsRootPath)
if err != nil {
return fmt.Errorf("Failed to get required Windows directories under path '%s': %w", windowsRootPath, err)
}
logger := r.logger
driversRegistry := "Windows Registry Editor Version 5.00"
systemRegistry := "Windows Registry Editor Version 5.00"
softwareRegistry := "Windows Registry Editor Version 5.00"
for driverName, driverInfo := range Drivers {
logger.WithField("driver", driverName).Debug("Injecting driver")
infFilename := ""
sourceDir := filepath.Join(driverPath, driverName, r.windowsVersion, r.windowsArchitecture)
targetBaseDir := filepath.Join(dirs["filerepository"], driverInfo.PackageName)
if !incus.PathExists(targetBaseDir) {
err := os.MkdirAll(targetBaseDir, 0o755)
if err != nil {
logger.Error(err)
return err
}
}
for ext, dir := range map[string]string{"inf": dirs["inf"], "cat": dirs["drivers"], "dll": dirs["drivers"], "exe": dirs["drivers"], "sys": dirs["drivers"]} {
sourceMatches, err := shared.FindAllMatches(sourceDir, fmt.Sprintf("*.%s", ext))
if err != nil {
logger.Debugf("failed to find first match %q %q", driverName, ext)
continue
}
for _, sourcePath := range sourceMatches {
targetName := filepath.Base(sourcePath)
targetPath := filepath.Join(targetBaseDir, targetName)
if err = shared.Copy(sourcePath, targetPath); err != nil {
return err
}
if ext == "cat" || ext == "exe" {
continue
} else if ext == "inf" {
targetName = "oem-" + targetName
infFilename = targetName
}
targetPath = filepath.Join(dir, targetName)
if err = shared.Copy(sourcePath, targetPath); err != nil {
return err
}
}
}
classGuid, err := ParseDriverClassGuid(driverName, filepath.Join(dirs["inf"], infFilename))
if err != nil {
return err
}
ctx := pongo2.Context{
"infFile": infFilename,
"packageName": driverInfo.PackageName,
"driverName": driverName,
"classGuid": classGuid,
}
// Update Windows DRIVERS registry
if driverInfo.DriversRegistry != "" {
tpl, err := pongo2.FromString(driverInfo.DriversRegistry)
if err != nil {
return fmt.Errorf("Failed to parse template for driver %q: %w", driverName, err)
}
out, err := tpl.Execute(ctx)
if err != nil {
return fmt.Errorf("Failed to render template for driver %q: %w", driverName, err)
}
driversRegistry = fmt.Sprintf("%s\n\n%s", driversRegistry, out)
}
// Update Windows SYSTEM registry
if driverInfo.SystemRegistry != "" {
tpl, err := pongo2.FromString(driverInfo.SystemRegistry)
if err != nil {
return fmt.Errorf("Failed to parse template for driver %q: %w", driverName, err)
}
out, err := tpl.Execute(ctx)
if err != nil {
return fmt.Errorf("Failed to render template for driver %q: %w", driverName, err)
}
systemRegistry = fmt.Sprintf("%s\n\n%s", systemRegistry, out)
}
// Update Windows SOFTWARE registry
if driverInfo.SoftwareRegistry != "" {
tpl, err := pongo2.FromString(driverInfo.SoftwareRegistry)
if err != nil {
return fmt.Errorf("Failed to parse template for driver %q: %w", driverName, err)
}
out, err := tpl.Execute(ctx)
if err != nil {
return fmt.Errorf("Failed to render template for driver %q: %w", driverName, err)
}
softwareRegistry = fmt.Sprintf("%s\n\n%s", softwareRegistry, out)
}
}
logger.WithField("hivefile", "DRIVERS").Debug("Updating Windows registry")
err = shared.RunCommand(r.ctx, strings.NewReader(driversRegistry), nil, "hivexregedit", "--merge", "--prefix='HKEY_LOCAL_MACHINE\\DRIVERS'", filepath.Join(dirs["config"], "DRIVERS"))
if err != nil {
return fmt.Errorf("Failed to edit Windows DRIVERS registry: %w", err)
}
logger.WithField("hivefile", "SYSTEM").Debug("Updating Windows registry")
err = shared.RunCommand(r.ctx, strings.NewReader(systemRegistry), nil, "hivexregedit", "--merge", "--prefix='HKEY_LOCAL_MACHINE\\SYSTEM'", filepath.Join(dirs["config"], "SYSTEM"))
if err != nil {
return fmt.Errorf("Failed to edit Windows SYSTEM registry: %w", err)
}
logger.WithField("hivefile", "SOFTWARE").Debug("Updating Windows registry")
err = shared.RunCommand(r.ctx, strings.NewReader(softwareRegistry), nil, "hivexregedit", "--merge", "--prefix='HKEY_LOCAL_MACHINE\\SOFTWARE'", filepath.Join(dirs["config"], "SOFTWARE"))
if err != nil {
return fmt.Errorf("Failed to edit Windows SOFTWARE registry: %w", err)
}
return nil
}
func (r *RepackUtil) getWindowsDirectories(rootPath string) (dirs map[string]string, err error) {
dirs = map[string]string{}
dirs["inf"], err = shared.FindFirstMatch(rootPath, "windows", "inf")
if err != nil {
return nil, fmt.Errorf("Failed to determine windows/inf path: %w", err)
}
dirs["config"], err = shared.FindFirstMatch(rootPath, "windows", "system32", "config")
if err != nil {
return nil, fmt.Errorf("Failed to determine windows/system32/config path: %w", err)
}
dirs["drivers"], err = shared.FindFirstMatch(rootPath, "windows", "system32", "drivers")
if err != nil {
return nil, fmt.Errorf("Failed to determine windows/system32/drivers path: %w", err)
}
dirs["filerepository"], err = shared.FindFirstMatch(rootPath, "windows", "system32", "driverstore", "filerepository")
if err != nil {
return nil, fmt.Errorf("Failed to determine windows/system32/driverstore/filerepository path: %w", err)
}
return
}
func (r *RepackUtil) modifyWimIndex(wimFile string, index int, name string, driverPath string) error {
wimIndex := strconv.Itoa(index)
wimPath := filepath.Join(r.cacheDir, "wim", wimIndex)
wimName := filepath.Base(wimFile)
logger := r.logger.WithFields(logrus.Fields{"wim": wimName, "idx": wimIndex + ":" + name})
if !incus.PathExists(wimPath) {
err := os.MkdirAll(wimPath, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", wimPath, err)
}
}
success := false
logger.Info("Mounting")
// Mount wim file
err := shared.RunCommand(r.ctx, nil, nil, "wimlib-imagex", "mountrw", wimFile, wimIndex, wimPath, "--allow-other")
if err != nil {
return fmt.Errorf("Failed to mount %q: %w", wimName, err)
}
defer func() {
if !success {
_ = shared.RunCommand(r.ctx, nil, nil, "wimlib-imagex", "unmount", wimPath)
}
}()
logger.Info("Modifying")
// Create registry entries and copy files
err = r.InjectDrivers(wimPath, driverPath)
if err != nil {
return fmt.Errorf("Failed to inject drivers: %w", err)
}
logger.Info("Unmounting")
err = shared.RunCommand(r.ctx, nil, nil, "wimlib-imagex", "unmount", wimPath, "--commit")
if err != nil {
return fmt.Errorf("Failed to unmount WIM image %q: %w", wimName, err)
}
success = true
return nil
}
|