Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: add optional fallback to /etc/initrd-release #15036

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions release/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,9 @@
package release

var (
ReadOSRelease = readOSRelease
ReadOSReleaseFromRoot = readOSReleaseFromRoot
)

func MockOSReleasePath(filename string) (restore func()) {
old := osReleasePath
oldFallback := fallbackOsReleasePath
osReleasePath = filename
fallbackOsReleasePath = filename
return func() {
osReleasePath = old
fallbackOsReleasePath = oldFallback
}
}

func MockFileExists(mockFileExists func(string) bool) (restorer func()) {
// Cannot use testutil.Backup due to import loop
old := fileExists
Expand Down
32 changes: 25 additions & 7 deletions release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"unicode"

Expand Down Expand Up @@ -53,25 +54,42 @@ func DistroLike(distros ...string) bool {
var (
osReleasePath = "/etc/os-release"
fallbackOsReleasePath = "/usr/lib/os-release"
initrdReleasePath = "/etc/initrd-release"
)

// readOSRelease returns the os-release information of the current system.
func readOSRelease() OS {
func readOSReleaseFromRoot(rootdir string) OS {
// TODO: separate this out into its own thing maybe (if made more general)
osRelease := OS{
// from os-release(5): If not set, defaults to "ID=linux".
ID: "linux",
}

f, err := os.Open(osReleasePath)
if err != nil {
var f *os.File
var err error
for _, candidate := range []string{
// is a default described in os-release(5) and should be valid for both
// booted system and an initrd
osReleasePath,
// this fallback is as per os-release(5)
f, err = os.Open(fallbackOsReleasePath)
if err != nil {
return osRelease
fallbackOsReleasePath,
// maybe we're in an initrd which is missing a symlink to
// /etc/initrd-release?
initrdReleasePath,
} {
f, err = os.Open(filepath.Join(rootdir, candidate))
if err == nil {
break
}
}

if f == nil {
// no os-release information source, return a default
return osRelease
}

defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
ws := strings.SplitN(scanner.Text(), "=", 2)
Expand Down Expand Up @@ -199,7 +217,7 @@ var WSLVersion int
var ReleaseInfo OS

func init() {
ReleaseInfo = readOSRelease()
ReleaseInfo = readOSReleaseFromRoot("/")

OnClassic = (ReleaseInfo.ID != "ubuntu-core")

Expand Down
121 changes: 88 additions & 33 deletions release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ func (s *ReleaseTestSuite) TestSetup(c *C) {
c.Check(release.Series, Equals, "16")
}

func mockOSRelease(c *C) string {
// FIXME: use AddCleanup here once available so that we
// can do release.SetLSBReleasePath() here directly
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
s := `
const refMockOSRelease = `
NAME="Ubuntu"
VERSION="18.09 (Awesome Artichoke)"
ID=ubuntu
Expand All @@ -59,10 +55,22 @@ HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
`
err := os.WriteFile(mockOSRelease, []byte(s), 0644)
c.Assert(err, IsNil)

return mockOSRelease
const refMockUbuntuCoreInitrdRelease = `
NAME="Ubuntu Core Initramfs"
VERSION="1"
ID=ubuntucoreinitramfs
PRETTY_NAME="Ubuntu Core Initramfs Kelly's Eye - Number One"
VERSION_ID="1"
VERSION_CODENAME=keelyseye
`

func mockOSRelease(c *C, root string, filesWithContent [][]string) {
for _, e := range filesWithContent {
target := filepath.Join(root, e[0])
c.Assert(os.MkdirAll(filepath.Dir(target), 0o755), IsNil)
c.Assert(os.WriteFile(target, []byte(e[1]), 0o644), IsNil)
}
}

// MockFilesystemRootType changes relase.ProcMountsPath so that it points to a temp file
Expand Down Expand Up @@ -109,16 +117,67 @@ func mockWSLsetup(c *C, settings mockWsl) func() {
}

func (s *ReleaseTestSuite) TestReadOSRelease(c *C) {
reset := release.MockOSReleasePath(mockOSRelease(c))
defer reset()
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", refMockOSRelease},
})

os := release.ReadOSRelease()
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntu")
c.Check(os.VersionID, Equals, "18.09")
}

func (s *ReleaseTestSuite) TestReadOSReleaseSymlink(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/mock-release", refMockOSRelease},
})

// now try again but with a symlink
c.Assert(os.Symlink("mock-release", filepath.Join(root, "/etc/os-release")), IsNil)

osRel := release.ReadOSReleaseFromRoot(root)
c.Check(osRel.ID, Equals, "ubuntu")
c.Check(osRel.VersionID, Equals, "18.09")
}

func (s *ReleaseTestSuite) TestReadOSReleaseFallback(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/usr/lib/os-release", refMockOSRelease},
})

os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntu")
c.Check(os.VersionID, Equals, "18.09")
}

func (s *ReleaseTestSuite) TestReadOSReleaseInitrdDirect(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/initrd-release", refMockUbuntuCoreInitrdRelease},
})

os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntucoreinitramfs")
c.Check(os.VersionID, Equals, "1")
}

func (s *ReleaseTestSuite) TestReadOSReleaseInitrdSymlink(c *C) {
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/mock-initrd-release", refMockUbuntuCoreInitrdRelease},
})

// now try again but with a symlink
c.Assert(os.Symlink("mock-initrd-release", filepath.Join(root, "/etc/os-release")), IsNil)

osRel := release.ReadOSReleaseFromRoot(root)
c.Check(osRel.ID, Equals, "ubuntucoreinitramfs")
c.Check(osRel.VersionID, Equals, "1")
}

func (s *ReleaseTestSuite) TestReadWonkyOSRelease(c *C) {
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
dump := `NAME="elementary OS"
VERSION="0.4 Loki"
ID="elementary OS"
Expand All @@ -128,19 +187,18 @@ VERSION_ID="0.4"
HOME_URL="http://elementary.io/"
SUPPORT_URL="http://elementary.io/support/"
BUG_REPORT_URL="https://bugs.launchpad.net/elementary/+filebug"`
err := os.WriteFile(mockOSRelease, []byte(dump), 0644)
c.Assert(err, IsNil)

reset := release.MockOSReleasePath(mockOSRelease)
defer reset()
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", dump},
})

os := release.ReadOSRelease()
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "elementary")
c.Check(os.VersionID, Equals, "0.4")
}

func (s *ReleaseTestSuite) TestFamilyOSRelease(c *C) {
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
dump := `NAME="CentOS Stream"
VERSION="9"
ID="centos"
Expand All @@ -155,43 +213,40 @@ HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://issues.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 9"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"`
err := os.WriteFile(mockOSRelease, []byte(dump), 0644)
c.Assert(err, IsNil)

reset := release.MockOSReleasePath(mockOSRelease)
defer reset()
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", dump},
})

os := release.ReadOSRelease()
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "centos")
c.Check(os.VersionID, Equals, "9")
c.Check(os.IDLike, DeepEquals, []string{"rhel", "fedora"})
}

func (s *ReleaseTestSuite) TestUbuntuCoreVariantRelease(c *C) {
mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
dump := `NAME="Ubuntu Core Desktop"
VERSION="22"
ID="ubuntu-core"
VARIANT_ID="desktop"
VERSION_ID="22"
"`
err := os.WriteFile(mockOSRelease, []byte(dump), 0644)
c.Assert(err, IsNil)

reset := release.MockOSReleasePath(mockOSRelease)
defer reset()
root := c.MkDir()
mockOSRelease(c, root, [][]string{
{"/etc/os-release", dump},
})

os := release.ReadOSRelease()
os := release.ReadOSReleaseFromRoot(root)
c.Check(os.ID, Equals, "ubuntu-core")
c.Check(os.VariantID, Equals, "desktop")
c.Check(os.VersionID, Equals, "22")
}

func (s *ReleaseTestSuite) TestReadOSReleaseNotFound(c *C) {
reset := release.MockOSReleasePath("not-there")
defer reset()

os := release.ReadOSRelease()
root := c.MkDir()
os := release.ReadOSReleaseFromRoot(root)
c.Assert(os, DeepEquals, release.OS{ID: "linux"})
}

Expand Down
Loading