2023-05-18 10:37:59 +00:00
|
|
|
package centos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-18 21:37:07 +00:00
|
|
|
"strings"
|
2023-05-18 10:37:59 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-18 21:37:07 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro"
|
2023-05-18 10:37:59 +00:00
|
|
|
)
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
func init() {
|
|
|
|
releases := []string{"6", "7", "8"}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
2023-05-23 16:54:34 +00:00
|
|
|
distro.Register(CentOS{release: release})
|
2023-05-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CentOS struct {
|
2023-05-23 16:54:34 +00:00
|
|
|
release string
|
2023-05-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (centos CentOS) Equal(d distro.Distro) bool {
|
|
|
|
return centos.release == d.Release && distro.CentOS == d.ID
|
|
|
|
}
|
|
|
|
|
2023-05-23 16:24:01 +00:00
|
|
|
func (centos CentOS) Distro() distro.Distro {
|
|
|
|
return distro.Distro{ID: distro.CentOS, Release: centos.release}
|
|
|
|
}
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
func (centos CentOS) Packages() (pkgs []string, err error) {
|
2023-05-23 16:54:34 +00:00
|
|
|
c, err := container.New(centos.Distro())
|
2023-05-18 21:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
err = c.Build("centos:"+centos.release, centos.envs(), centos.runs())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-22 06:01:40 +00:00
|
|
|
cmd := "yum search kernel --showduplicates 2>/dev/null " +
|
2023-05-18 21:37:07 +00:00
|
|
|
"| grep '^kernel-[0-9]' " +
|
|
|
|
"| grep -v src " +
|
|
|
|
"| cut -d ' ' -f 1"
|
|
|
|
|
2023-05-23 20:46:09 +00:00
|
|
|
output, err := c.Run(config.Dir("tmp"), []string{cmd})
|
2023-05-18 21:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkg := range strings.Fields(output) {
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:00:20 +00:00
|
|
|
func (centos CentOS) Kernels() (kernels []distro.KernelInfo, err error) {
|
|
|
|
c, err := container.New(centos.Distro())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Kernels()
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
func (centos CentOS) envs() (envs []string) {
|
2023-05-18 10:37:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
func (centos CentOS) runs() (commands []string) {
|
2023-05-18 10:37:59 +00:00
|
|
|
cmdf := func(f string, s ...interface{}) {
|
|
|
|
commands = append(commands, fmt.Sprintf(f, s...))
|
|
|
|
}
|
|
|
|
|
|
|
|
var repos []string
|
|
|
|
|
|
|
|
// TODO refactor
|
2023-05-23 13:20:48 +00:00
|
|
|
switch centos.release {
|
2023-05-18 10:37:59 +00:00
|
|
|
case "6":
|
|
|
|
repofmt := "[6.%d-%s]\\nbaseurl=https://vault.centos.org/6.%d/%s/$basearch/\\ngpgcheck=0"
|
|
|
|
for i := 0; i <= 10; i++ {
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, i, "os", i, "os"))
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, i, "updates", i, "updates"))
|
|
|
|
}
|
|
|
|
cmdf("rm /etc/yum.repos.d/*")
|
|
|
|
case "7":
|
|
|
|
repofmt := "[%s-%s]\\nbaseurl=https://vault.centos.org/%s/%s/$basearch/\\ngpgcheck=0"
|
|
|
|
for _, ver := range []string{
|
|
|
|
"7.0.1406", "7.1.1503", "7.2.1511",
|
|
|
|
"7.3.1611", "7.4.1708", "7.5.1804",
|
|
|
|
"7.6.1810", "7.7.1908", "7.8.2003",
|
|
|
|
} {
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, ver, "os", ver, "os"))
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, ver, "updates", ver, "updates"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME http/gpgcheck=0
|
|
|
|
repofmt = "[%s-%s]\\nbaseurl=http://mirror.centos.org/centos-7/%s/%s/$basearch/\\ngpgcheck=0"
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, "7.9.2009", "os", "7.9.2009", "os"))
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, "7.9.2009", "updates", "7.9.2009", "updates"))
|
|
|
|
case "8":
|
|
|
|
repofmt := "[%s-%s]\\nbaseurl=https://vault.centos.org/%s/%s/$basearch/os/\\ngpgcheck=0"
|
|
|
|
|
|
|
|
for _, ver := range []string{
|
|
|
|
"8.0.1905", "8.1.1911", "8.2.2004",
|
|
|
|
"8.3.2011", "8.4.2105", "8.5.2111",
|
|
|
|
} {
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, ver, "baseos", ver, "BaseOS"))
|
|
|
|
repos = append(repos, fmt.Sprintf(repofmt, ver, "appstream", ver, "AppStream"))
|
|
|
|
}
|
|
|
|
default:
|
2023-05-23 13:20:48 +00:00
|
|
|
log.Fatal().Msgf("no support for centos %s", centos.release)
|
2023-05-18 10:37:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdf("sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/* || true")
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
cmdf("echo -e '%s' >> /etc/yum.repos.d/oot.repo\n", repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not remove old kernels
|
|
|
|
|
|
|
|
cmdf("sed -i 's;installonly_limit=;installonly_limit=100500;' /etc/yum.conf")
|
|
|
|
cmdf("yum -y update")
|
|
|
|
|
|
|
|
cmdf("yum -y groupinstall 'Development Tools'")
|
|
|
|
|
2023-05-25 12:05:32 +00:00
|
|
|
cmdf("yum -y install linux-firmware")
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
if centos.release < "8" {
|
2023-05-18 10:37:59 +00:00
|
|
|
cmdf("yum -y install deltarpm")
|
|
|
|
} else {
|
|
|
|
cmdf("yum -y install grub2-tools-minimal elfutils-libelf-devel")
|
|
|
|
}
|
|
|
|
|
|
|
|
var flags string
|
2023-05-23 13:20:48 +00:00
|
|
|
if centos.release >= "8" {
|
2023-05-18 10:37:59 +00:00
|
|
|
flags = "--noautoremove"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install and remove a single kernel and headers.
|
|
|
|
// This ensures that all dependencies are cached.
|
|
|
|
|
2023-05-18 10:50:24 +00:00
|
|
|
cmd := "export HEADERS=$(yum search kernel-devel --showduplicates " +
|
|
|
|
"| grep '^kernel-devel' | cut -d ' ' -f 1 | head -n 1)"
|
2023-05-18 10:37:59 +00:00
|
|
|
|
2023-05-18 10:50:24 +00:00
|
|
|
cmd += " KERNEL=$(echo $HEADERS | sed 's/-devel//')"
|
|
|
|
cmd += " MODULES=$(echo $HEADERS | sed 's/-devel/-modules/')"
|
|
|
|
cmd += " CORE=$(echo $HEADERS | sed 's/-devel/-core/')"
|
2023-05-18 10:37:59 +00:00
|
|
|
|
2023-05-18 10:50:24 +00:00
|
|
|
cmd += " && yum -y install $KERNEL $HEADERS"
|
|
|
|
cmd += " && yum -y remove %s $KERNEL $HEADERS $MODULES $CORE"
|
|
|
|
|
|
|
|
cmdf(cmd, flags)
|
2023-05-18 10:37:59 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-05-23 22:36:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|