2023-05-18 09:49:48 +00:00
|
|
|
package ubuntu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-18 11:28:06 +00:00
|
|
|
"strings"
|
2023-05-18 09:49:48 +00:00
|
|
|
|
2024-02-20 13:25:31 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/config/dotfiles"
|
2023-05-18 11:28:06 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
2023-05-18 21:37:07 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro"
|
2023-05-18 09:49:48 +00:00
|
|
|
)
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
func init() {
|
|
|
|
releases := []string{
|
|
|
|
"12.04",
|
|
|
|
"14.04",
|
|
|
|
"16.04",
|
|
|
|
"18.04",
|
|
|
|
"20.04",
|
|
|
|
"22.04",
|
2024-05-22 18:00:18 +00:00
|
|
|
"24.04",
|
2023-05-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
2023-05-23 16:54:34 +00:00
|
|
|
distro.Register(Ubuntu{release: release})
|
2023-05-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Ubuntu struct {
|
2023-05-23 16:54:34 +00:00
|
|
|
release string
|
2023-05-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u Ubuntu) Equal(d distro.Distro) bool {
|
|
|
|
return u.release == d.Release && distro.Ubuntu == d.ID
|
|
|
|
}
|
|
|
|
|
2023-05-23 16:24:01 +00:00
|
|
|
func (u Ubuntu) Distro() distro.Distro {
|
|
|
|
return distro.Distro{ID: distro.Ubuntu, Release: u.release}
|
|
|
|
}
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
func (u Ubuntu) Packages() (pkgs []string, err error) {
|
2023-05-23 16:54:34 +00:00
|
|
|
c, err := container.New(u.Distro())
|
2023-05-18 21:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-21 18:16:51 +00:00
|
|
|
if !c.Exist() {
|
|
|
|
err = c.Build("ubuntu:"+u.release, u.envs(), u.runs())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-05-23 13:20:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
cmd := "apt-cache search " +
|
2023-05-22 06:56:12 +00:00
|
|
|
"--names-only '^linux-image-[0-9\\.\\-]*-generic$' " +
|
2023-05-18 21:37:07 +00:00
|
|
|
"| awk '{ print $1 }'"
|
|
|
|
|
2024-02-20 13:25:31 +00:00
|
|
|
output, err := c.Run(dotfiles.Dir("tmp"), []string{cmd})
|
2023-05-18 21:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-20 13:25:31 +00:00
|
|
|
pkgs = append(pkgs, strings.Fields(output)...)
|
2023-05-18 21:37:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:00:20 +00:00
|
|
|
func (u Ubuntu) Kernels() (kernels []distro.KernelInfo, err error) {
|
|
|
|
c, err := container.New(u.Distro())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Kernels()
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
func (u Ubuntu) envs() (envs []string) {
|
2023-05-18 09:49:48 +00:00
|
|
|
envs = append(envs, "DEBIAN_FRONTEND=noninteractive")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
func (u Ubuntu) runs() (commands []string) {
|
2023-05-18 09:49:48 +00:00
|
|
|
cmdf := func(f string, s ...interface{}) {
|
|
|
|
commands = append(commands, fmt.Sprintf(f, s...))
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
if u.release < "14.04" {
|
2023-05-18 09:49:48 +00:00
|
|
|
cmdf("sed -i 's/archive.ubuntu.com/old-releases.ubuntu.com/' " +
|
|
|
|
"/etc/apt/sources.list")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdf("apt-get update")
|
|
|
|
cmdf("apt-get install -y build-essential libelf-dev")
|
|
|
|
cmdf("apt-get install -y wget git")
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
if u.release == "12.04" {
|
2023-05-22 06:55:06 +00:00
|
|
|
cmdf("apt-get install -y grub")
|
|
|
|
cmdf("cp /bin/true /usr/sbin/grub-probe")
|
|
|
|
cmdf("mkdir -p /boot/grub")
|
|
|
|
cmdf("touch /boot/grub/menu.lst")
|
|
|
|
}
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
if u.release < "14.04" {
|
2023-05-18 09:49:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-21 16:03:18 +00:00
|
|
|
if u.release == "22.04" {
|
|
|
|
cmdf("apt-get install -y gcc-12")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 09:49:48 +00:00
|
|
|
cmdf("apt-get install -y libseccomp-dev")
|
|
|
|
|
|
|
|
// 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=$(apt-cache search " +
|
2023-05-18 09:49:48 +00:00
|
|
|
"--names-only '^linux-headers-[0-9\\.\\-]*-generic' " +
|
2023-05-18 10:50:24 +00:00
|
|
|
"| awk '{ print $1 }' | head -n 1)"
|
2023-05-18 09:49:48 +00:00
|
|
|
|
2023-05-18 10:50:24 +00:00
|
|
|
cmd += " KERNEL=$(echo $HEADERS | sed 's/headers/image/')"
|
|
|
|
cmd += " MODULES=$(echo $HEADERS | sed 's/headers/modules/')"
|
2023-05-18 09:49:48 +00:00
|
|
|
|
2023-05-18 10:50:24 +00:00
|
|
|
cmd += " && apt-get install -y $HEADERS $KERNEL $MODULES"
|
|
|
|
cmd += " && apt-get remove -y $HEADERS $KERNEL $MODULES"
|
|
|
|
|
|
|
|
cmdf(cmd)
|
2023-05-18 09:49:48 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-05-18 11:28:06 +00:00
|
|
|
|
2023-05-23 22:36:46 +00:00
|
|
|
func (u Ubuntu) Install(pkgname string, headers bool) (err error) {
|
2023-05-18 11:46:12 +00:00
|
|
|
var headerspkg string
|
|
|
|
if headers {
|
|
|
|
headerspkg = strings.Replace(pkgname, "image", "headers", -1)
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:36:46 +00:00
|
|
|
var commands []string
|
2023-05-18 11:46:12 +00:00
|
|
|
cmdf := func(f string, s ...interface{}) {
|
|
|
|
commands = append(commands, fmt.Sprintf(f, s...))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdf("apt-get install -y %s %s", pkgname, headerspkg)
|
2023-05-23 22:36:46 +00:00
|
|
|
cmdf("cp -r /boot /target/")
|
|
|
|
cmdf("cp -r /lib/modules /target/lib/")
|
|
|
|
cmdf("cp -r /usr/src /target/usr/")
|
2023-05-18 11:46:12 +00:00
|
|
|
|
2023-05-23 22:36:46 +00:00
|
|
|
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
|
|
|
|
}
|
2023-05-18 11:46:12 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-06-15 15:24:29 +00:00
|
|
|
|
|
|
|
func (u Ubuntu) RootFS() string {
|
|
|
|
return fmt.Sprintf("out_of_tree_ubuntu_%s.img",
|
|
|
|
strings.Replace(u.release, ".", "__", -1))
|
|
|
|
}
|