Skip to content

Commit

Permalink
[Linux] Properly detect Debian bookworm/sid (dotnet#6221)
Browse files Browse the repository at this point in the history
The current Debian unstable distribution (codenamed "bookworm") no
longer contains any release version information in the `/etc/os-release`
file (the `VERSION` and `VERSION_ID` and `VERSION_CODENAME` fields were
removed) causing `xaprepare` to fail when running on this version of
Debian.

Fix the Debian/unstable version detection by looking for known release
codenames and faking the version number.
  • Loading branch information
grendello authored Aug 23, 2021
1 parent 1dbb547 commit 10335e6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class LinuxDebian : LinuxDebianCommon
new DebianLinuxProgram ("zulu-8"),
};

static readonly Dictionary<string, string> DebianUnstableVersionMap = new Dictionary<string, string> (StringComparer.OrdinalIgnoreCase) {
{ "bookworm", "12" },
{ "bookworm/sid", "12" },
};

protected Version DebianRelease { get; private set; } = new Version (0, 0);
protected bool IsTesting { get; private set; }

Expand All @@ -42,24 +47,49 @@ protected override void InitializeDependencies ()
Dependencies.AddRange (packagesPre10);
}

static bool IsDebian10OrNewer (string[] lines)
static bool IsDebian10OrNewer (string? version)
{
if (lines == null || lines.Length < 1)
return false;

string version = lines[0].Trim ();
if (String.IsNullOrEmpty (version))
if (String.IsNullOrEmpty (version)) {
return false;
}

return
version.IndexOf ("bullseye", StringComparison.OrdinalIgnoreCase) >= 0 ||
version.IndexOf ("bookworm", StringComparison.OrdinalIgnoreCase) >= 0 ||
version.IndexOf ("sid", StringComparison.OrdinalIgnoreCase) >= 0;
}

protected override bool InitOS ()
static string? ReadDebianVersion ()
{
if (!File.Exists (DebianVersionPath)) {
return null;
}

string[] lines = File.ReadAllLines (DebianVersionPath);
return lines[0].Trim ();
}

static bool IsBookwormSidOrNewer (string? debian_version)
{
if (!base.InitOS ())
if (String.IsNullOrEmpty (debian_version)) {
return false;
}

return debian_version!.IndexOf ("bookworm", StringComparison.OrdinalIgnoreCase) >= 0;
}

protected override bool EnsureVersionInformation (Context context)
{
string? debian_version = null;
if (String.IsNullOrEmpty (Release)) {
// Debian/unstable "bookworm" (to become Debian 12 eventually) removed
// VERSION_ID and VERSION_CODENAME from /etc/os-release, so we need to
// fake it
debian_version = ReadDebianVersion ();
if (IsBookwormSidOrNewer (debian_version) && DebianUnstableVersionMap.TryGetValue (debian_version!, out string unstable_version)) {
Release = unstable_version;
};
}

Version debianRelease;
if (!Version.TryParse (Release, out debianRelease)) {
Expand All @@ -76,7 +106,11 @@ protected override bool InitOS ()
}

if (debianRelease.Major < 10 && DerivativeDistro && File.Exists (DebianVersionPath)) {
if (IsDebian10OrNewer (File.ReadAllLines (DebianVersionPath)))
if (String.IsNullOrEmpty (debian_version)) {
debian_version = ReadDebianVersion ();
}

if (IsDebian10OrNewer (debian_version))
debianRelease = new Version (10, 0); // faking it, but it's ok
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override void InitializeDependencies ()
}
}

protected override bool InitOS ()
protected override bool EnsureVersionInformation (Context context)
{
Version ubuntuRelease;
if (!Version.TryParse (Release, out ubuntuRelease)) {
Expand All @@ -51,7 +51,7 @@ protected override bool InitOS ()
}
UbuntuRelease = ubuntuRelease;

return base.InitOS ();
return true;
}
};
}
9 changes: 9 additions & 0 deletions build-tools/xaprepare/xaprepare/OperatingSystems/Linux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ static bool MapDistro (string id, ref string distro)
return true;
}

protected virtual bool EnsureVersionInformation (Context context)
{
return true;
}

public static Linux DetectAndCreate (Context context)
{
string name = String.Empty;
Expand Down Expand Up @@ -234,6 +239,10 @@ public static Linux DetectAndCreate (Context context)
linux.codeName = codeName;
linux.derived = usingBaseDistro;

if (!linux.EnsureVersionInformation (context)) {
throw new InvalidOperationException ("Unable to detect version of your Linux distribution");
}

return linux;
}

Expand Down

0 comments on commit 10335e6

Please sign in to comment.