>From 2895d8b361881a00473b620a059ded0e04ed20c5 Mon Sep 17 00:00:00 2001
From: Stef Walter <stefw@redhat.com>
Date: Fri, 21 May 2021 13:49:02 +0200
Subject: [PATCH] templates: Add CentOS Stream 8

This adds CentOS Stream 8 to the set of images that the builder
can use. CentOS Stream 8 is a continuously delivered upstream of
the next RHEL 8.x release. It doesn't have a minor version number.

It appears that my index-fragment has a hash in it, and thus this
commit may need to be amended by the maintainer before merging
into the main repo.

This has been tested by creating a local centosstream-8.xz image
then consuming that image via a local repos.d file and using
virt-builder to build a bootable CentOS Stream 8 image:

     $ cat ~/.config/virt-builder/repos.d/local.conf
     [local]
     uri=file:///data/src/guestfs-tools/builder/templates/index
     $ virt-builder --list | grep stream
     centosstream-8           x86_64     CentOS Stream 8
     $ virt-builder centosstream-8
     [   1.0] Downloading: file:///data/src/guestfs-tools/build...
     [   1.7] Planning how to build this image
     [   1.7] Uncompressing
     [   7.8] Opening the new disk
     [  12.6] Setting a random seed
     [  12.6] Setting passwords
     virt-builder: Setting random password of root to qmjtBFyLX...
     [  13.5] Finishing off
                   Output file: centosstream-8.img
                   Output size: 6.0G
                 Output format: raw
            Total usable space: 5.4G
                    Free space: 3.8G (71%)
---
 .../templates/centosstream-8.index-fragment   | 19 +++++++
 builder/templates/centosstream-8.ks           | 29 ++++++++++
 .../templates/centosstream-8.virt-install-cmd | 20 +++++++
 builder/templates/make-template.ml            | 53 +++++++++++++------
 4 files changed, 104 insertions(+), 17 deletions(-)
 create mode 100644 builder/templates/centosstream-8.index-fragment
 create mode 100644 builder/templates/centosstream-8.ks
 create mode 100644 builder/templates/centosstream-8.virt-install-cmd

diff --git a/builder/templates/centosstream-8.index-fragment b/builder/templates/centosstream-8.index-fragment
new file mode 100644
index 00000000..13cbbea3
--- /dev/null
+++ b/builder/templates/centosstream-8.index-fragment
@@ -0,0 +1,19 @@
+[centosstream-8]
+name=CentOS Stream 8
+osinfo=centosstream8
+arch=x86_64
+file=centosstream-8.xz
+checksum[sha512]=73668d92f2b028820ed7ec01367f16cff12c4a62c2b9dcd3c56d7ea3aefa25f45f03d859a0f047dd264db3bfc69e8bc95aa48e6bd350b41e2353a97e5d95fdb1
+format=raw
+size=6442450944
+compressed_size=471938880
+expand=/dev/sda4
+notes=CentOS Stream 8
+ 
+ This CentOS Stream image contains only unmodified @Core group packages.
+ 
+ This template was generated by a script in the libguestfs source tree:
+     builder/templates/make-template.ml
+ Associated files used to prepare this template can be found in the
+ same directory.
+
diff --git a/builder/templates/centosstream-8.ks b/builder/templates/centosstream-8.ks
new file mode 100644
index 00000000..098e33e4
--- /dev/null
+++ b/builder/templates/centosstream-8.ks
@@ -0,0 +1,29 @@
+# Kickstart file for centosstream-8
+# Generated by libguestfs.git/builder/templates/make-template.ml
+
+install
+text
+reboot
+lang en_US.UTF-8
+keyboard us
+network --bootproto dhcp
+rootpw builder
+firewall --enabled --ssh
+timezone --utc America/New_York
+selinux --enforcing
+
+bootloader --location=mbr --append="console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH"
+
+
+zerombr
+clearpart --all --initlabel --disklabel=gpt
+autopart --type=plain
+
+# Halt the system once configuration has finished.
+poweroff
+
+%packages
+@core
+%end
+
+# EOF
diff --git a/builder/templates/centosstream-8.virt-install-cmd b/builder/templates/centosstream-8.virt-install-cmd
new file mode 100644
index 00000000..6b66b3ff
--- /dev/null
+++ b/builder/templates/centosstream-8.virt-install-cmd
@@ -0,0 +1,20 @@
+# This is the virt-install command which was used to create
+# the virt-builder template 'centosstream-8'
+# NB: This file is generated for documentation purposes ONLY!
+# This script was never run, and is not intended to be run.
+
+'virt-install' \
+    '--transient' \
+    '--name=tmp-parkabak' \
+    '--ram=4096' \
+    '--arch=x86_64' \
+    '--cpu=host' \
+    '--vcpus=4' \
+    '--os-variant=rhel8.0' \
+    '--initrd-inject=centosstream-8.ks' \
+    '--extra-args=ks=file:/centosstream-8.ks  console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH' \
+    '--disk=/data/src/guestfs-tools/builder/templates/tmp-parkabak.img,size=6,format=raw' \
+    '--location=http://mirror.centos.org/centos/8-stream/BaseOS/x86_64/os' \
+    '--serial=pty' \
+    '--nographics' 
+
diff --git a/builder/templates/make-template.ml b/builder/templates/make-template.ml
index b7120a04..f9a1cdbb 100755
--- a/builder/templates/make-template.ml
+++ b/builder/templates/make-template.ml
@@ -76,6 +76,7 @@ let () =
 
 type os =
   | CentOS of int * int         (* major, minor *)
+  | CentOSStream of int         (* major *)
   | RHEL of int * int
   | Debian of int * string      (* version, dist name like "wheezy" *)
   | Ubuntu of string * string
@@ -346,6 +347,7 @@ Options:
 and os_of_string os ver =
   match os, ver with
   | "centos", ver -> let maj, min = parse_major_minor ver in CentOS (maj, min)
+  | "centosstream", ver -> CentOSStream(int_of_string ver)
   | "rhel", ver -> let maj, min = parse_major_minor ver in RHEL (maj, min)
   | "debian", "6" -> Debian (6, "squeeze")
   | "debian", "7" -> Debian (7, "wheezy")
@@ -424,6 +426,9 @@ and filename_of_os os arch ext =
   | CentOS (major, minor) ->
      if arch = X86_64 then sprintf "centos-%d.%d%s" major minor ext
      else sprintf "centos-%d.%d-%s%s" major minor (string_of_arch arch) ext
+  | CentOSStream ver ->
+     if arch = X86_64 then sprintf "centosstream-%d%s" ver ext
+     else sprintf "centosstream-%d-%s%s" ver (string_of_arch arch) ext
   | RHEL (major, minor) ->
      if arch = X86_64 then sprintf "rhel-%d.%d%s" major minor ext
      else sprintf "rhel-%d.%d-%s%s" major minor (string_of_arch arch) ext
@@ -451,6 +456,7 @@ and string_of_os os arch = filename_of_os os arch ""
 and string_of_os_noarch = function
   | Fedora ver -> sprintf "fedora-%d" ver
   | CentOS (major, minor) -> sprintf "centos-%d.%d" major minor
+  | CentOSStream ver -> sprintf "centosstream-%d" ver
   | RHEL (major, minor) -> sprintf "rhel-%d.%d" major minor
   | Debian (ver, _) -> sprintf "debian-%d" ver
   | Ubuntu (ver, _) -> sprintf "ubuntu-%s" ver
@@ -460,11 +466,11 @@ and string_of_os_noarch = function
 
 (* Does virt-sysprep know how to sysprep this OS? *)
 and can_sysprep_os = function
-  | RHEL _ | CentOS _ | Fedora _ | Debian _ | Ubuntu _ -> true
+  | RHEL _ | CentOS _ | CentOSStream _ | Fedora _ | Debian _ | Ubuntu _ -> true
   | FreeBSD _ | Windows _ -> false
 
 and is_selinux_os = function
-  | RHEL _ | CentOS _ | Fedora _ -> true
+  | RHEL _ | CentOS _ | CentOSStream _ | Fedora _ -> true
   | Debian _ | Ubuntu _
   | FreeBSD _ | Windows _ -> false
 
@@ -472,13 +478,13 @@ and needs_uefi os arch =
   match os, arch with
   | Fedora _, Aarch64
   | RHEL _, Aarch64 -> true
-  | RHEL _, _ | CentOS _, _ | Fedora _, _
+  | RHEL _, _ | CentOS _, _ | CentOSStream _, _ | Fedora _, _
   | Debian _, _ | Ubuntu _, _
   | FreeBSD _, _ | Windows _, _ -> false
 
 and get_virtual_size_gb os arch =
   match os with
-  | RHEL _ | CentOS _ | Fedora _
+  | RHEL _ | CentOS _ | CentOSStream _ | Fedora _
   | Debian _ | Ubuntu _
   | FreeBSD _ -> 6
   | Windows (10, _, _) -> 40    (* Windows 10 *)
@@ -489,7 +495,7 @@ and get_virtual_size_gb os arch =
 and make_kickstart os arch =
   match os with
   (* Kickstart. *)
-  | Fedora _ | CentOS _ | RHEL _ ->
+  | Fedora _ | CentOS _ | CentOSStream _ | RHEL _ ->
      let ks_filename = filename_of_os os arch ".ks" in
      Some (make_kickstart_common ks_filename os arch)
 
@@ -516,7 +522,7 @@ and make_kickstart_common ks_filename os arch =
   (* Fedora 34+ removes the "install" keyword. *)
   (match os with
    | Fedora n when n >= 34 -> ()
-   | RHEL (n, _) | CentOS (n, _) when n >= 9 -> ()
+   | RHEL (n, _) | CentOS (n, _) | CentOSStream n when n >= 9 -> ()
    | _ -> bpf "install\n";
   );
 
@@ -578,7 +584,7 @@ part /boot --fstype=%s   --size=512         --asprimary
 part swap                --size=1024        --asprimary
 part /     --fstype=%s   --size=1024 --grow --asprimary
 " bootfs rootfs;
-   | CentOS _ | RHEL _ | Fedora _ ->
+   | CentOS _ | CentOSStream _ | RHEL _ | Fedora _ ->
       bpf "\
 zerombr
 clearpart --all --initlabel --disklabel=gpt
@@ -854,6 +860,9 @@ and make_boot_media os arch =
   | CentOS (8, _), X86_64 ->
      Location "http://mirror.centos.org/centos-8/8/BaseOS/x86_64/kickstart"
 
+  | CentOSStream ver, X86_64 ->
+     Location (sprintf "http://mirror.centos.org/centos/%d-stream/BaseOS/x86_64/os" ver)
+
   | Debian (_, dist), arch ->
      Location (sprintf "http://deb.debian.org/debian/dists/%s/main/installer-%s"
                        dist (debian_arch_of_arch arch))
@@ -981,7 +990,7 @@ The FreeBSD install is not automated.  Select all defaults, except:
 
 (* If the install is not automated and we need a graphical console. *)
 and needs_graphics = function
-  | CentOS _ | RHEL _ | Debian _ | Ubuntu _ | Fedora _ -> false
+  | CentOS _ | CentOSStream _ | RHEL _ | Debian _ | Ubuntu _ | Fedora _ -> false
   | FreeBSD _ | Windows _ -> true
 
 (* NB: Arguments do not need to be quoted, because we pass them
@@ -1052,7 +1061,7 @@ and make_virt_install_command os arch ks tmpname tmpout tmpefivars
   (* --initrd-inject and --extra-args flags for Linux only. *)
   (match os with
    | Debian _ | Ubuntu _
-   | Fedora _ | RHEL _ | CentOS _ ->
+   | Fedora _ | RHEL _ | CentOS _ | CentOSStream _ ->
       let ks =
         match ks with None -> assert false | Some ks -> ks in
       add (sprintf "--initrd-inject=%s" ks);
@@ -1062,9 +1071,9 @@ and make_virt_install_command os arch ks tmpname tmpout tmpefivars
         | Debian _ | Ubuntu _ -> "auto"
         | Fedora n when n >= 34 ->
            sprintf "inst.ks=file:/%s" (Filename.basename ks)
-        | RHEL (n, _) | CentOS (n, _) when n >= 9 ->
+        | RHEL (n, _) | CentOS (n, _) | CentOSStream n when n >= 9 ->
            sprintf "inst.ks=file:/%s" (Filename.basename ks)
-        | Fedora _ | RHEL _ | CentOS _ ->
+        | Fedora _ | RHEL _ | CentOS _ | CentOSStream _ ->
            sprintf "ks=file:/%s" (Filename.basename ks)
         | FreeBSD _ | Windows _ -> assert false in
       let proxy =
@@ -1072,15 +1081,15 @@ and make_virt_install_command os arch ks tmpname tmpout tmpefivars
         match p with
         | None ->
            (match os with
-            | Fedora _ | RHEL _ | CentOS _ | Ubuntu _ -> ""
+            | Fedora _ | RHEL _ | CentOS _ | CentOSStream _ | Ubuntu _ -> ""
             | Debian _ -> "mirror/http/proxy="
             | FreeBSD _ | Windows _ -> assert false
            )
         | Some p ->
            match os with
            | Fedora n when n >= 34 -> sprintf "inst.proxy=" ^ p
-           | RHEL (n, _) | CentOS (n, _) when n >= 9 -> "inst.proxy=" ^ p
-           | Fedora _ | RHEL _ | CentOS _ -> "proxy=" ^ p
+           | RHEL (n, _) | CentOS (n, _) | CentOSStream n when n >= 9 -> "inst.proxy=" ^ p
+           | Fedora _ | RHEL _ | CentOS _ | CentOSStream _ -> "proxy=" ^ p
            | Debian _ | Ubuntu _ -> "mirror/http/proxy=" ^ p
            | FreeBSD _ | Windows _ -> assert false in
 
@@ -1138,6 +1147,7 @@ and os_variant_of_os ?(for_fedora = false) os arch =
     match os with
     | Fedora ver -> sprintf "fedora%d" ver
     | CentOS (major, minor) -> sprintf "centos%d.%d" major minor
+    | CentOSStream ver -> sprintf "centosstream%d" ver
     | RHEL (major, minor) -> sprintf "rhel%d.%d" major minor
     | Debian (ver, _) -> sprintf "debian%d" ver
     | Ubuntu (ver, _) -> sprintf "ubuntu%s" ver
@@ -1166,6 +1176,9 @@ and os_variant_of_os ?(for_fedora = false) os arch =
        sprintf "centos%d.%d" major minor
     | CentOS _, _ -> "centos7.0" (* max version known in Fedora 31 *)
 
+    | CentOSStream 8, _ -> "rhel8.0" (* temporary until osinfo updated *)
+    | CentOSStream _, _ -> "rhel8.0" (* min known version is 8 *)
+
     | RHEL (6, minor), _ when minor <= 8 ->
        sprintf "rhel6.%d" minor
     | RHEL (6, _), _ -> "rhel6.9" (* max version known in Fedora 29 *)
@@ -1205,8 +1218,8 @@ and kernel_cmdline_of_os os arch =
      "console=tty0 console=ttyAMA0,115200 rd_NO_PLYMOUTH"
   | (Debian _|Fedora _|Ubuntu _), (PPC64|PPC64le) ->
      "console=tty0 console=hvc0 rd_NO_PLYMOUTH"
-  | (RHEL _|CentOS _), PPC64
-  | (RHEL _|CentOS _), PPC64le ->
+  | (RHEL _|CentOS _|CentOSStream _), PPC64
+  | (RHEL _|CentOS _|CentOSStream _), PPC64le ->
      "console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH"
 
   | FreeBSD _, _ | Windows _, _ -> assert false
@@ -1231,7 +1244,7 @@ and make_postinstall os arch =
          g#write "/etc/yum.repos.d/download.devel.redhat.com.repo" yum_conf
      )
 
-  | RHEL _ | Fedora _ | CentOS _ | FreeBSD _ | Windows _ -> None
+  | RHEL _ | Fedora _ | CentOS _ | CentOSStream _ | FreeBSD _ | Windows _ -> None
 
 and make_rhel_yum_conf major minor arch =
   let buf = Buffer.create 4096 in
@@ -1383,6 +1396,10 @@ and long_name_of_os os arch =
      sprintf "CentOS %d.%d" major minor
   | CentOS (major, minor), arch ->
      sprintf "CentOS %d.%d (%s)" major minor (string_of_arch arch)
+  | CentOSStream ver, X86_64 ->
+     sprintf "CentOS Stream %d" ver
+  | CentOSStream ver, arch ->
+     sprintf "CentOS Stream %d (%s)" ver (string_of_arch arch)
   | Debian (ver, dist), X86_64 ->
      sprintf "Debian %d (%s)" ver dist
   | Debian (ver, dist), arch ->
@@ -1427,6 +1444,8 @@ and notes_of_os os arch nvram =
   (match os with
    | CentOS _ ->
       add "This CentOS image contains only unmodified @Core group packages."
+   | CentOSStream _ ->
+      add "This CentOS Stream image contains only unmodified @Core group packages."
    | Debian _ ->
       add "This is a minimal Debian install."
    | Fedora _ ->
-- 
2.31.1

