1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
package sources
import (
"crypto/sha256"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/lxc/distrobuilder/shared"
)
type almalinux struct {
commonRHEL
fname string
majorVersion string
}
// Run downloads the tarball and unpacks it.
func (s *almalinux) Run() error {
var err error
s.majorVersion = strings.Split(s.definition.Image.Release, ".")[0]
baseURL := fmt.Sprintf("%s/%s/isos/%s/", s.definition.Source.URL,
strings.ToLower(s.definition.Image.Release),
s.definition.Image.ArchitectureMapped)
s.fname, err = s.getRelease(s.definition.Source.URL, s.definition.Image.Release,
s.definition.Source.Variant, s.definition.Image.ArchitectureMapped)
if err != nil {
return fmt.Errorf("Failed to get release: %w", err)
}
fpath := s.getTargetDir()
// Skip download if raw image exists and has already been decompressed.
if strings.HasSuffix(s.fname, ".raw.xz") {
imagePath := filepath.Join(fpath, filepath.Base(strings.TrimSuffix(s.fname, ".xz")))
stat, err := os.Stat(imagePath)
if err == nil && stat.Size() > 0 {
s.logger.WithField("file", filepath.Join(fpath, strings.TrimSuffix(s.fname, ".xz"))).Info("Unpacking raw image")
return s.unpackRaw(filepath.Join(fpath, strings.TrimSuffix(s.fname, ".xz")),
s.rootfsDir, s.rawRunner)
}
}
url, err := url.Parse(baseURL)
if err != nil {
return fmt.Errorf("Failed to parse URL %q: %w", baseURL, err)
}
checksumFile := ""
if !s.definition.Source.SkipVerification {
// Force gpg checks when using http
if url.Scheme != "https" {
if len(s.definition.Source.Keys) == 0 {
return errors.New("GPG keys are required if downloading from HTTP")
}
if s.definition.Image.ArchitectureMapped == "armhfp" {
checksumFile = "sha256sum.txt"
} else {
if strings.HasPrefix(s.definition.Image.Release, "8") {
checksumFile = "CHECKSUM"
} else {
checksumFile = "sha256sum.txt.asc"
}
}
fpath, err := s.DownloadHash(s.definition.Image, baseURL+checksumFile, "", nil)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", baseURL+checksumFile, err)
}
// Only verify file if possible.
if strings.HasSuffix(checksumFile, ".asc") {
valid, err := s.VerifyFile(filepath.Join(fpath, checksumFile), "")
if err != nil {
return fmt.Errorf("Failed to verify %q: %w", checksumFile, err)
}
if !valid {
return fmt.Errorf("Invalid signature for %q", filepath.Join(fpath, checksumFile))
}
}
}
}
_, err = s.DownloadHash(s.definition.Image, baseURL+s.fname, checksumFile, sha256.New())
if err != nil {
return fmt.Errorf("Failed to download %q: %w", baseURL+s.fname, err)
}
if strings.HasSuffix(s.fname, ".raw.xz") || strings.HasSuffix(s.fname, ".raw") {
s.logger.WithField("file", filepath.Join(fpath, s.fname)).Info("Unpacking raw image")
return s.unpackRaw(filepath.Join(fpath, s.fname), s.rootfsDir, s.rawRunner)
}
s.logger.WithField("file", filepath.Join(fpath, s.fname)).Info("Unpacking ISO")
return s.unpackISO(filepath.Join(fpath, s.fname), s.rootfsDir, s.isoRunner)
}
func (s *almalinux) rawRunner() error {
err := shared.RunScript(s.ctx, fmt.Sprintf(`#!/bin/sh
set -eux
# Create required files
touch /etc/mtab /etc/fstab
# Create a minimal rootfs
mkdir /rootfs
yum --installroot=/rootfs --disablerepo=* --enablerepo=base -y --releasever=%s install basesystem almalinux-release yum
rm -rf /rootfs/var/cache/yum
`, s.majorVersion))
if err != nil {
return fmt.Errorf("Failed to run script: %w", err)
}
return nil
}
func (s *almalinux) isoRunner(gpgKeysPath string) error {
err := shared.RunScript(s.ctx, fmt.Sprintf(`#!/bin/sh
set -eux
GPG_KEYS="%s"
# Create required files
touch /etc/mtab /etc/fstab
yum_args=""
mkdir -p /etc/yum.repos.d
if [ -d /mnt/cdrom ]; then
# Install initial package set
cd /mnt/cdrom/Packages
rpm -ivh --nodeps $(ls rpm-*.rpm | head -n1)
rpm -ivh --nodeps $(ls yum-*.rpm | head -n1)
# Add cdrom repo
cat <<- EOF > /etc/yum.repos.d/cdrom.repo
[cdrom]
name=Install CD-ROM
baseurl=file:///mnt/cdrom
enabled=0
EOF
if [ -n "${GPG_KEYS}" ]; then
echo gpgcheck=1 >> /etc/yum.repos.d/cdrom.repo
echo gpgkey=${GPG_KEYS} >> /etc/yum.repos.d/cdrom.repo
else
echo gpgcheck=0 >> /etc/yum.repos.d/cdrom.repo
fi
yum_args="--disablerepo=* --enablerepo=cdrom"
yum ${yum_args} -y reinstall yum
else
if ! [ -f /etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux ]; then
mv /etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-* /etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux
fi
cat <<- "EOF" > /etc/yum.repos.d/almalinux.repo
[baseos]
name=AlmaLinux $releasever - BaseOS
baseurl=https://repo.almalinux.org/almalinux/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux
EOF
# Use dnf in the boot iso since yum isn't available
alias yum=dnf
fi
pkgs="basesystem almalinux-release yum"
# Create a minimal rootfs
mkdir /rootfs
yum ${yum_args} --installroot=/rootfs -y --releasever=%s --skip-broken install ${pkgs}
rm -rf /rootfs/var/cache/yum
`, gpgKeysPath, s.majorVersion))
if err != nil {
return fmt.Errorf("Failed to run script: %w", err)
}
return nil
}
func (s *almalinux) getRelease(URL, release, variant, arch string) (string, error) {
fullURL := URL + path.Join("/", strings.ToLower(release), "isos", arch)
var (
err error
resp *http.Response
)
err = shared.Retry(func() error {
resp, err = http.Get(fullURL)
if err != nil {
return fmt.Errorf("Failed to GET %q: %w", fullURL, err)
}
return nil
}, 3)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Failed to read body: %w", err)
}
re := s.getRegexes(arch, variant, release)
for _, r := range re {
matches := r.FindAllString(string(body), -1)
if len(matches) > 0 {
return matches[len(matches)-1], nil
}
}
return "", nil
}
func (s *almalinux) getRegexes(arch string, variant string, release string) []*regexp.Regexp {
releaseFields := strings.Split(release, ".")
var re []string
switch len(releaseFields) {
case 1:
re = append(re, fmt.Sprintf("AlmaLinux-%s(\\.\\d+)*-%s-(?i:%s)(-\\d+)?.iso",
releaseFields[0], arch, variant))
re = append(re, fmt.Sprintf("AlmaLinux-%s(.\\d+)*-(beta|rc)-\\d-%s-(?i:%s).iso",
releaseFields[0], arch, variant))
case 2:
re = append(re, fmt.Sprintf("AlmaLinux-%s\\.%s-%s-(?i:%s)(-\\d+)?.iso",
releaseFields[0], releaseFields[1], arch, variant))
re = append(re, fmt.Sprintf("AlmaLinux-%s\\.%s-(beta|rc)-\\d-%s-(?i:%s).iso",
releaseFields[0], releaseFields[1], arch, variant))
}
regexes := make([]*regexp.Regexp, len(re))
for i, r := range re {
regexes[i] = regexp.MustCompile(r)
}
return regexes
}
|