From d14fd7bb50f0e71bbf7e1212e8ed9e28c02d97b3 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Wed, 7 Jun 2023 13:06:19 +0200 Subject: [PATCH] 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 2568528cb7f52663 https://github.com/containers/toolbox/pull/861 [3] Commit a4e5861ae5c93625 https://github.com/containers/toolbox/pull/1308 https://github.com/containers/toolbox/pull/1303 --- src/pkg/utils/utils.go | 53 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go index f5ba5da73..d25f09757 100644 --- a/src/pkg/utils/utils.go +++ b/src/pkg/utils/utils.go @@ -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")