1
0
mirror of https://github.com/lxc/distrobuilder.git synced 2026-02-05 06:45:19 +01:00

Fix golang-ci reported issues

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
Stéphane Graber
2025-04-02 14:28:44 -04:00
parent 8d36e37670
commit 57091a0e63
49 changed files with 160 additions and 135 deletions

View File

@@ -32,17 +32,17 @@ func getOverlay(logger *logrus.Logger, cacheDir, sourceDir string) (func(), stri
overlayDir := filepath.Join(cacheDir, "overlay")
workDir := filepath.Join(cacheDir, "work")
err := os.Mkdir(upperDir, 0755)
err := os.Mkdir(upperDir, 0o755)
if err != nil {
return nil, "", fmt.Errorf("Failed to create directory %q: %w", upperDir, err)
}
err = os.Mkdir(overlayDir, 0755)
err = os.Mkdir(overlayDir, 0o755)
if err != nil {
return nil, "", fmt.Errorf("Failed to create directory %q: %w", overlayDir, err)
}
err = os.Mkdir(workDir, 0755)
err = os.Mkdir(workDir, 0o755)
if err != nil {
return nil, "", fmt.Errorf("Failed to create directory %q: %w", workDir, err)
}

View File

@@ -277,14 +277,14 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
// Clean up cache directory before doing anything
c.cleanupCacheDirectory()
err := os.MkdirAll(c.flagCacheDir, 0755)
err := os.MkdirAll(c.flagCacheDir, 0o755)
if err != nil {
return fmt.Errorf("Failed creating cache directory: %w", err)
}
if len(args) > 1 {
// Create and set target directory if provided
err := os.MkdirAll(args[1], 0755)
err := os.MkdirAll(args[1], 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", args[1], err)
}
@@ -305,7 +305,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
}
// Create source directory if it doesn't exist
err = os.MkdirAll(c.sourceDir, 0755)
err = os.MkdirAll(c.sourceDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", c.sourceDir, err)
}
@@ -318,7 +318,7 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
// Create cache directory if we also plan on creating LXC or Incus images
if !isRunningBuildDir {
err = os.MkdirAll(c.flagCacheDir, 0755)
err = os.MkdirAll(c.flagCacheDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", c.flagCacheDir, err)
}
@@ -455,7 +455,7 @@ func (c *cmdGlobal) preRunPack(cmd *cobra.Command, args []string) error {
// Clean up cache directory before doing anything
c.cleanupCacheDirectory()
err := os.MkdirAll(c.flagCacheDir, 0755)
err := os.MkdirAll(c.flagCacheDir, 0o755)
if err != nil {
return fmt.Errorf("Failed creating cache directory: %w", err)
}
@@ -622,7 +622,7 @@ func addSystemdGenerator() error {
return nil
}
err := os.MkdirAll("/etc/systemd/system-generators", 0755)
err := os.MkdirAll("/etc/systemd/system-generators", 0o755)
if err != nil {
return fmt.Errorf("Failed creating directory: %w", err)
}
@@ -632,7 +632,7 @@ func addSystemdGenerator() error {
return fmt.Errorf("Failed reading lxc.generator: %w", err)
}
err = os.WriteFile("/etc/systemd/system-generators/lxc", content, 0755)
err = os.WriteFile("/etc/systemd/system-generators/lxc", content, 0o755)
if err != nil {
return fmt.Errorf("Failed creating system generator: %w", err)
}

View File

@@ -315,7 +315,7 @@ func (c *cmdIncus) run(cmd *cobra.Command, args []string, overlayDir string) err
if c.flagVM {
vmDir = filepath.Join(c.global.flagCacheDir, "vm")
err := os.Mkdir(vmDir, 0755)
err := os.Mkdir(vmDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", vmDir, err)
}

View File

@@ -141,7 +141,7 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
success := false
err = os.Mkdir(c.global.flagCacheDir, 0755)
err = os.Mkdir(c.global.flagCacheDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", c.global.flagCacheDir, err)
}
@@ -155,7 +155,7 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
c.global.sourceDir = filepath.Join(c.global.flagCacheDir, "source")
// Create source path
err = os.MkdirAll(c.global.sourceDir, 0755)
err = os.MkdirAll(c.global.sourceDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", c.global.sourceDir, err)
}
@@ -177,7 +177,7 @@ func (c *cmdRepackWindows) preRun(cmd *cobra.Command, args []string) error {
driverPath := filepath.Join(c.global.flagCacheDir, "drivers")
if !incus.PathExists(driverPath) {
err := os.MkdirAll(driverPath, 0755)
err := os.MkdirAll(driverPath, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", driverPath, err)
}
@@ -215,7 +215,7 @@ func (c *cmdRepackWindows) checkVirtioISOPath() (err error) {
// Download vioscsi driver
virtioURL := "https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/" + c.defaultDrivers
err = os.MkdirAll(filepath.Dir(virtioISOPath), 0755)
err = os.MkdirAll(filepath.Dir(virtioISOPath), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(virtioISOPath), err)
}
@@ -322,9 +322,12 @@ func (c *cmdRepackWindows) run(cmd *cobra.Command, args []string, overlayDir str
version := strings.Split(stdout.String(), "\n")[0]
genArgs := []string{"-l", "-iso-level", "4", "-no-emul-boot",
genArgs := []string{
"-l", "-iso-level", "4", "-no-emul-boot",
"-b", "boot/etfsboot.com", "-boot-load-seg", "0",
"-boot-load-size", "8", "-eltorito-alt-boot"}
"-boot-load-size", "8", "-eltorito-alt-boot",
}
if strings.HasPrefix(version, "mkisofs") {
genArgs = append(genArgs,
"-eltorito-platform", "efi", "-no-emul-boot",
@@ -349,7 +352,6 @@ func (c *cmdRepackWindows) run(cmd *cobra.Command, args []string, overlayDir str
return os.Stderr.Write(b)
})),
nil, nil, software, genArgs...)
if err != nil {
return fmt.Errorf("Failed to generate ISO: %w", err)
}

View File

@@ -99,7 +99,7 @@ func (v *vm) createEmptyDiskImage() error {
defer f.Close()
err = f.Chmod(0600)
err = f.Chmod(0o600)
if err != nil {
return fmt.Errorf("Failed to chmod %s: %w", v.imageFile, err)
}
@@ -117,7 +117,8 @@ func (v *vm) createPartitions(args ...[]string) error {
args = [][]string{
{"--zap-all"},
{"--new=1::+100M", "-t 1:EF00"},
{"--new=2::", "-t 2:8300"}}
{"--new=2::", "-t 2:8300"},
}
}
for _, cmd := range args {
@@ -220,7 +221,7 @@ func (v *vm) mountImage() (err error) {
dev := unix.Mkdev(uint32(major), uint32(minor))
err = unix.Mknod(v.getUEFIDevFile(), unix.S_IFBLK|0644, int(dev))
err = unix.Mknod(v.getUEFIDevFile(), unix.S_IFBLK|0o644, int(dev))
if err != nil {
err = fmt.Errorf("Failed to create block device %q: %w", v.getUEFIDevFile(), err)
return
@@ -236,7 +237,7 @@ func (v *vm) mountImage() (err error) {
dev := unix.Mkdev(uint32(major), uint32(minor))
err = unix.Mknod(v.getRootfsDevFile(), unix.S_IFBLK|0644, int(dev))
err = unix.Mknod(v.getRootfsDevFile(), unix.S_IFBLK|0o644, int(dev))
if err != nil {
err = fmt.Errorf("Failed to create block device %q: %w", v.getRootfsDevFile(), err)
return
@@ -338,7 +339,7 @@ func (v *vm) mountUEFIPartition() error {
v.bootfsDir = filepath.Join(v.rootfsDir, "boot", "efi")
err := os.MkdirAll(v.bootfsDir, 0755)
err := os.MkdirAll(v.bootfsDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", v.bootfsDir, err)
}

View File

@@ -48,7 +48,7 @@ func (g *cloudInit) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLX
path := filepath.Join(g.sourceDir, "/etc/cloud")
if !incus.PathExists(path) {
err := os.MkdirAll(path, 0755)
err := os.MkdirAll(path, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", path, err)
}
@@ -71,7 +71,7 @@ func (g *cloudInit) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLX
func (g *cloudInit) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIncus) error {
templateDir := filepath.Join(g.cacheDir, "templates")
err := os.MkdirAll(templateDir, 0755)
err := os.MkdirAll(templateDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", templateDir, err)
}

View File

@@ -29,10 +29,10 @@ func TestCloudInitGeneratorRunLXC(t *testing.T) {
require.NoError(t, err)
// Prepare rootfs
err = os.MkdirAll(filepath.Join(rootfsDir, "etc", "runlevels"), 0755)
err = os.MkdirAll(filepath.Join(rootfsDir, "etc", "runlevels"), 0o755)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(rootfsDir, "etc", "cloud"), 0755)
err = os.MkdirAll(filepath.Join(rootfsDir, "etc", "cloud"), 0o755)
require.NoError(t, err)
for _, f := range []string{"cloud-init-local", "cloud-config", "cloud-init", "cloud-final"} {

View File

@@ -154,7 +154,6 @@ func (g *copy) copyDir(srcPath, destPath string, defFile shared.DefinitionFile)
return nil
})
if err != nil {
return fmt.Errorf("Failed to walk file tree of %q: %w", srcPath, err)
}
@@ -211,7 +210,7 @@ func (g *copy) copyFile(src, dest string, defFile shared.DefinitionFile) error {
out, err := os.Create(dest)
if err != nil {
if os.IsExist(err) {
out, err = os.OpenFile(dest, os.O_WRONLY, 0644)
out, err = os.OpenFile(dest, os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", dest, err)
}

View File

@@ -49,7 +49,7 @@ func (g *dump) run(content string) error {
path := filepath.Join(g.sourceDir, g.defFile.Path)
// Create any missing directory
err := os.MkdirAll(filepath.Dir(path), 0755)
err := os.MkdirAll(filepath.Dir(path), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(path), err)
}

View File

@@ -109,7 +109,8 @@ func TestDumpGeneratorRunIncus(t *testing.T) {
err = generator.RunIncus(nil, shared.DefinitionTargetIncus{
VM: shared.DefinitionTargetIncusVM{
Filesystem: "ext4",
}})
},
})
require.NoError(t, err)
require.FileExists(t, filepath.Join(rootfsDir, "hello", "world"))
@@ -136,7 +137,8 @@ func TestDumpGeneratorRunIncus(t *testing.T) {
err = generator.RunIncus(nil, shared.DefinitionTargetIncus{
VM: shared.DefinitionTargetIncusVM{
Filesystem: "ext4",
}})
},
})
require.NoError(t, err)
require.FileExists(t, filepath.Join(rootfsDir, "hello", "world"))

View File

@@ -44,7 +44,7 @@ LABEL=UEFI /boot/efi vfat defaults 0 0
options = fmt.Sprintf("%s,subvol=@", options)
}
_, err = f.WriteString(fmt.Sprintf(content, fs, options))
_, err = fmt.Fprintf(f, content, fs, options)
if err != nil {
return fmt.Errorf("Failed to write string to file %q: %w", filepath.Join(g.sourceDir, "etc/fstab"), err)
}

View File

@@ -14,7 +14,7 @@ import (
func setup(t *testing.T, cacheDir string) {
// Create rootfs directory
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0o755)
require.NoError(t, err)
}

View File

@@ -55,7 +55,7 @@ func (g *hostname) RunIncus(img *image.IncusImage, target shared.DefinitionTarge
templateDir := filepath.Join(g.cacheDir, "templates")
err := os.MkdirAll(templateDir, 0755)
err := os.MkdirAll(templateDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", templateDir, err)
}

View File

@@ -34,7 +34,7 @@ func TestHostnameGeneratorRunLXC(t *testing.T) {
image := image.NewLXCImage(context.TODO(), cacheDir, "", cacheDir, definition)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0o755)
require.NoError(t, err)
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hostname"), "hostname")
@@ -67,7 +67,7 @@ func TestHostnameGeneratorRunIncus(t *testing.T) {
image := image.NewIncusImage(context.TODO(), cacheDir, "", cacheDir, definition)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0o755)
require.NoError(t, err)
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hostname"), "hostname")

View File

@@ -31,7 +31,7 @@ func (g *hosts) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLXC) e
}
// Replace hostname with placeholder
content = []byte(strings.Replace(string(content), "distrobuilder", "LXC_NAME", -1))
content = []byte(strings.ReplaceAll(string(content), "distrobuilder", "LXC_NAME"))
// Add a new line if needed
if !strings.Contains(string(content), "LXC_NAME") {
@@ -70,7 +70,7 @@ func (g *hosts) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIn
templateDir := filepath.Join(g.cacheDir, "templates")
// Create templates path
err := os.MkdirAll(templateDir, 0755)
err := os.MkdirAll(templateDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", templateDir, err)
}
@@ -82,7 +82,7 @@ func (g *hosts) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIn
}
// Replace hostname with placeholder
content = []byte(strings.Replace(string(content), "distrobuilder", "{{ container.name }}", -1))
content = []byte(strings.ReplaceAll(string(content), "distrobuilder", "{{ container.name }}"))
// Add a new line if needed
if !strings.Contains(string(content), "{{ container.name }}") {
@@ -90,7 +90,7 @@ func (g *hosts) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIn
}
// Write the template
err = os.WriteFile(filepath.Join(templateDir, "hosts.tpl"), content, 0644)
err = os.WriteFile(filepath.Join(templateDir, "hosts.tpl"), content, 0o644)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(templateDir, "hosts.tpl"), err)
}

View File

@@ -34,7 +34,7 @@ func TestHostsGeneratorRunLXC(t *testing.T) {
image := image.NewLXCImage(context.TODO(), cacheDir, "", cacheDir, definition)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0o755)
require.NoError(t, err)
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
@@ -69,7 +69,7 @@ func TestHostsGeneratorRunIncus(t *testing.T) {
image := image.NewIncusImage(context.TODO(), cacheDir, "", cacheDir, definition)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "etc"), 0o755)
require.NoError(t, err)
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),

View File

@@ -146,14 +146,14 @@ StartLimitBurst=10
path := filepath.Join(g.sourceDir, systemdPath, "system", "incus-agent.service")
err := os.WriteFile(path, []byte(incusAgentServiceUnit), 0644)
err := os.WriteFile(path, []byte(incusAgentServiceUnit), 0o644)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", path, err)
}
path = filepath.Join(g.sourceDir, systemdPath, "incus-agent-setup")
err = os.WriteFile(path, []byte(incusAgentSetupScript), 0755)
err = os.WriteFile(path, []byte(incusAgentSetupScript), 0o755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", path, err)
}
@@ -166,7 +166,7 @@ StartLimitBurst=10
incusAgentRules := `SYMLINK=="virtio-ports/org.linuxcontainers.incus", TAG+="systemd", ENV{SYSTEMD_WANTS}+="incus-agent.service"
`
err = os.WriteFile(filepath.Join(g.sourceDir, udevPath, "99-incus-agent.rules"), []byte(incusAgentRules), 0400)
err = os.WriteFile(filepath.Join(g.sourceDir, udevPath, "99-incus-agent.rules"), []byte(incusAgentRules), 0o400)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, udevPath, "99-incus-agent.rules"), err)
}
@@ -192,7 +192,7 @@ depend() {
}
`
err := os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/incus-agent"), []byte(incusAgentScript), 0755)
err := os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/incus-agent"), []byte(incusAgentScript), 0o755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, "/etc/init.d/incus-agent"), err)
}
@@ -209,7 +209,7 @@ command=/usr/local/bin/incus-agent-setup
required_dirs=/dev/virtio-ports/
`
err = os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/incus-agent-setup"), []byte(incusConfigShareMountScript), 0755)
err = os.WriteFile(filepath.Join(g.sourceDir, "/etc/init.d/incus-agent-setup"), []byte(incusConfigShareMountScript), 0o755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", filepath.Join(g.sourceDir, "/etc/init.d/incus-agent-setup"), err)
}
@@ -221,7 +221,7 @@ required_dirs=/dev/virtio-ports/
path := filepath.Join(g.sourceDir, "/usr/local/bin", "incus-agent-setup")
err = os.WriteFile(path, []byte(incusAgentSetupScript), 0755)
err = os.WriteFile(path, []byte(incusAgentSetupScript), 0o755)
if err != nil {
return fmt.Errorf("Failed to write file %q: %w", path, err)
}

View File

@@ -27,7 +27,7 @@ func (g *template) RunLXC(img *image.LXCImage, target shared.DefinitionTargetLXC
func (g *template) RunIncus(img *image.IncusImage, target shared.DefinitionTargetIncus) error {
templateDir := filepath.Join(g.cacheDir, "templates")
err := os.MkdirAll(templateDir, 0755)
err := os.MkdirAll(templateDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", templateDir, err)
}

View File

@@ -39,7 +39,7 @@ func TestTemplateGeneratorRunIncus(t *testing.T) {
image := image.NewIncusImage(context.TODO(), cacheDir, "", cacheDir, definition)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "root"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs", "root"), 0o755)
require.NoError(t, err)
createTestFile(t, filepath.Join(cacheDir, "rootfs", "root", "template"), "--test--")

View File

@@ -26,7 +26,8 @@ type IncusImage struct {
// NewIncusImage returns an IncusImage.
func NewIncusImage(ctx context.Context, sourceDir, targetDir, cacheDir string,
definition shared.Definition) *IncusImage {
definition shared.Definition,
) *IncusImage {
return &IncusImage{
sourceDir,
targetDir,

View File

@@ -38,10 +38,10 @@ func setupIncus(t *testing.T) (*IncusImage, string) {
cacheDir, err := os.MkdirTemp(os.TempDir(), "distrobuilder-test-")
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0o755)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(cacheDir, "templates"), 0755)
err = os.MkdirAll(filepath.Join(cacheDir, "templates"), 0o755)
require.NoError(t, err)
image := NewIncusImage(context.TODO(), cacheDir, "", cacheDir, incusDef)

View File

@@ -35,7 +35,7 @@ func NewLXCImage(ctx context.Context, sourceDir, targetDir, cacheDir string, def
}
// create metadata directory
err := os.MkdirAll(filepath.Join(cacheDir, "metadata"), 0755)
err := os.MkdirAll(filepath.Join(cacheDir, "metadata"), 0o755)
if err != nil {
return nil
}
@@ -48,14 +48,14 @@ func (l *LXCImage) AddTemplate(path string) error {
metaDir := filepath.Join(l.cacheDir, "metadata")
file, err := os.OpenFile(filepath.Join(metaDir, "templates"),
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", filepath.Join(metaDir, "templates"), err)
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("%v\n", path))
_, err = fmt.Fprintf(file, "%v\n", path)
if err != nil {
return fmt.Errorf("Failed to write to template file: %w", err)
}
@@ -211,7 +211,7 @@ func (l *LXCImage) writeMetadata(filename, content string, appendContent bool) e
// Open the file either in append or create mode
if appendContent {
file, err = os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
file, err = os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", filename, err)
}

View File

@@ -144,7 +144,7 @@ func TestLXCBuild(t *testing.T) {
image, cacheDir := setupLXC()
defer os.RemoveAll(cacheDir)
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0755)
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs"), 0o755)
require.NoError(t, err)
err = image.Build("xz")
@@ -209,7 +209,7 @@ func TestLXCCreateMetadataBasic(t *testing.T) {
"",
func(l LXCImage) *LXCImage {
// Create /dev and device file.
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs", "dev"), 0755)
err := os.MkdirAll(filepath.Join(cacheDir, "rootfs", "dev"), 0o755)
require.NoError(t, err)
err = unix.Mknod(filepath.Join(cacheDir, "rootfs", "dev", "null"), unix.S_IFCHR, 0)
require.NoError(t, err)

View File

@@ -67,13 +67,13 @@ func (m *anise) manageRepository(repoAction shared.DefinitionPackagesRepository)
}
if !incus.PathExists(filepath.Dir(targetFile)) {
err := os.MkdirAll(filepath.Dir(targetFile), 0755)
err := os.MkdirAll(filepath.Dir(targetFile), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(targetFile), err)
}
}
f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", targetFile, err)
}

View File

@@ -63,7 +63,7 @@ func (m *apk) appendRepositoryURL(repoAction shared.DefinitionPackagesRepository
repoFile := "/etc/apk/repositories"
f, err := os.OpenFile(repoFile, os.O_WRONLY|os.O_APPEND, 0644)
f, err := os.OpenFile(repoFile, os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("Failed to open %q: %w", repoFile, err)
}
@@ -88,7 +88,7 @@ func (m *apk) writeKeyFile(repoAction shared.DefinitionPackagesRepository) error
}
keyFile := "/etc/apk/keys/" + repoAction.Name
f, err := os.OpenFile(keyFile, os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(keyFile, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("Failed to open %q: %w", keyFile, err)
}

View File

@@ -64,13 +64,13 @@ func (m *apt) manageRepository(repoAction shared.DefinitionPackagesRepository) e
}
if !incus.PathExists(filepath.Dir(targetFile)) {
err := os.MkdirAll(filepath.Dir(targetFile), 0755)
err := os.MkdirAll(filepath.Dir(targetFile), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(targetFile), err)
}
}
f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_RDWR, 0644)
f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", targetFile, err)
}

View File

@@ -42,9 +42,10 @@ func (m *equo) load() error {
}
func (m *equo) manageRepository(repoAction shared.DefinitionPackagesRepository) error {
if repoAction.Type == "" || repoAction.Type == "equo" {
switch repoAction.Type {
case "", "equo":
return m.equoRepoCaller(repoAction)
} else if repoAction.Type == "enman" {
case "enman":
return m.enmanRepoCaller(repoAction)
}

View File

@@ -142,9 +142,10 @@ func (m *Manager) ManagePackages(imageTarget shared.ImageTarget) error {
}
for _, set := range optimizePackageSets(validSets) {
if set.Action == "install" {
switch set.Action {
case "install":
err = m.mgr.install(set.Packages, set.Flags)
} else if set.Action == "remove" {
case "remove":
err = m.mgr.remove(set.Packages, set.Flags)
}

View File

@@ -38,7 +38,7 @@ func (m *opkg) load() error {
m.hooks = managerHooks{
preRefresh: func() error {
return os.MkdirAll("/var/lock", 0755)
return os.MkdirAll("/var/lock", 0o755)
},
}

View File

@@ -88,7 +88,7 @@ func yumManageRepository(repoAction shared.DefinitionPackagesRepository) error {
}
if !incus.PathExists(filepath.Dir(targetFile)) {
err := os.MkdirAll(filepath.Dir(targetFile), 0755)
err := os.MkdirAll(filepath.Dir(targetFile), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(targetFile), err)
}

View File

@@ -26,7 +26,7 @@ var ActiveChroots = make(map[string]func() error)
func setupMounts(rootfs string, mounts []ChrootMount) error {
// Create a temporary mount path
err := os.MkdirAll(filepath.Join(rootfs, ".distrobuilder"), 0700)
err := os.MkdirAll(filepath.Join(rootfs, ".distrobuilder"), 0o700)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Join(rootfs, ".distrobuilder"), err)
}
@@ -37,7 +37,7 @@ func setupMounts(rootfs string, mounts []ChrootMount) error {
// Create the target mountpoint
if mount.IsDir {
err := os.MkdirAll(tmpTarget, 0755)
err := os.MkdirAll(tmpTarget, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", tmpTarget, err)
}
@@ -110,19 +110,19 @@ func moveMounts(mounts []ChrootMount) error {
}
// Create parent paths if missing
err := os.MkdirAll(targetDir, 0755)
err := os.MkdirAll(targetDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", targetDir, err)
}
// Create target path
if mount.IsDir {
err = os.MkdirAll(target, 0755)
err = os.MkdirAll(target, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", target, err)
}
} else {
err := os.WriteFile(target, nil, 0644)
err := os.WriteFile(target, nil, 0o644)
if err != nil {
return fmt.Errorf("Failed to create file %q: %w", target, err)
}
@@ -242,7 +242,7 @@ func SetupChroot(rootfs string, definition Definition, m []ChrootMount) (func()
}
// Change permission for /dev/shm
err = unix.Chmod("/dev/shm", 01777)
err = unix.Chmod("/dev/shm", 0o1777)
if err != nil {
return nil, fmt.Errorf("Failed to chmod /dev/shm: %w", err)
}
@@ -276,9 +276,10 @@ func SetupChroot(rootfs string, definition Definition, m []ChrootMount) (func()
if len(envs.EnvVariables) > 0 {
imageTargets := ImageTargetUndefined | ImageTargetAll
if definition.Targets.Type == DefinitionFilterTypeContainer {
switch definition.Targets.Type {
case DefinitionFilterTypeContainer:
imageTargets |= ImageTargetContainer
} else if definition.Targets.Type == DefinitionFilterTypeVM {
case DefinitionFilterTypeVM:
imageTargets |= ImageTargetVM
}
@@ -308,7 +309,7 @@ func SetupChroot(rootfs string, definition Definition, m []ChrootMount) (func()
if incus.PathExists("/usr/sbin/") && !incus.PathExists("/usr/sbin/policy-rc.d") {
err = os.WriteFile("/usr/sbin/policy-rc.d", []byte(`#!/bin/sh
exit 101
`), 0755)
`), 0o755)
if err != nil {
return nil, err
}
@@ -369,7 +370,7 @@ exit 101
ActiveChroots[rootfs] = nil
return os.MkdirAll(devPath, 0755)
return os.MkdirAll(devPath, 0o755)
}
ActiveChroots[rootfs] = exitFunc
@@ -384,13 +385,13 @@ func populateDev() error {
Minor uint32
Mode uint32
}{
{"/dev/console", 5, 1, unix.S_IFCHR | 0640},
{"/dev/full", 1, 7, unix.S_IFCHR | 0666},
{"/dev/null", 1, 3, unix.S_IFCHR | 0666},
{"/dev/random", 1, 8, unix.S_IFCHR | 0666},
{"/dev/tty", 5, 0, unix.S_IFCHR | 0666},
{"/dev/urandom", 1, 9, unix.S_IFCHR | 0666},
{"/dev/zero", 1, 5, unix.S_IFCHR | 0666},
{"/dev/console", 5, 1, unix.S_IFCHR | 0o640},
{"/dev/full", 1, 7, unix.S_IFCHR | 0o666},
{"/dev/null", 1, 3, unix.S_IFCHR | 0o666},
{"/dev/random", 1, 8, unix.S_IFCHR | 0o666},
{"/dev/tty", 5, 0, unix.S_IFCHR | 0o666},
{"/dev/urandom", 1, 9, unix.S_IFCHR | 0o666},
{"/dev/zero", 1, 5, unix.S_IFCHR | 0o666},
}
for _, d := range devs {

View File

@@ -9,7 +9,6 @@ import (
"strings"
"time"
"github.com/lxc/incus/v6/shared/osarch"
incusArch "github.com/lxc/incus/v6/shared/osarch"
)
@@ -313,7 +312,7 @@ func (d *Definition) SetValue(key string, value string) error {
func (d *Definition) SetDefaults() {
// default to local arch
if d.Image.Architecture == "" {
localArch, _ := osarch.ArchitectureGetLocal()
localArch, _ := incusArch.ArchitectureGetLocal()
d.Image.Architecture = localArch
}

View File

@@ -252,7 +252,7 @@ func findMatchHelper(t *testing.T, filenames ...string) (dir string, actuals []s
actuals = make([]string, len(filenames))
for i, filename := range filenames {
filename = filepath.Join(dir, filename)
err = os.MkdirAll(filepath.Dir(filename), 0755)
err = os.MkdirAll(filepath.Dir(filename), 0o755)
if err != nil {
t.Fatal(err)
}

View File

@@ -144,7 +144,7 @@ func (s *alpineLinux) Run() error {
}
// Fix bad permissions in Alpine tarballs
err = os.Chmod(s.rootfsDir, 0755)
err = os.Chmod(s.rootfsDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to chmod %q: %w", s.rootfsDir, err)
}

View File

@@ -37,7 +37,7 @@ func (s *busybox) Run() error {
sourceDir := filepath.Join(s.cacheDir, "src")
err = os.MkdirAll(sourceDir, 0755)
err = os.MkdirAll(sourceDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", sourceDir, err)
}
@@ -91,7 +91,7 @@ mv ${source_dir}/busybox "${rootfs_dir}/bin/busybox"
s.logger.Debugf("Creating directory %q", path)
err = os.MkdirAll(filepath.Dir(path), 0755)
err = os.MkdirAll(filepath.Dir(path), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(path), err)
}
@@ -105,7 +105,7 @@ mv ${source_dir}/busybox "${rootfs_dir}/bin/busybox"
}
for _, path := range []string{"dev", "mnt", "proc", "root", "sys", "tmp"} {
err := os.Mkdir(filepath.Join(s.rootfsDir, path), 0755)
err := os.Mkdir(filepath.Join(s.rootfsDir, path), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Join(s.rootfsDir, path), err)
}

View File

@@ -49,7 +49,7 @@ func (s *common) init(ctx context.Context, logger *logrus.Logger, definition sha
func (s *common) getTargetDir() string {
dir := filepath.Join(s.sourcesDir, fmt.Sprintf("%s-%s-%s", s.definition.Image.Distribution, s.definition.Image.Release, s.definition.Image.ArchitectureMapped))
dir = strings.Replace(dir, " ", "", -1)
dir = strings.ReplaceAll(dir, " ", "")
dir = strings.ToLower(dir)
return dir
@@ -65,7 +65,7 @@ func (s *common) DownloadHash(def shared.DefinitionImage, file, checksum string,
destDir := s.getTargetDir()
err = os.MkdirAll(destDir, 0755)
err = os.MkdirAll(destDir, 0o755)
if err != nil {
return "", err
}
@@ -232,7 +232,7 @@ func (s *common) VerifyFile(signedFile, signatureFile string) (bool, error) {
// CreateGPGKeyring creates a new GPG keyring.
func (s *common) CreateGPGKeyring() (string, error) {
err := os.MkdirAll(s.getTargetDir(), 0700)
err := os.MkdirAll(s.getTargetDir(), 0o700)
if err != nil {
return "", err
}
@@ -242,7 +242,7 @@ func (s *common) CreateGPGKeyring() (string, error) {
return "", fmt.Errorf("Failed to create gpg directory: %w", err)
}
err = os.MkdirAll(gpgDir, 0700)
err = os.MkdirAll(gpgDir, 0o700)
if err != nil {
return "", err
}

View File

@@ -57,7 +57,7 @@ func (s *fedora) Run() error {
return fmt.Errorf("Failed to create OCI path: %q: %w", ociDir, err)
}
err = os.Mkdir(filepath.Join(ociDir, "image"), 0755)
err = os.Mkdir(filepath.Join(ociDir, "image"), 0o755)
if err != nil {
return fmt.Errorf("Failed to create OCI path: %q: %w", ociDir, err)
}
@@ -68,7 +68,7 @@ func (s *fedora) Run() error {
}
// Extract the image to a temporary path.
err = os.Mkdir(filepath.Join(ociDir, "content"), 0755)
err = os.Mkdir(filepath.Join(ociDir, "content"), 0o755)
if err != nil {
return fmt.Errorf("Failed to create OCI path: %q: %w", ociDir, err)
}

View File

@@ -16,13 +16,15 @@ type funtoo struct {
// Run downloads a Funtoo stage3 tarball.
func (s *funtoo) Run() error {
topLevelArch := s.definition.Image.ArchitectureMapped
if topLevelArch == "generic_32" {
switch topLevelArch {
case "generic_32":
topLevelArch = "x86-32bit"
} else if topLevelArch == "generic_64" {
case "generic_64":
topLevelArch = "x86-64bit"
} else if topLevelArch == "armv7a_vfpv3_hardfp" {
case "armv7a_vfpv3_hardfp":
topLevelArch = "arm-32bit"
} else if topLevelArch == "arm64_generic" {
case "arm64_generic":
topLevelArch = "arm-64bit"
}

View File

@@ -49,9 +49,10 @@ func (s *oraclelinux) Run() error {
latestUpdate = fmt.Sprintf("u%d", i)
if s.definition.Image.Release == "9" {
if s.architecture == "x86_64" {
switch s.architecture {
case "x86_64":
fname = fmt.Sprintf("OracleLinux-R9-U%d-%s-boot.iso", i, s.architecture)
} else if s.architecture == "aarch64" {
case "aarch64":
fname = fmt.Sprintf("OracleLinux-R9-U%d-%s-dvd.iso", i, s.architecture)
}
}
@@ -140,7 +141,7 @@ func (s *oraclelinux) unpackISO(latestUpdate, filePath, rootfsDir string) error
tempRootDir := filepath.Join(s.cacheDir, "rootfs")
for _, dir := range []string{isoDir, squashfsDir, roRootDir} {
err := os.MkdirAll(dir, 0755)
err := os.MkdirAll(dir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create %q: %w", dir, err)
}
@@ -374,11 +375,12 @@ EOF
func (s *oraclelinux) getISO(URL string, architecture string) (string, error) {
var re *regexp.Regexp
if architecture == "x86_64" {
switch architecture {
case "x86_64":
re = regexp.MustCompile(fmt.Sprintf("%s-boot(-\\d{8})?.iso", architecture))
} else if architecture == "aarch64" {
case "aarch64":
re = regexp.MustCompile(fmt.Sprintf("%s-boot-uek(-\\d{8})?.iso", architecture))
} else {
default:
return "", fmt.Errorf("Unsupported architecture %q", architecture)
}

View File

@@ -37,8 +37,10 @@ func (s *plamolinux) Run() error {
s.definition.Image.ArchitectureMapped, "plamo")
paths := []string{path.Join(mirrorPath, "00_base")}
ignoredPkgs := []string{"alsa_utils", "grub", "kernel", "lilo", "linux_firmware", "microcode_ctl",
"linux_firmwares", "cpufreqd", "cpufrequtils", "gpm", "ntp", "kmod", "kmscon"}
ignoredPkgs := []string{
"alsa_utils", "grub", "kernel", "lilo", "linux_firmware", "microcode_ctl",
"linux_firmwares", "cpufreqd", "cpufrequtils", "gpm", "ntp", "kmod", "kmscon",
}
if release < 7 {
paths = append(paths, path.Join(mirrorPath, "01_minimum"))

View File

@@ -112,7 +112,7 @@ func (c *commonRHEL) unpackISO(filePath, rootfsDir string, scriptRunner func(str
if incus.PathExists(packagesDir) && incus.PathExists(repodataDir) {
// Create cdrom repo for yum
err = os.MkdirAll(filepath.Join(tempRootDir, "mnt", "cdrom"), 0755)
err = os.MkdirAll(filepath.Join(tempRootDir, "mnt", "cdrom"), 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Join(tempRootDir, "mnt", "cdrom"), err)
}
@@ -241,7 +241,7 @@ func (c *commonRHEL) unpackRaw(filePath, rootfsDir string, scriptRunner func() e
roRootDir := filepath.Join(c.cacheDir, "rootfs.ro")
tempRootDir := filepath.Join(c.cacheDir, "rootfs")
err := os.MkdirAll(roRootDir, 0755)
err := os.MkdirAll(roRootDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", roRootDir, err)
}

View File

@@ -26,7 +26,7 @@ func (s *rpmbootstrap) yumordnf() (cmd string, err error) {
func (s *rpmbootstrap) repodirs() (dir string, err error) {
reposdir := path.Join(s.sourcesDir, "etc", "yum.repos.d")
err = os.MkdirAll(reposdir, 0755)
err = os.MkdirAll(reposdir, 0o755)
if err != nil {
return "", err
}
@@ -38,7 +38,7 @@ func (s *rpmbootstrap) repodirs() (dir string, err error) {
return "", err
}
err = os.WriteFile(path.Join(reposdir, distribution+".repo"), []byte(content), 0644)
err = os.WriteFile(path.Join(reposdir, distribution+".repo"), []byte(content), 0o644)
if err != nil {
return "", err
}
@@ -59,10 +59,12 @@ func (s *rpmbootstrap) Run() (err error) {
}
release := s.definition.Image.Release
args := []string{fmt.Sprintf("--installroot=%s", s.rootfsDir),
args := []string{
fmt.Sprintf("--installroot=%s", s.rootfsDir),
fmt.Sprintf("--releasever=%s", release),
fmt.Sprintf("--setopt=reposdir=%s", repodir),
"install", "-y"}
"install", "-y",
}
os.RemoveAll(s.rootfsDir)
earlyPackagesRemove := s.definition.GetEarlyPackages("remove")

View File

@@ -29,13 +29,14 @@ func (s *slackware) Run() error {
slackpkgPath := ""
// set mirror path based on architecture
if s.definition.Image.ArchitectureMapped == "i586" {
switch s.definition.Image.ArchitectureMapped {
case "i586":
mirrorPath = path.Join(u.Path, fmt.Sprintf("slackware-%s", s.definition.Image.Release), "slackware")
slackpkgPath = s.definition.Source.URL + fmt.Sprintf("slackware-%s", s.definition.Image.Release)
} else if s.definition.Image.ArchitectureMapped == "x86_64" {
case "x86_64":
mirrorPath = path.Join(u.Path, fmt.Sprintf("slackware64-%s", s.definition.Image.Release), "slackware64")
slackpkgPath = s.definition.Source.URL + fmt.Sprintf("slackware64-%s", s.definition.Image.Release)
} else {
default:
return fmt.Errorf("Invalid architecture: %s", s.definition.Image.Architecture)
}
@@ -150,7 +151,7 @@ func (s *slackware) downloadFiles(def shared.DefinitionImage, URL string, requir
pkgName := strings.Split(target, "-")[0]
twoPkgName := strings.Split(target, "-")[0] + "-" + strings.Split(target, "-")[1]
if !((slices.Contains(requiredPkgs, pkgName)) || (slices.Contains(requiredPkgs, twoPkgName))) {
if !slices.Contains(requiredPkgs, pkgName) && !slices.Contains(requiredPkgs, twoPkgName) {
continue
}

View File

@@ -110,7 +110,7 @@ func (s ubuntu) unpack(filePath, rootDir string) error {
return fmt.Errorf("Failed to remove directory %q: %w", rootDir, err)
}
err = os.MkdirAll(rootDir, 0755)
err = os.MkdirAll(rootDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", rootDir, err)
}

View File

@@ -320,8 +320,10 @@ func recvGPGKeys(ctx context.Context, gpgDir string, keyserver string, keys []st
}
func getEnvHttpProxy() (httpProxy string) {
for _, key := range []string{"http_proxy",
"HTTP_PROXY", "https_proxy", "HTTPS_PROXY"} {
for _, key := range []string{
"http_proxy",
"HTTP_PROXY", "https_proxy", "HTTPS_PROXY",
} {
if httpProxy = os.Getenv(key); httpProxy != "" {
return
}

View File

@@ -245,9 +245,11 @@ func TestRecvGPGKeys(t *testing.T) {
want bool
}{
{[]string{testdataKey1, testdataKey2, testdataKey3, testdataKey4, testdataKey5}, true},
{[]string{testdataKey1, testdataKey2, testdataKey3, testdataKey4, testdataKey5,
{[]string{
testdataKey1, testdataKey2, testdataKey3, testdataKey4, testdataKey5,
`-----BEGIN PGP PUBLIC KEY BLOCK-----
invalid public key`}, false},
invalid public key`,
}, false},
}
for _, tc := range tcs {

View File

@@ -90,7 +90,7 @@ func (r *RepackUtil) InjectDrivers(windowsRootPath string, driverPath string) er
sourceDir := filepath.Join(driverPath, driverName, r.windowsVersion, r.windowsArchitecture)
targetBaseDir := filepath.Join(dirs["filerepository"], driverInfo.PackageName)
if !incus.PathExists(targetBaseDir) {
err := os.MkdirAll(targetBaseDir, 0755)
err := os.MkdirAll(targetBaseDir, 0o755)
if err != nil {
logger.Error(err)
return err
@@ -238,7 +238,7 @@ func (r *RepackUtil) modifyWimIndex(wimFile string, index int, name string, driv
wimName := filepath.Base(wimFile)
logger := r.logger.WithFields(logrus.Fields{"wim": wimName, "idx": wimIndex + ":" + name})
if !incus.PathExists(wimPath) {
err := os.MkdirAll(wimPath, 0755)
err := os.MkdirAll(wimPath, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", wimPath, err)
}

View File

@@ -12,9 +12,12 @@ import (
var (
SupportedWindowsVersions = []string{
"w11", "w10", "w8", "w7", "2k19", "2k12", "2k16",
"2k22", "2k3", "2k8", "xp", "2k12r2", "2k8r2", "w8.1"}
"2k22", "2k3", "2k8", "xp", "2k12r2", "2k8r2", "w8.1",
}
SupportedWindowsArchitectures = []string{
"amd64", "ARM64", "x86"}
"amd64", "ARM64", "x86",
}
)
type WimInfo map[int]map[string]string

View File

@@ -23,10 +23,12 @@ func TestParseWimInfo(t *testing.T) {
8: "Windows 10 Pro Education",
9: "Windows 10 Pro Education N",
10: "Windows 10 Pro for Workstations",
11: "Windows 10 Pro N for Workstations"}},
11: "Windows 10 Pro N for Workstations",
}},
{"testdata/w10_boot_wim_info.txt", 2, map[int]string{
1: "Microsoft Windows PE (x64)",
2: "Microsoft Windows Setup (x64)"}},
2: "Microsoft Windows Setup (x64)",
}},
{"testdata/winpe_boot_wim_info.txt", 1, map[int]string{
1: "Microsoft Windows PE (amd64)",
}},