1
0

refactor: add volume list

This commit is contained in:
dump_stack() 2023-05-22 14:28:28 +00:00
parent 2eb91ffac9
commit 2fe3103603
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
3 changed files with 75 additions and 38 deletions

View File

@ -71,10 +71,8 @@ func ImagePath(sk config.Target) string {
return config.Dir("containers", sk.Distro.ID.String(), sk.Distro.Release) return config.Dir("containers", sk.Distro.ID.String(), sk.Distro.Release)
} }
type Volumes struct { type Volume struct {
LibModules string Src, Dest string
UsrSrc string
Boot string
} }
type Container struct { type Container struct {
@ -82,7 +80,7 @@ type Container struct {
timeout time.Duration timeout time.Duration
Volumes Volumes Volumes []Volume
// Additional arguments // Additional arguments
Args []string Args []string
@ -98,9 +96,20 @@ func New(name string, timeout time.Duration) (c Container, err error) {
c.name = name c.name = name
c.timeout = timeout c.timeout = timeout
c.Volumes.LibModules = config.Dir("volumes", name, "lib", "modules") c.Volumes = append(c.Volumes, Volume{
c.Volumes.UsrSrc = config.Dir("volumes", name, "usr", "src") Src: config.Dir("volumes", name, "lib", "modules"),
c.Volumes.Boot = config.Dir("volumes", name, "boot") Dest: "/lib/modules",
})
c.Volumes = append(c.Volumes, Volume{
Src: config.Dir("volumes", name, "usr", "src"),
Dest: "/usr/src",
})
c.Volumes = append(c.Volumes, Volume{
Src: config.Dir("volumes", name, "boot"),
Dest: "/boot",
})
return return
} }
@ -115,9 +124,20 @@ func NewFromKernelInfo(ki config.KernelInfo, timeout time.Duration) (
Str("container", c.name). Str("container", c.name).
Logger() Logger()
c.Volumes.LibModules = path.Dir(ki.ModulesPath) c.Volumes = append(c.Volumes, Volume{
c.Volumes.Boot = path.Dir(ki.KernelPath) Src: path.Dir(ki.ModulesPath),
c.Volumes.UsrSrc = filepath.Join(path.Dir(ki.KernelPath), "../usr/src") Dest: "/lib/modules",
})
c.Volumes = append(c.Volumes, Volume{
Src: filepath.Join(path.Dir(ki.KernelPath), "../usr/src"),
Dest: "/usr/src",
})
c.Volumes = append(c.Volumes, Volume{
Src: path.Dir(ki.KernelPath),
Dest: "/boot",
})
return return
} }
@ -168,15 +188,12 @@ func (c Container) Run(workdir string, command string) (output string, err error
if workdir != "" { if workdir != "" {
args = append(args, "-v", workdir+":/work") args = append(args, "-v", workdir+":/work")
} }
if c.Volumes.LibModules != "" {
args = append(args, "-v", c.Volumes.LibModules+":/lib/modules") for _, volume := range c.Volumes {
} mount := fmt.Sprintf("%s:%s", volume.Src, volume.Dest)
if c.Volumes.UsrSrc != "" { args = append(args, "-v", mount)
args = append(args, "-v", c.Volumes.UsrSrc+":/usr/src")
}
if c.Volumes.Boot != "" {
args = append(args, "-v", c.Volumes.Boot+":/boot")
} }
args = append(args, c.name, "bash", "-c") args = append(args, c.name, "bash", "-c")
if workdir != "" { if workdir != "" {
args = append(args, "cd /work && "+command) args = append(args, "cd /work && "+command)

View File

@ -341,12 +341,23 @@ func ContainerKernels(d container.Image, kcfg *config.KernelConfig) (err error)
return return
} }
func Volumes(km config.Target, pkgname string) (volumes container.Volumes) { func Volumes(km config.Target, pkgname string) (volumes []container.Volume) {
pkgdir := filepath.Join("volumes", km.DockerName(), pkgname) pkgdir := filepath.Join("volumes", km.DockerName(), pkgname)
volumes.LibModules = config.Dir(pkgdir, "/lib/modules") volumes = append(volumes, container.Volume{
volumes.UsrSrc = config.Dir(pkgdir, "/usr/src") Src: config.Dir(pkgdir, "/lib/modules"),
volumes.Boot = config.Dir(pkgdir, "/boot") Dest: "/lib/modules",
})
volumes = append(volumes, container.Volume{
Src: config.Dir(pkgdir, "/usr/src"),
Dest: "/usr/src",
})
volumes = append(volumes, container.Volume{
Src: config.Dir(pkgdir, "/boot"),
Dest: "/boot",
})
return return
} }

View File

@ -194,7 +194,12 @@ func installKernel(sk config.Target, pkgname string, force, headers bool) (err e
return return
} }
searchdir := c.Volumes.LibModules searchdir := ""
for _, volume := range c.Volumes {
if volume.Dest == "/lib/modules" {
searchdir = volume.Src
}
}
if sk.Distro.ID == distro.Debian { if sk.Distro.ID == distro.Debian {
// TODO We need some kind of API for that // TODO We need some kind of API for that
@ -223,12 +228,6 @@ func installKernel(sk config.Target, pkgname string, force, headers bool) (err e
c.Volumes = debian.Volumes(sk, pkgname) c.Volumes = debian.Volumes(sk, pkgname)
} }
volumes := c.Volumes
c.Volumes.LibModules = ""
c.Volumes.UsrSrc = ""
c.Volumes.Boot = ""
slog.Debug().Msgf("Installing kernel") slog.Debug().Msgf("Installing kernel")
var commands []string var commands []string
@ -275,9 +274,9 @@ func installKernel(sk config.Target, pkgname string, force, headers bool) (err e
cmd += fmt.Sprintf(" && %s", command) cmd += fmt.Sprintf(" && %s", command)
} }
c.Args = append(c.Args, "-v", volumes.LibModules+":/target/lib/modules") for i := range c.Volumes {
c.Args = append(c.Args, "-v", volumes.UsrSrc+":/target/usr/src") c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
c.Args = append(c.Args, "-v", volumes.Boot+":/target/boot") }
cmd += " && cp -r /boot /target/" cmd += " && cp -r /boot /target/"
cmd += " && cp -r /lib/modules /target/lib/" cmd += " && cp -r /lib/modules /target/lib/"
@ -413,12 +412,22 @@ func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig,
return return
} }
moddirs, err := ioutil.ReadDir(c.Volumes.LibModules) var libmodules, boot string
for _, volume := range c.Volumes {
switch volume.Dest {
case "/lib/modules":
libmodules = volume.Dest
case "/boot":
boot = volume.Dest
}
}
moddirs, err := ioutil.ReadDir(libmodules)
if err != nil { if err != nil {
return return
} }
bootfiles, err := ioutil.ReadDir(c.Volumes.Boot) bootfiles, err := ioutil.ReadDir(boot)
if err != nil { if err != nil {
return return
} }
@ -445,9 +454,9 @@ func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig,
KernelRelease: krel.Name(), KernelRelease: krel.Name(),
ContainerName: dii.Name, ContainerName: dii.Name,
KernelPath: c.Volumes.Boot + "/" + kernelFile, KernelPath: filepath.Join(boot, kernelFile),
InitrdPath: c.Volumes.Boot + "/" + initrdFile, InitrdPath: filepath.Join(boot, initrdFile),
ModulesPath: c.Volumes.LibModules + "/" + krel.Name(), ModulesPath: filepath.Join(libmodules, krel.Name()),
RootFS: rootfs, RootFS: rootfs,
} }