Skip to content

Commit 125c5ac

Browse files
committed
Create MicroShift iso using image mode and bootc image builder
With 4.18 microshift removed the steps of creating the iso using image builder and there is no more `build.sh` script which is consumed by mircoshift.sh script to create it. This PR use the image mode and bootc image builder (BIB) to create the iso which is now microshift team also pushing forward.
1 parent 359c50d commit 125c5ac

File tree

4 files changed

+265
-89
lines changed

4 files changed

+265
-89
lines changed

image-mode/microshift/build.sh

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/bin/bash
2+
set -exo pipefail
3+
4+
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../../" && pwd )"
5+
SCRIPTDIR=${ROOTDIR}/image-mode/microshift
6+
IMGNAME=microshift
7+
USHIFT_VERSION=4.17
8+
BUILD_ARCH=$(uname -m)
9+
OSVERSION=$(awk -F: '{print $5}' /etc/system-release-cpe)
10+
LVM_SYSROOT_SIZE_MIN=10240
11+
LVM_SYSROOT_SIZE=${LVM_SYSROOT_SIZE_MIN}
12+
OCP_PULL_SECRET_FILE=
13+
AUTHORIZED_KEYS_FILE=
14+
AUTHORIZED_KEYS=
15+
USE_MIRROR_REPO=
16+
17+
# shellcheck disable=SC2034
18+
STARTTIME="$(date +%s)"
19+
BUILDDIR=${BUILDDIR:-${ROOTDIR}/_output/image-mode}
20+
21+
usage() {
22+
local error_message="$1"
23+
24+
if [ -n "${error_message}" ]; then
25+
echo "ERROR: ${error_message}"
26+
echo
27+
fi
28+
29+
echo "Usage: $(basename "$0") <-pull_secret_file path_to_file> [OPTION]..."
30+
echo ""
31+
echo " -pull_secret_file path_to_file"
32+
echo " Path to a file containing the OpenShift pull secret, which can be"
33+
echo " obtained from https://console.redhat.com/openshift/downloads#tool-pull-secret"
34+
echo ""
35+
echo "Optional arguments:"
36+
echo " -lvm_sysroot_size num_in_MB"
37+
echo " Size of the system root LVM partition. The remaining"
38+
echo " disk space will be allocated for data (default: ${LVM_SYSROOT_SIZE})"
39+
echo " -authorized_keys_file path_to_file"
40+
echo " Path to an SSH authorized_keys file to allow SSH access"
41+
echo " into the default 'redhat' account"
42+
echo " -use-mirror-repo <mirror_repo>"
43+
echo " Use mirror repo to get release candidate and engineering preview rpms"
44+
echo " like (https://mirror.openshift.com/pub/openshift-v4/x86_64/microshift/ocp-dev-preview/latest-4.18/el9/os/)"
45+
echo " -ushift-version <microshift-version>"
46+
echo " Version of microshift for image generation (default: ${USHIFT_VERSION}"
47+
exit 1
48+
}
49+
50+
title() {
51+
echo -e "\E[34m\n# $1\E[00m"
52+
}
53+
54+
# Parse the command line
55+
while [ $# -gt 0 ] ; do
56+
case $1 in
57+
-pull_secret_file)
58+
shift
59+
OCP_PULL_SECRET_FILE="$1"
60+
[ -z "${OCP_PULL_SECRET_FILE}" ] && usage "Pull secret file not specified"
61+
[ ! -s "${OCP_PULL_SECRET_FILE}" ] && usage "Empty or missing pull secret file"
62+
shift
63+
;;
64+
-lvm_sysroot_size)
65+
shift
66+
LVM_SYSROOT_SIZE="$1"
67+
[ -z "${LVM_SYSROOT_SIZE}" ] && usage "System root LVM partition size not specified"
68+
[ "${LVM_SYSROOT_SIZE}" -lt ${LVM_SYSROOT_SIZE_MIN} ] && usage "System root LVM partition size cannot be smaller than ${LVM_SYSROOT_SIZE_MIN}MB"
69+
shift
70+
;;
71+
-authorized_keys_file)
72+
shift
73+
AUTHORIZED_KEYS_FILE="$1"
74+
[ -z "${AUTHORIZED_KEYS_FILE}" ] && usage "Authorized keys file not specified"
75+
shift
76+
;;
77+
-use-mirror-repo)
78+
shift
79+
USE_MIRROR_REPO="$1"
80+
[ -z "${USE_MIRROR_REPO}" ] && usage "Mirror repo not specified"
81+
shift
82+
;;
83+
-ushift-version)
84+
shift
85+
USHIFT_VERSION="$1"
86+
[ -z "${USHIFT_VERSION}" ] && usage "MicroShift version not specified"
87+
shift
88+
;;
89+
*)
90+
usage
91+
;;
92+
esac
93+
done
94+
95+
if [ ! -r "${OCP_PULL_SECRET_FILE}" ] ; then
96+
echo "ERROR: pull_secret_file file does not exist or not readable: ${OCP_PULL_SECRET_FILE}"
97+
exit 1
98+
fi
99+
if [ -n "${AUTHORIZED_KEYS_FILE}" ]; then
100+
if [ ! -e "${AUTHORIZED_KEYS_FILE}" ]; then
101+
echo "ERROR: authorized_keys_file does not exist: ${AUTHORIZED_KEYS_FILE}"
102+
exit 1
103+
else
104+
AUTHORIZED_KEYS=$(cat "${AUTHORIZED_KEYS_FILE}")
105+
fi
106+
fi
107+
108+
mkdir -p "${BUILDDIR}"
109+
110+
title "Preparing kickstart config"
111+
# Create a kickstart file from a template, compacting pull secret contents if necessary
112+
cat < "${SCRIPTDIR}/config/config.toml.template" \
113+
| sed "s;REPLACE_LVM_SYSROOT_SIZE;${LVM_SYSROOT_SIZE};g" \
114+
| sed "s;REPLACE_OCP_PULL_SECRET_CONTENTS;$(cat < "${OCP_PULL_SECRET_FILE}" | jq -c);g" \
115+
| sed "s^REPLACE_CORE_AUTHORIZED_KEYS_CONTENTS^${AUTHORIZED_KEYS}^g" \
116+
> "${BUILDDIR}"/config.toml
117+
118+
title "Building bootc image for microshift"
119+
sudo podman build --authfile ${OCP_PULL_SECRET_FILE} -t ${IMGNAME}:${USHIFT_VERSION} \
120+
--build-arg USHIFT_VER=${USHIFT_VERSION} \
121+
--env MIRROR_REPO=${USE_MIRROR_REPO} \
122+
-f "${SCRIPTDIR}/config/Containerfile.bootc-rhel9"
123+
124+
title "Creating ISO image"
125+
sudo podman run --authfile ${OCP_PULL_SECRET_FILE} --rm -it \
126+
--privileged \
127+
--security-opt label=type:unconfined_t \
128+
-v /var/lib/containers/storage:/var/lib/containers/storage \
129+
-v "${BUILDDIR}"/config.toml:/config.toml \
130+
-v "${BUILDDIR}":/output \
131+
registry.redhat.io/rhel9/bootc-image-builder:latest \
132+
--local \
133+
--type iso \
134+
--config /config.toml \
135+
localhost/${IMGNAME}:${USHIFT_VERSION}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM registry.redhat.io/rhel9/rhel-bootc:9.4
2+
3+
ARG USHIFT_VER=4.17
4+
RUN if [ -z "${MIRROR_REPO}" ]; then \
5+
dnf config-manager --set-enabled "rhocp-${USHIFT_VER}-for-rhel-9-$(uname -m)-rpms" \
6+
--set-enabled "fast-datapath-for-rhel-9-$(uname -m)-rpms"; \
7+
else \
8+
# This is required to update the gpgcheck for repoID
9+
repoID=$(echo "${MIRROR_REPO#*://}" | tr '/:' '_'); \
10+
dnf config-manager --add-repo "${MIRROR_REPO}" \
11+
--add-repo "https://mirror.openshift.com/pub/openshift-v4/$(uname -m)/dependencies/rpms/${USHIFT_VER}-el9-beta" \
12+
--set-enabled "fast-datapath-for-rhel-9-$(uname -m)-rpms"; \
13+
dnf config-manager --save --setopt="${repoID}".gpgcheck=0 --setopt=*-el9-beta.gpgcheck=0; \
14+
fi
15+
RUN dnf install -y firewalld microshift microshift-release-info cloud-utils-growpart qemu-guest-agent && \
16+
systemctl enable microshift && \
17+
dnf clean all && rm -fr /etc/yum.repos.d/*
18+
19+
RUN rm -fr /opt && ln -sf /var/opt /opt && mkdir /var/opt
20+
21+
# Mandatory firewall configuration
22+
RUN firewall-offline-cmd --zone=public --add-port=22/tcp && \
23+
firewall-offline-cmd --zone=trusted --add-source=10.42.0.0/16 && \
24+
firewall-offline-cmd --zone=trusted --add-source=169.254.169.1 && \
25+
firewall-offline-cmd --zone=trusted --add-source=fd01::/48
26+
# Application-specific firewall configuration
27+
RUN firewall-offline-cmd --zone=public --add-port=80/tcp && \
28+
firewall-offline-cmd --zone=public --add-port=443/tcp && \
29+
firewall-offline-cmd --zone=public --add-port=30000-32767/tcp && \
30+
firewall-offline-cmd --zone=public --add-port=30000-32767/udp
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[customizations.installer.kickstart]
2+
contents = """
3+
lang en_US.UTF-8
4+
keyboard us
5+
timezone UTC
6+
text
7+
reboot
8+
9+
# Configure network to use DHCP and activate on boot
10+
network --bootproto=dhcp --device=link --activate --onboot=on
11+
12+
# Partition disk with a 1MB BIOS boot, 200M EFI, 800M boot XFS partition and
13+
# an LVM volume containing a 10GB+ system root. The remainder of the volume
14+
# will be used by the CSI driver for storing data
15+
#
16+
# For example, a 20GB disk would be partitioned in the following way:
17+
#
18+
# NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
19+
# sda 8:0 0 20G 0 disk
20+
# ├─sda1 8:1 0 1M 0 part
21+
# ├─sda2 8:2 0 200M 0 part /boot/efi
22+
# ├─sda3 8:3 0 800M 0 part /boot
23+
# └─sda4 8:4 0 19G 0 part
24+
# └─rhel-root 253:0 0 10G 0 lvm /sysroot
25+
#
26+
zerombr
27+
clearpart --all --disklabel gpt
28+
part biosboot --fstype=biosboot --size=1
29+
part /boot/efi --fstype=efi --size=200
30+
part /boot --fstype=xfs --asprimary --size=800
31+
# Uncomment this line to add a SWAP partition of the recommended size
32+
#part swap --fstype=swap --recommended
33+
part pv.01 --grow
34+
volgroup rhel pv.01
35+
logvol / --vgname=rhel --fstype=xfs --size=REPLACE_LVM_SYSROOT_SIZE --name=root
36+
37+
# Lock root user account
38+
rootpw --lock
39+
40+
41+
%post --log=/var/log/anaconda/post-install.log --erroronfail
42+
43+
# The pull secret is mandatory for MicroShift builds on top of OpenShift, but not OKD
44+
# The /etc/crio/crio.conf.d/microshift.conf references the /etc/crio/openshift-pull-secret file
45+
cat > /etc/crio/openshift-pull-secret <<EOF
46+
REPLACE_OCP_PULL_SECRET_CONTENTS
47+
EOF
48+
chmod 600 /etc/crio/openshift-pull-secret
49+
50+
# Create a default core user, allowing it to run sudo commands without password
51+
useradd -m -d /home/core core
52+
echo -e 'core\tALL=(ALL)\tNOPASSWD: ALL' > /etc/sudoers.d/microshift
53+
54+
# Add authorized ssh keys
55+
mkdir -m 700 /home/core/.ssh
56+
cat > /home/core/.ssh/authorized_keys <<EOF
57+
REPLACE_CORE_AUTHORIZED_KEYS_CONTENTS
58+
EOF
59+
chmod 600 /home/core/.ssh/authorized_keys
60+
61+
# Make sure core user directory contents ownership is correct
62+
chown -R core:core /home/core/
63+
64+
# Configure the firewall (rules reload is not necessary here)
65+
firewall-offline-cmd --zone=trusted --add-source=10.42.0.0/16
66+
firewall-offline-cmd --zone=trusted --add-source=169.254.169.1
67+
68+
# Make the KUBECONFIG from MicroShift directly available for the root user
69+
echo -e 'export KUBECONFIG=/var/lib/microshift/resources/kubeadmin/kubeconfig' >> /root/.profile
70+
71+
# Configure systemd journal service to persist logs between boots and limit their size to 1G
72+
sudo mkdir -p /etc/systemd/journald.conf.d
73+
cat > /etc/systemd/journald.conf.d/microshift.conf <<EOF
74+
[Journal]
75+
Storage=persistent
76+
SystemMaxUse=1G
77+
RuntimeMaxUse=1G
78+
EOF
79+
80+
# Update certificate trust storage in case new certificates were
81+
# installed at /etc/pki/ca-trust/source/anchors directory
82+
update-ca-trust
83+
%end
84+
"""

microshift.sh

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ SNC_CLUSTER_CPUS=${SNC_CLUSTER_CPUS:-2}
1616
CRC_VM_DISK_SIZE=${CRC_VM_DISK_SIZE:-31}
1717
BASE_DOMAIN=${CRC_BASE_DOMAIN:-testing}
1818
MIRROR=${MIRROR:-https://mirror.openshift.com/pub/openshift-v4/$ARCH/clients/ocp-dev-preview}
19-
OPENSHIFT_MINOR_VERSION=${OPENSHIFT_MINOR_VERSION:-4.17}
20-
21-
if ! grep -q -i "release 9" /etc/redhat-release
22-
then
23-
echo "This script only works for RHEL-9"
24-
exit 1
25-
fi
19+
MICROSHIFT_VERSION=${MICROSHIFT_VERSION:-4.19}
20+
MIRROR_REPO=${MIRROR_REPO:-https://mirror.openshift.com/pub/openshift-v4/$ARCH/microshift/ocp-dev-preview/latest-${MICROSHIFT_VERSION}/el9/os}
2621

2722
echo "Check if system is registered"
2823
# Check the subscription status and register if necessary
@@ -42,96 +37,28 @@ create_libvirt_resources
4237
rm id_ecdsa_crc* || true
4338
ssh-keygen -t ecdsa -b 521 -N "" -f id_ecdsa_crc -C "core"
4439

45-
# This requirement is taken from https://github.com/openshift/microshift/blob/main/scripts/image-builder/configure.sh
46-
# Also https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/composing_a_customized_rhel_system_image/installing-composer_composing-a-customized-rhel-system-image
47-
# list out the dependencies and usecase.
48-
# lorax packages which install mkksiso is required for embedding kickstart file to iso file
49-
# podman package is required to run the ostree-container to serve the rpm-ostree content
50-
# createrepo package is required to create localrepo for microshift and it's dependenices
51-
# yum-utils package is required for reposync utility to synchronize packages of a remote DNF repository to a local directory
52-
function configure_host {
53-
sudo dnf install -y git osbuild-composer composer-cli ostree rpm-ostree \
54-
cockpit-composer cockpit-machines bash-completion lorax \
55-
yum-utils createrepo
56-
sudo dnf install -y podman --setopt=install_weak_deps=True
57-
sudo systemctl start osbuild-composer.socket
58-
sudo systemctl start cockpit.socket
59-
sudo firewall-cmd --add-service=cockpit
60-
}
61-
62-
function enable_repos {
63-
local enable_repos="--enable fast-datapath-for-rhel-9-$(uname -i)-rpms"
64-
if [ -z "${MICROSHIFT_PRERELEASE-}" ]; then
65-
enable_repos="${enable_repos} --enable rhocp-${OPENSHIFT_MINOR_VERSION}-for-rhel-9-$(uname -i)-rpms"
66-
fi
67-
sudo subscription-manager repos ${enable_repos}
68-
}
69-
70-
function download_microshift_rpm {
71-
local pkgDir=$1
72-
local extra_opts=""
73-
local nvr_suffix=""
74-
if [ -n "${MICROSHIFT_PRERELEASE-}" ]; then
75-
extra_opts="--setopt=reposdir=./repos"
76-
elif [ -n "${MICROSHIFT_NVR-}" ]; then
77-
nvr_suffix="-${MICROSHIFT_NVR-}"
78-
fi
79-
sudo yum download ${extra_opts} --downloaddir ${pkgDir} --downloadonly microshift${nvr_suffix} microshift-networking${nvr_suffix} \
80-
microshift-release-info${nvr_suffix} microshift-selinux${nvr_suffix} microshift-greenboot${nvr_suffix} microshift-olm${nvr_suffix} \
81-
microshift-multus${nvr_suffix}
82-
}
83-
8440
function create_iso {
85-
local pkgDir=$1
86-
rm -fr microshift
87-
git clone -b release-${OPENSHIFT_MINOR_VERSION} https://github.com/openshift/microshift.git
88-
cp podman_changes.ks microshift/
89-
pushd microshift
90-
sed -i '/# customizations/,$d' scripts/image-builder/config/blueprint_v0.0.1.toml
91-
cat << EOF >> scripts/image-builder/config/blueprint_v0.0.1.toml
92-
[[packages]]
93-
name = "microshift-release-info"
94-
version = "*"
95-
[[packages]]
96-
name = "cloud-utils-growpart"
97-
version = "*"
98-
[[packages]]
99-
name = "qemu-guest-agent"
100-
version = "*"
101-
EOF
102-
sed -i 's/redhat/core/g' scripts/image-builder/config/kickstart.ks.template
103-
sed -i "/--bootproto=dhcp/a\network --hostname=api.${SNC_PRODUCT_NAME}.${BASE_DOMAIN}" scripts/image-builder/config/kickstart.ks.template
104-
sed -i 's/clearpart --all --initlabel/clearpart --all --disklabel gpt/g' scripts/image-builder/config/kickstart.ks.template
105-
sed -i "/clearpart --all/a\part biosboot --fstype=biosboot --size=1" scripts/image-builder/config/kickstart.ks.template
106-
sed -i '$i\grub2-install --target=i386-pc /dev/vda' scripts/image-builder/config/kickstart.ks.template
107-
sed -i '$e cat podman_changes.ks' scripts/image-builder/config/kickstart.ks.template
108-
scripts/image-builder/cleanup.sh -full
109-
# The home dir and files must have read permissions to group
110-
# and others because osbuilder is running from another non-priviledged user account
111-
# and allow it to read the files on current user home (like reading yum repo which is created as part of build script), it is required.
112-
# https://github.com/openshift/microshift/blob/main/scripts/image-builder/configure.sh#L29-L32
113-
chmod 0755 $HOME
114-
115-
scripts/image-builder/build.sh -microshift_rpms ${pkgDir} -pull_secret_file ${OPENSHIFT_PULL_SECRET_PATH} -lvm_sysroot_size 15360 -authorized_keys_file $(realpath ../id_ecdsa_crc.pub)
116-
popd
41+
local buildDir=$1
42+
local mirror_repo=""
43+
if [ -n "${MICROSHIFT_PRERELEASE-}" ]; then
44+
mirror_repo=${MIRROR_REPO}
45+
fi
46+
BUILDDIR=${buildDir} image-mode/microshift/build.sh -pull_secret_file ${OPENSHIFT_PULL_SECRET_PATH} \
47+
-lvm_sysroot_size 15360 \
48+
-authorized_keys_file $(realpath id_ecdsa_crc.pub) \
49+
-ushift-version ${MICROSHIFT_VERSION} \
50+
-use-mirror-repo ${mirror_repo}
11751
}
11852

119-
configure_host
120-
121-
enable_repos
12253
microshift_pkg_dir=$(mktemp -p /tmp -d tmp-rpmXXX)
123-
# This directory contains the microshift rpm passed to osbuilder, worker for osbuilder
124-
# running as non-priviledged user and this tmp directory have 0700 permission. To allow
125-
# worker to read/execute this file we need to change the permission to 0755
126-
chmod 0755 ${microshift_pkg_dir}
127-
download_microshift_rpm ${microshift_pkg_dir}
54+
12855
create_iso ${microshift_pkg_dir}
129-
sudo cp -Z microshift/_output/image-builder/microshift-installer-*.iso /var/lib/libvirt/${SNC_PRODUCT_NAME}/microshift-installer.iso
130-
OPENSHIFT_RELEASE_VERSION=$(rpm -qp --qf '%{VERSION}' ${microshift_pkg_dir}/microshift-4.*.rpm)
56+
sudo cp -Z ${microshift_pkg_dir}/bootiso/install.iso /var/lib/libvirt/${SNC_PRODUCT_NAME}/microshift-installer.iso
57+
OPENSHIFT_RELEASE_VERSION=$(sudo podman run --rm -it localhost/microshift:${MICROSHIFT_VERSION} /usr/bin/rpm -q --qf '%{VERSION}' microshift)
13158
# Change 4.x.0~ec0 to 4.x.0-ec0
13259
# https://docs.fedoraproject.org/en-US/packaging-guidelines/Versioning/#_complex_versioning
13360
OPENSHIFT_RELEASE_VERSION=$(echo ${OPENSHIFT_RELEASE_VERSION} | tr '~' '-')
134-
rm -fr ${microshift_pkg_dir}
61+
sudo rm -fr ${microshift_pkg_dir}
13562

13663
# Download the oc binary for specific OS environment
13764
OC=./openshift-clients/linux/oc

0 commit comments

Comments
 (0)