Skip to content

Commit c9b2352

Browse files
committed
Add scripts to create iso using bootc-image-builder
1 parent b1a54e7 commit c9b2352

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed

scripts/image-mode/build.sh

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/bin/bash
2+
set -exo pipefail
3+
4+
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../../" && pwd )"
5+
SCRIPTDIR=${ROOTDIR}/scripts/image-mode
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=${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_REDHAT_AUTHORIZED_KEYS_CONTENTS^${AUTHORIZED_KEYS}^g" \
116+
> 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 "${SCRIPTDIR}"/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+
${IMAGE_NAME}:${IMAGE_VERSION}
136+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 && \
16+
systemctl enable microshift && \
17+
dnf clean all
18+
19+
# Mandatory firewall configuration
20+
RUN firewall-offline-cmd --zone=public --add-port=22/tcp && \
21+
firewall-offline-cmd --zone=trusted --add-source=10.42.0.0/16 && \
22+
firewall-offline-cmd --zone=trusted --add-source=169.254.169.1 && \
23+
firewall-offline-cmd --zone=trusted --add-source=fd01::/48
24+
# Application-specific firewall configuration
25+
RUN firewall-offline-cmd --zone=public --add-port=80/tcp && \
26+
firewall-offline-cmd --zone=public --add-port=443/tcp && \
27+
firewall-offline-cmd --zone=public --add-port=30000-32767/tcp && \
28+
firewall-offline-cmd --zone=public --add-port=30000-32767/udp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
# Configure ostree
41+
ostreesetup --nogpg --osname=rhel --remote=edge --url=file:///run/install/repo/ostree/repo --ref=rhel/REPLACE_OSVERSION/REPLACE_BUILD_ARCH/edge
42+
43+
%post --log=/var/log/anaconda/post-install.log --erroronfail
44+
45+
# Update the ostree server URL
46+
ostree remote delete edge
47+
ostree remote add --no-gpg-verify edge REPLACE_OSTREE_SERVER_URL
48+
49+
# The pull secret is mandatory for MicroShift builds on top of OpenShift, but not OKD
50+
# The /etc/crio/crio.conf.d/microshift.conf references the /etc/crio/openshift-pull-secret file
51+
cat > /etc/crio/openshift-pull-secret <<EOF
52+
REPLACE_OCP_PULL_SECRET_CONTENTS
53+
EOF
54+
chmod 600 /etc/crio/openshift-pull-secret
55+
56+
# Create a default redhat user, allowing it to run sudo commands without password
57+
useradd -m -d /home/redhat -p redhat redhat
58+
echo -e 'redhat\tALL=(ALL)\tNOPASSWD: ALL' > /etc/sudoers.d/microshift
59+
60+
# Add authorized ssh keys
61+
mkdir -m 700 /home/redhat/.ssh
62+
cat > /home/redhat/.ssh/authorized_keys <<EOF
63+
REPLACE_REDHAT_AUTHORIZED_KEYS_CONTENTS
64+
EOF
65+
chmod 600 /home/redhat/.ssh/authorized_keys
66+
67+
# Make sure redhat user directory contents ownership is correct
68+
chown -R redhat:redhat /home/redhat/
69+
70+
# Configure the firewall (rules reload is not necessary here)
71+
firewall-offline-cmd --zone=trusted --add-source=10.42.0.0/16
72+
firewall-offline-cmd --zone=trusted --add-source=169.254.169.1
73+
74+
# Make the KUBECONFIG from MicroShift directly available for the root user
75+
echo -e 'export KUBECONFIG=/var/lib/microshift/resources/kubeadmin/kubeconfig' >> /root/.profile
76+
77+
# Configure systemd journal service to persist logs between boots and limit their size to 1G
78+
sudo mkdir -p /etc/systemd/journald.conf.d
79+
cat > /etc/systemd/journald.conf.d/microshift.conf <<EOF
80+
[Journal]
81+
Storage=persistent
82+
SystemMaxUse=1G
83+
RuntimeMaxUse=1G
84+
EOF
85+
86+
# Update certificate trust storage in case new certificates were
87+
# installed at /etc/pki/ca-trust/source/anchors directory
88+
update-ca-trust
89+
%end
90+
"""

0 commit comments

Comments
 (0)