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

View File

@ -341,12 +341,23 @@ func ContainerKernels(d container.Image, kcfg *config.KernelConfig) (err error)
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)
volumes.LibModules = config.Dir(pkgdir, "/lib/modules")
volumes.UsrSrc = config.Dir(pkgdir, "/usr/src")
volumes.Boot = config.Dir(pkgdir, "/boot")
volumes = append(volumes, container.Volume{
Src: config.Dir(pkgdir, "/lib/modules"),
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
}

View File

@ -194,7 +194,12 @@ func installKernel(sk config.Target, pkgname string, force, headers bool) (err e
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 {
// 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)
}
volumes := c.Volumes
c.Volumes.LibModules = ""
c.Volumes.UsrSrc = ""
c.Volumes.Boot = ""
slog.Debug().Msgf("Installing kernel")
var commands []string
@ -275,9 +274,9 @@ func installKernel(sk config.Target, pkgname string, force, headers bool) (err e
cmd += fmt.Sprintf(" && %s", command)
}
c.Args = append(c.Args, "-v", volumes.LibModules+":/target/lib/modules")
c.Args = append(c.Args, "-v", volumes.UsrSrc+":/target/usr/src")
c.Args = append(c.Args, "-v", volumes.Boot+":/target/boot")
for i := range c.Volumes {
c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
}
cmd += " && cp -r /boot /target/"
cmd += " && cp -r /lib/modules /target/lib/"
@ -413,12 +412,22 @@ func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig,
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 {
return
}
bootfiles, err := ioutil.ReadDir(c.Volumes.Boot)
bootfiles, err := ioutil.ReadDir(boot)
if err != nil {
return
}
@ -445,9 +454,9 @@ func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig,
KernelRelease: krel.Name(),
ContainerName: dii.Name,
KernelPath: c.Volumes.Boot + "/" + kernelFile,
InitrdPath: c.Volumes.Boot + "/" + initrdFile,
ModulesPath: c.Volumes.LibModules + "/" + krel.Name(),
KernelPath: filepath.Join(boot, kernelFile),
InitrdPath: filepath.Join(boot, initrdFile),
ModulesPath: filepath.Join(libmodules, krel.Name()),
RootFS: rootfs,
}