+# os-release(5):
+# (...) a newline-separated list of environment-like shell-compatible
+# variable assignments. (...) beyond mere variable assignments, no shell
+# features are supported (this means variable expansion is explicitly not
+# supported) (...). Variable assignment values must be enclosed in double or
+# single quotes *if* they include spaces, semicolons or other special
+# characters outside of A-Z, a-z, 0-9. Shell special characters ("$", quotes,
+# backslash, backtick) must be escaped with backslashes (...). All strings
+# should be in UTF-8 format, and non-printable characters should not be used.
+# It is not supported to concatenate multiple individually quoted strings.
+# Lines beginning with "#" shall be ignored as comments.
+my $parse_os_release = sub {
+ my ($data) = @_;
+ my $variables = {};
+ while (defined($data) && $data =~ /^(.+)$/gm) {
+ next if $1 !~ /^\s*([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;
+ my ($var, $content) = ($1, $2);
+ chomp $content;
+
+ if ($content =~ /^'([^']*)'/) {
+ $variables->{$var} = $1;
+ } elsif ($content =~ /^"((?:[^"\\]|\\.)*)"/) {
+ my $s = $1;
+ $s =~ s/(\\["'`nt\$\\])/"\"$1\""/eeg;
+ $variables->{$var} = $s;
+ } elsif ($content =~ /^([A-Za-z0-9]*)/) {
+ $variables->{$var} = $1;
+ }
+ }
+ return $variables;
+};
+
+sub get_ct_os_release {
+ my ($self) = @_;
+
+ my $code = sub {
+ if (-f '/etc/os-release') {
+ return PVE::Tools::file_get_contents('/etc/os-release');
+ } elsif (-f '/usr/lib/os-release') {
+ return PVE::Tools::file_get_contents('/usr/lib/os-release');
+ }
+ return undef;
+ };
+
+ my $data = $self->protected_call($code);
+
+ return &$parse_os_release($data);
+}
+