1
0

feat: add kernel install to distro interface

This commit is contained in:
2023-05-23 22:36:46 +00:00
parent daaef89050
commit e2d66db16f
7 changed files with 143 additions and 137 deletions

View File

@ -158,3 +158,48 @@ func (centos CentOS) runs() (commands []string) {
return
}
func (centos CentOS) Install(pkgname string, headers bool) (err error) {
var headerspkg string
if headers {
headerspkg = strings.Replace(pkgname, "kernel", "kernel-devel", -1)
}
var commands []string
cmdf := func(f string, s ...interface{}) {
commands = append(commands, fmt.Sprintf(f, s...))
}
cmdf("yum -y install %s %s", pkgname, headerspkg)
version := strings.Replace(pkgname, "kernel-", "", -1)
if centos.release <= "7" {
cmdf("dracut -v --add-drivers 'e1000 ext4' -f "+
"/boot/initramfs-%s.img %s", version, version)
} else {
cmdf("dracut -v --add-drivers 'ata_piix libata' "+
"--force-drivers 'e1000 ext4 sd_mod' -f "+
"/boot/initramfs-%s.img %s", version, version)
}
cmdf("cp -r /boot /target/")
cmdf("cp -r /lib/modules /target/lib/")
cmdf("cp -r /usr/src /target/usr/")
c, err := container.New(centos.Distro())
if err != nil {
return
}
for i := range c.Volumes {
c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
}
_, err = c.Run("", commands)
if err != nil {
return
}
return
}

View File

@ -348,8 +348,13 @@ func (d Debian) Kernels() (kernels []distro.KernelInfo, err error) {
return
}
func Volumes(km config.Target, pkgname string) (volumes []container.Volume) {
pkgdir := filepath.Join("volumes", km.DockerName(), pkgname)
func (d Debian) volumes(pkgname string) (volumes []container.Volume) {
c, err := container.New(d.Distro())
if err != nil {
return
}
pkgdir := filepath.Join("volumes", c.Name(), pkgname)
volumes = append(volumes, container.Volume{
Src: config.Dir(pkgdir, "/lib/modules"),
@ -369,7 +374,13 @@ func Volumes(km config.Target, pkgname string) (volumes []container.Volume) {
return
}
func Install(km config.Target, pkgname string, headers bool) (cmds []string, err error) {
func (d Debian) Install(pkgname string, headers bool) (err error) {
defer func() {
if err != nil {
d.cleanup(pkgname)
}
}()
dk, err := getCachedKernel(pkgname + ".deb")
if err != nil {
return
@ -382,9 +393,11 @@ func Install(km config.Target, pkgname string, headers bool) (cmds []string, err
pkgs = []snapshot.Package{dk.Image}
}
var cmds []string
for _, pkg := range pkgs {
found, newurl := cache.PackageURL(
km.Distro.ID,
distro.Debian,
pkg.Deb.URL,
)
if found {
@ -402,15 +415,39 @@ func Install(km config.Target, pkgname string, headers bool) (cmds []string, err
cmds = append(cmds, "dpkg -i ./*.deb")
c, err := container.New(d.Distro())
if err != nil {
return
}
c.Volumes = d.volumes(pkgname)
for i := range c.Volumes {
c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
}
cmds = append(cmds, "cp -r /boot /target/")
cmds = append(cmds, "cp -r /lib/modules /target/lib/")
cmds = append(cmds, "cp -r /usr/src /target/usr/")
_, err = c.Run("", cmds)
if err != nil {
return
}
return
}
func Cleanup(km config.Target, pkgname string) {
pkgdir := config.Dir(filepath.Join("volumes", km.DockerName(), pkgname))
func (d Debian) cleanup(pkgname string) {
c, err := container.New(d.Distro())
if err != nil {
return
}
pkgdir := config.Dir(filepath.Join("volumes", c.Name(), pkgname))
log.Debug().Msgf("cleanup %s", pkgdir)
err := os.RemoveAll(pkgdir)
err = os.RemoveAll(pkgdir)
if err != nil {
log.Warn().Err(err).Msg("cleanup")
}

View File

@ -1,6 +1,7 @@
package distro
import (
"errors"
"sync"
)
@ -11,6 +12,7 @@ type distribution interface {
Distro() Distro
Equal(Distro) bool
Packages() (packages []string, err error)
Install(pkg string, headers bool) (err error)
Kernels() (kernels []KernelInfo, err error)
}
@ -54,6 +56,17 @@ func (d Distro) Packages() (packages []string, err error) {
return
}
func (d Distro) Install(pkg string, headers bool) (err error) {
for _, dd := range distros {
if !dd.Equal(d) {
continue
}
return dd.Install(pkg, headers)
}
return errors.New("not found")
}
func (d Distro) Kernels() (kernels []KernelInfo, err error) {
for _, dd := range distros {
if dd.Equal(d) {

View File

@ -96,7 +96,7 @@ func (ol OracleLinux) runs() (commands []string) {
return
}
func Install(km config.Target, pkgname string, headers bool) (commands []string, err error) {
func (ol OracleLinux) Install(pkgname string, headers bool) (err error) {
var headerspkg string
if headers {
if strings.Contains(pkgname, "uek") {
@ -108,6 +108,7 @@ func Install(km config.Target, pkgname string, headers bool) (commands []string,
}
}
var commands []string
cmdf := func(f string, s ...interface{}) {
commands = append(commands, fmt.Sprintf(f, s...))
}
@ -121,7 +122,7 @@ func Install(km config.Target, pkgname string, headers bool) (commands []string,
version = strings.Replace(pkgname, "kernel-", "", -1)
}
if km.Distro.Release <= "7" {
if ol.release <= "7" {
cmdf("dracut -v --add-drivers 'e1000 ext4' -f "+
"/boot/initramfs-%s.img %s", version, version)
} else {
@ -130,9 +131,23 @@ func Install(km config.Target, pkgname string, headers bool) (commands []string,
"/boot/initramfs-%s.img %s", version, version)
}
return
}
cmdf("cp -r /boot /target/")
cmdf("cp -r /lib/modules /target/lib/")
cmdf("cp -r /usr/src /target/usr/")
c, err := container.New(ol.Distro())
if err != nil {
return
}
for i := range c.Volumes {
c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
}
_, err = c.Run("", commands)
if err != nil {
return
}
func Cleanup(km config.Target, pkgname string) {
return
}

View File

@ -122,22 +122,35 @@ func (u Ubuntu) runs() (commands []string) {
return
}
func Install(km config.Target, pkgname string, headers bool) (commands []string, err error) {
func (u Ubuntu) Install(pkgname string, headers bool) (err error) {
var headerspkg string
if headers {
headerspkg = strings.Replace(pkgname, "image", "headers", -1)
}
var commands []string
cmdf := func(f string, s ...interface{}) {
commands = append(commands, fmt.Sprintf(f, s...))
}
cmdf("apt-get install -y %s %s", pkgname, headerspkg)
cmdf("cp -r /boot /target/")
cmdf("cp -r /lib/modules /target/lib/")
cmdf("cp -r /usr/src /target/usr/")
c, err := container.New(u.Distro())
if err != nil {
return
}
for i := range c.Volumes {
c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
}
_, err = c.Run("", commands)
if err != nil {
return
}
return
}
func Cleanup(km config.Target, pkgname string) {
return
}