Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Add scripts and documentation for building a CentOS image.
Browse files Browse the repository at this point in the history
Signed-off-by: dlorenc <[email protected]>
  • Loading branch information
dlorenc authored and dlorenc committed Oct 31, 2018
1 parent 6dcf6a4 commit 2c31c67
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mkdebootstrap/mkimage/
mkdebootstrap/Dockerfile
bazel-*
rootfs.tar.gz
centos/layer.tar
3 changes: 3 additions & 0 deletions centos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
ADD layer.tar /
CMD ["/bin/bash"]
4 changes: 4 additions & 0 deletions centos/Dockerfile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM centos
COPY build.sh .
COPY chroot.sh /target/

27 changes: 27 additions & 0 deletions centos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## CentOS

This directory contains scripts and Dockerfiles for building a CentOS container image.

### Overview

We bootstrap the image following a process based on a combination of https://wiki.centos.org/HowTos/ManualInstall
and https://github.com/CentOS/sig-cloud-instance-build/blob/master/docker/centos-7.ks.

We avoid using kickstart to make it easier to run in a container environment like Cloud Build, but still reuse the
package list and cleanup steps from the kickstart installation.

To build an image:

```shell
gcloud builds submit --config=cloudbuild.yaml
```

To build locally:

```shell
docker build . -f Dockerfile.build -t builder

docker run --privileged -v $(pwd):/workspace builder /build.sh

docker build . -t mycentosimage
```
27 changes: 27 additions & 0 deletions centos/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# This script sets up a bootstrapped CentOS chroot and saves it as a tarball.

mkdir /target
rpm --nodeps --root /target/ -i http://mirror.centos.org/centos/7/os/x86_64/Packages/centos-release-7-5.1804.el7.centos.x86_64.rpm
cp -f /etc/resolv.conf /target/etc

yum -q -y --installroot=/target --releasever=7 install yum
cp -f /etc/yum.conf /target/etc/
mkdir -p /target/dev
mount --bind /dev/ /target/dev/
mount -t proc procfs /target/proc/
mount -t sysfs sysfs /target/sys/

# Execute the chroot script.
chroot /target ./chroot.sh

# Cleanup and save as a tar.
umount /target/dev/
umount /target/proc/
umount /target/sys/
rm /target/chroot.sh

tar -C /target -cf /workspace/layer.tar .


28 changes: 28 additions & 0 deletions centos/chroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# This script runs inside a chroot and sets up a bootstrapped centos image.

yum -y -q --releasever=7 install yum centos-release
yum install -q -y bind-utils bash yum vim-minimal centos-release less iputils iproute systemd rootfiles tar passwd yum-utils yum-plugin-ovl hostname
yum -q -y erase kernel* *firmware firewalld-filesystem os-prober gettext* GeoIP bind-license freetype libteam teamd
rpm -e kernel
yum -y remove bind-libs bind-libs-lite dhclient dhcp-common dhcp-libs dracut-network e2fsprogs e2fsprogs-libs ebtables ethtool file firewalld freetype gettext gettext-libs groff-base grub2 grub2-tools grubby initscripts iproute iptables kexec-tools libcroco libgomp libmnl libnetfilter_conntrack libnfnetlink libselinux-python lzo libunistring os-prober python-decorator python-slip python-slip-dbus snappy sysvinit-tools which linux-firmware GeoIP firewalld-filesystem qemu-guest-agent
yum clean all
rm -rf /var/cache/yum
rm -rf /boot
rm -rf /etc/firewalld
passwd -l root
echo 'container' > /etc/yum/vars/infra
rm -rf /var/cache/yum/x86_64
rm -f /tmp/ks-script*
rm -rf /etc/sysconfig/network-scripts/ifcfg-*
rm -rf /etc/udev/hwdb.bin
rm -rf /usr/lib/udev/hwdb.d/*
:> /etc/machine-id
umount /run
systemd-tmpfiles --create --boot
rm /var/run/nologin
rpm --rebuilddb
rm /etc/resolv.conf

rm -rf /root/.bash_history
8 changes: 8 additions & 0 deletions centos/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
steps:
- name: 'docker'
args: ['build', '.', '-f', 'Dockerfile.build', '-t', 'builder']
- name: 'builder'
args: ['/build.sh']
- name: 'docker'
args: ['build', '.', '-t', 'gcr.io/$PROJECT_ID/centos7:$COMMIT_SHA']
images: ['gcr.io/$PROJECT_ID/centos7:$COMMIT_SHA']
8 changes: 8 additions & 0 deletions centos/release_cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
steps:
- name: 'docker'
args: ['build', '.', '-f', 'Dockerfile.build', '-t', 'builder']
- name: 'builder'
args: ['/build.sh']
- name: 'docker'
args: ['build', '.', '-t', 'gcr.io/$PROJECT_ID/centos7:$_TAG_NAME']
images: ['gcr.io/$PROJECT_ID/centos7:$_TAG_NAME']

0 comments on commit 2c31c67

Please sign in to comment.