2014-01-23 12:01:21 -05:00
|
|
|
#!/usr/bin/env bash
|
2014-01-17 08:46:11 -05:00
|
|
|
#
|
|
|
|
# Create a base CentOS Docker image.
|
|
|
|
#
|
|
|
|
# This script is useful on systems with yum installed (e.g., building
|
2021-06-28 05:36:34 -04:00
|
|
|
# a CentOS image on CentOS).
|
2014-01-17 08:46:11 -05:00
|
|
|
|
2016-09-14 05:55:29 -04:00
|
|
|
set -e
|
|
|
|
|
2014-01-17 08:46:11 -05:00
|
|
|
usage() {
|
2020-03-02 22:27:49 -05:00
|
|
|
cat << EOOPTS
|
2014-01-17 08:46:11 -05:00
|
|
|
$(basename $0) [OPTIONS] <name>
|
|
|
|
OPTIONS:
|
2015-12-20 07:20:30 -05:00
|
|
|
-p "<packages>" The list of packages to install in the container.
|
2019-03-27 11:51:17 -04:00
|
|
|
The default is blank. Can use multiple times.
|
2015-12-20 07:20:30 -05:00
|
|
|
-g "<groups>" The groups of packages to install in the container.
|
2019-03-27 11:51:17 -04:00
|
|
|
The default is "Core". Can use multiple times.
|
2015-12-20 07:20:30 -05:00
|
|
|
-y <yumconf> The path to the yum config to install packages from. The
|
|
|
|
default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
|
2019-03-01 05:27:02 -05:00
|
|
|
-t <tag> Specify Tag information.
|
2022-03-01 01:26:35 -05:00
|
|
|
default is referred at /etc/{redhat,system}-release
|
2014-01-17 08:46:11 -05:00
|
|
|
EOOPTS
|
2020-03-02 22:27:49 -05:00
|
|
|
exit 1
|
2014-01-17 08:46:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# option defaults
|
|
|
|
yum_config=/etc/yum.conf
|
2015-11-21 07:06:27 -05:00
|
|
|
if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
|
|
|
|
yum_config=/etc/dnf/dnf.conf
|
|
|
|
alias yum=dnf
|
2015-12-20 07:20:30 -05:00
|
|
|
fi
|
2019-03-27 11:51:17 -04:00
|
|
|
# for names with spaces, use double quotes (") as install_groups=('Core' '"Compute Node"')
|
2019-03-29 10:51:33 -04:00
|
|
|
install_groups=()
|
|
|
|
install_packages=()
|
2019-03-01 05:27:02 -05:00
|
|
|
version=
|
|
|
|
while getopts ":y:p:g:t:h" opt; do
|
2020-03-02 22:27:49 -05:00
|
|
|
case $opt in
|
|
|
|
y)
|
|
|
|
yum_config=$OPTARG
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
install_packages+=("$OPTARG")
|
|
|
|
;;
|
|
|
|
g)
|
|
|
|
install_groups+=("$OPTARG")
|
|
|
|
;;
|
|
|
|
t)
|
|
|
|
version="$OPTARG"
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG"
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
2014-01-17 08:46:11 -05:00
|
|
|
done
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
name=$1
|
|
|
|
|
|
|
|
if [[ -z $name ]]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
usage
|
2014-01-17 08:46:11 -05:00
|
|
|
fi
|
|
|
|
|
2019-03-29 10:51:33 -04:00
|
|
|
# default to Core group if not specified otherwise
|
|
|
|
if [ ${#install_groups[*]} -eq 0 ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
install_groups=('Core')
|
2019-03-29 10:51:33 -04:00
|
|
|
fi
|
|
|
|
|
2014-01-17 08:46:11 -05:00
|
|
|
target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
|
|
|
|
|
|
|
|
set -x
|
|
|
|
|
2014-03-02 09:50:16 -05:00
|
|
|
mkdir -m 755 "$target"/dev
|
2014-02-26 22:34:42 -05:00
|
|
|
mknod -m 600 "$target"/dev/console c 5 1
|
|
|
|
mknod -m 600 "$target"/dev/initctl p
|
|
|
|
mknod -m 666 "$target"/dev/full c 1 7
|
|
|
|
mknod -m 666 "$target"/dev/null c 1 3
|
|
|
|
mknod -m 666 "$target"/dev/ptmx c 5 2
|
|
|
|
mknod -m 666 "$target"/dev/random c 1 8
|
|
|
|
mknod -m 666 "$target"/dev/tty c 5 0
|
|
|
|
mknod -m 666 "$target"/dev/tty0 c 4 0
|
|
|
|
mknod -m 666 "$target"/dev/urandom c 1 9
|
|
|
|
mknod -m 666 "$target"/dev/zero c 1 5
|
2014-01-17 08:46:11 -05:00
|
|
|
|
2015-02-18 18:07:31 -05:00
|
|
|
# amazon linux yum will fail without vars set
|
|
|
|
if [ -d /etc/yum/vars ]; then
|
|
|
|
mkdir -p -m 755 "$target"/etc/yum
|
|
|
|
cp -a /etc/yum/vars "$target"/etc/yum/
|
|
|
|
fi
|
|
|
|
|
2020-03-02 22:27:49 -05:00
|
|
|
if [[ -n "$install_groups" ]]; then
|
|
|
|
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
|
|
|
|
--setopt=group_package_types=mandatory -y groupinstall "${install_groups[@]}"
|
2015-12-20 07:20:30 -05:00
|
|
|
fi
|
|
|
|
|
2020-03-02 22:27:49 -05:00
|
|
|
if [[ -n "$install_packages" ]]; then
|
|
|
|
yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
|
|
|
|
--setopt=group_package_types=mandatory -y install "${install_packages[@]}"
|
2015-12-20 07:20:30 -05:00
|
|
|
fi
|
|
|
|
|
2014-02-12 08:02:36 -05:00
|
|
|
yum -c "$yum_config" --installroot="$target" -y clean all
|
2014-01-17 08:46:11 -05:00
|
|
|
|
2020-03-02 22:27:49 -05:00
|
|
|
cat > "$target"/etc/sysconfig/network << EOF
|
2014-01-17 08:46:11 -05:00
|
|
|
NETWORKING=yes
|
|
|
|
HOSTNAME=localhost.localdomain
|
|
|
|
EOF
|
|
|
|
|
2015-11-03 23:33:48 -05:00
|
|
|
# effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
|
2014-01-17 08:46:11 -05:00
|
|
|
# locales
|
|
|
|
rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
|
2015-11-03 23:33:48 -05:00
|
|
|
# docs and man pages
|
2014-01-17 08:46:11 -05:00
|
|
|
rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
|
|
|
|
# cracklib
|
|
|
|
rm -rf "$target"/usr/share/cracklib
|
|
|
|
# i18n
|
|
|
|
rm -rf "$target"/usr/share/i18n
|
2015-11-03 23:33:48 -05:00
|
|
|
# yum cache
|
|
|
|
rm -rf "$target"/var/cache/yum
|
|
|
|
mkdir -p --mode=0755 "$target"/var/cache/yum
|
2014-01-17 08:46:11 -05:00
|
|
|
# sln
|
|
|
|
rm -rf "$target"/sbin/sln
|
|
|
|
# ldconfig
|
2015-11-03 23:33:48 -05:00
|
|
|
rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
|
|
|
|
mkdir -p --mode=0755 "$target"/var/cache/ldconfig
|
2014-01-17 08:46:11 -05:00
|
|
|
|
2019-03-01 05:27:02 -05:00
|
|
|
if [ -z "$version" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
for file in "$target"/etc/{redhat,system}-release; do
|
|
|
|
if [ -r "$file" ]; then
|
|
|
|
version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2019-03-01 05:27:02 -05:00
|
|
|
fi
|
2014-01-17 08:46:11 -05:00
|
|
|
|
|
|
|
if [ -z "$version" ]; then
|
2020-03-02 22:27:49 -05:00
|
|
|
echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
|
|
|
|
version=$name
|
2014-01-17 08:46:11 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
tar --numeric-owner -c -C "$target" . | docker import - $name:$version
|
2015-11-03 23:33:48 -05:00
|
|
|
|
2015-12-20 07:20:30 -05:00
|
|
|
docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
|
2014-01-17 08:46:11 -05:00
|
|
|
|
|
|
|
rm -rf "$target"
|