pkg/utils: Support host operating systems without VERSION_ID

The VERSION_ID field in os-release(5) is optional [1].  It's absent on
Arch Linux, which follows a rolling-release model and uses the BUILD_ID
field instead:
  BUILD_ID=rolling

A subsequent commit will add built-in support for Arch Linux.  Hence,
the code to get the default release from the host operating system can
no longer assume the presence of the VERSION_ID field in os-release(5).

Note that the arch-toolbox image is tagged with 'latest', in accordance
with OCI conventions, not 'rolling' [2,3], which is the os-release(5)
BUILD_ID.  Therefore, it will be wise to use 'latest' as the default
release on Arch Linux, to simplify how the default release matches with
the default image's tag.  This means that a os-release(5) field can't be
used for the default release on Arch.

[1] https://www.freedesktop.org/software/systemd/man/os-release.html

[2] Commit 2568528cb7
    https://github.com/containers/toolbox/pull/861

[3] Commit a4e5861ae5
    https://github.com/containers/toolbox/pull/1308

https://github.com/containers/toolbox/pull/1303
This commit is contained in:
Debarshi Ray 2023-06-07 13:06:19 +02:00
parent 28913fad1d
commit d14fd7bb50

View file

@ -39,12 +39,14 @@ import (
"golang.org/x/sys/unix"
)
type GetDefaultReleaseFunc func() (string, error)
type GetFullyQualifiedImageFunc func(string, string) string
type ParseReleaseFunc func(string) (string, error)
type Distro struct {
ContainerNamePrefix string
ImageBasename string
GetDefaultRelease GetDefaultReleaseFunc
GetFullyQualifiedImage GetFullyQualifiedImageFunc
ParseRelease ParseReleaseFunc
}
@ -100,18 +102,21 @@ var (
"fedora": {
"fedora-toolbox",
"fedora-toolbox",
getDefaultReleaseFedora,
getFullyQualifiedImageFedora,
parseReleaseFedora,
},
"rhel": {
"rhel-toolbox",
"toolbox",
getDefaultReleaseRHEL,
getFullyQualifiedImageRHEL,
parseReleaseRHEL,
},
"ubuntu": {
"ubuntu-toolbox",
"ubuntu-toolbox",
getDefaultReleaseUbuntu,
getFullyQualifiedImageUbuntu,
parseReleaseUbuntu,
},
@ -140,7 +145,7 @@ func init() {
hostID, err := GetHostID()
if err == nil {
if distroObj, supportedDistro := supportedDistros[hostID]; supportedDistro {
release, err := getHostVersionID()
release, err := getDefaultReleaseForDistro(hostID)
if err == nil {
containerNamePrefixDefault = distroObj.ContainerNamePrefix
distroDefault = hostID
@ -273,6 +278,52 @@ func getDefaultImageForDistro(distro, release string) string {
return image
}
func getDefaultReleaseForDistro(distro string) (string, error) {
if distro == "" {
panic("distro not specified")
}
distroObj, supportedDistro := supportedDistros[distro]
if !supportedDistro {
panicMsg := fmt.Sprintf("failed to find %s in the list of supported distributions", distro)
panic(panicMsg)
}
release, err := distroObj.GetDefaultRelease()
if err != nil {
return "", err
}
return release, nil
}
func getDefaultReleaseFedora() (string, error) {
release, err := getHostVersionID()
if err != nil {
return "", err
}
return release, nil
}
func getDefaultReleaseRHEL() (string, error) {
release, err := getHostVersionID()
if err != nil {
return "", err
}
return release, nil
}
func getDefaultReleaseUbuntu() (string, error) {
release, err := getHostVersionID()
if err != nil {
return "", err
}
return release, nil
}
func GetEnvOptionsForPreservedVariables() []string {
logrus.Debug("Creating list of environment variables to forward")