aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMhd Sulhan <ms@kilabit.info>2016-01-29 12:17:54 +0700
committerMhd Sulhan <ms@kilabit.info>2016-01-29 12:17:54 +0700
commit7aa08d6583be57195d53f3b7337b6264ff0d600f (patch)
treeb99117e9a497f285bf4660f4ca568ef7bd3414ec
parent4da1e3686ba189e56f8a410172eaa3b460a033b4 (diff)
downloadarch-docker-7aa08d6583be57195d53f3b7337b6264ff0d600f.tar.xz
Simplify user rootfs scripts.
- Simplify bootstrap, handle copy and run internally in rootfs script. - create_rootfs only run one function, which is rootfs_main. - create_image only run one function, which is rootfs_to_docker. - bootstraping through one function - allow replacing hostname, locales, and timezone through vars.sh
-rwxr-xr-xarch-base/bootstrap_base.sh15
-rwxr-xr-xarch-base/vars.sh17
-rwxr-xr-xscripts/_bootstrap_post.sh6
-rwxr-xr-xscripts/_bootstrap_script.sh (renamed from scripts/bootstrap.sh)58
-rwxr-xr-xscripts/create_image.sh2
-rwxr-xr-xscripts/create_rootfs.sh1
-rwxr-xr-xscripts/rootfs.sh61
7 files changed, 114 insertions, 46 deletions
diff --git a/arch-base/bootstrap_base.sh b/arch-base/bootstrap_base.sh
deleted file mode 100755
index 05358f5..0000000
--- a/arch-base/bootstrap_base.sh
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/bash
-
-echo "==> bootstraping base ..."
-
-. /bootstrap.sh
-
-bootstrap_hostname
-
-bootstrap_timezone
-
-bootstrap_locales
-
-bootstrap_clean_common
-
-bootstrap_clean_base
diff --git a/arch-base/vars.sh b/arch-base/vars.sh
index 77644d4..a72e6e4 100755
--- a/arch-base/vars.sh
+++ b/arch-base/vars.sh
@@ -1,12 +1,17 @@
#!/bin/zsh
-THISD=${0:a:h}
+## Default environment if its not set
+export HOSTNAME="arch-base"
+#export LANG=C.UTF-8
+#export BOOT_LANG=en_GB.UTF-8
+#export TIMEZONE=UTC
+#export LOCALES=(
+# "en_GB.UTF-8 UTF-8"
+# "en_US.UTF-8 UTF-8"
+#)
PKGS=(coreutils binutils findutils sed gzip file)
-PKGS_REMOVED+=(file gzip sed findutils less bzip2 pcre binutils perl db gdbm linux-api-headers)
+PKGS_REMOVED=(file gzip sed findutils less bzip2 pcre binutils perl db gdbm linux-api-headers)
+
IMAGE_NAME="sulhan/arch-base"
IMAGE_ARGS=()
-
-FILES+=("${THISD}/bootstrap_base.sh" "${ROOTFS}/")
-
-BOOTSTRAP_S+=("/bootstrap_base.sh")
diff --git a/scripts/_bootstrap_post.sh b/scripts/_bootstrap_post.sh
new file mode 100755
index 0000000..7123095
--- /dev/null
+++ b/scripts/_bootstrap_post.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+. /vars.sh
+. /_bootstrap_script.sh
+
+bootstrap_post_main
diff --git a/scripts/bootstrap.sh b/scripts/_bootstrap_script.sh
index c186b78..341ab52 100755
--- a/scripts/bootstrap.sh
+++ b/scripts/_bootstrap_script.sh
@@ -3,7 +3,12 @@
export LANG=C.UTF-8
export HOSTNAME="arch-base"
export BOOT_LANG=en_GB.UTF-8
+export TIMEZONE=UTC
export PKG_REMOVED=()
+declare -a LOCALES=(
+ "en_GB.UTF-8 UTF-8"
+ "en_US.UTF-8 UTF-8"
+)
strip_bin() {
find /usr/bin -type f \( -perm -0100 \) -print |
@@ -20,9 +25,13 @@ strip_lib() {
}
bootstrap_clean_common() {
+ echo ""
echo "==> cleaning ..."
+ echo ">>> stripping binaries"
strip_bin
+ echo ">>> stripping libraries"
strip_lib
+
rm -rf /usr/share/doc/*
rm -rf /usr/share/licenses/*
rm -rf /usr/share/locale/*
@@ -30,26 +39,29 @@ bootstrap_clean_common() {
rm -rf /usr/share/info/*
rm -rf /var/cache/pacman/pkg/*
rm -rf /var/log/*
- rm -f /run_bootstrap.sh
- rm -f /bootstrap_base.sh
- rm -f /bootstrap.sh
rm -f /vars.sh
}
bootstrap_hostname() {
- echo "==> set hostname ..."
+ echo ""
+ echo "==> set hostname to '${HOSTNAME}' ..."
echo ${HOSTNAME} > /etc/hostname
}
bootstrap_timezone() {
- echo "==> set timezone to UTC ..."
- cp /usr/share/zoneinfo/UTC /etc/localtime
+ echo ""
+ echo "==> set timezone to '${TIMEZONE}'..."
+ cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
}
bootstrap_locales() {
+ echo ""
echo "==> set locales ..."
- echo "en_GB.UTF-8 UTF-8" > /etc/locale.gen
- echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
+
+ echo "#" > /etc/locale.gen
+ for i in "${LOCALES[@]}"; do
+ echo "$i" >> /etc/locale.gen
+ done
echo "==> generate locale ..."
/usr/bin/locale-gen
@@ -60,6 +72,7 @@ bootstrap_locales() {
}
bootstrap_remove_packages() {
+ echo ""
echo "==> remove unneeded packages ..."
for pkg in ${PKGS_REMOVED[@]}; do
echo " removing $pkg"
@@ -68,6 +81,7 @@ bootstrap_remove_packages() {
}
bootstrap_clean_base() {
+ echo ""
echo "==> cleaning base ..."
## Remove all charmaps except UTF-8.
find /usr/share/i18n/charmaps/ \! -name "UTF-8.gz" -delete
@@ -90,18 +104,34 @@ bootstrap_clean_base() {
rm -rf /usr/share/iana-etc/*
rm -rf /usr/share/gtk-doc/*
rm -rf /usr/share/readline/*
-}
-bootstrap_clean_nodejs() {
- echo "==> cleaning nodejs ..."
rm -r /usr/share/icu/*
- rm -r /usr/lib/node_modules/npm/doc/*
- rm -r /usr/lib/node_modules/npm/html/doc/*
- rm -r /usr/lib/node_modules/npm/man/*
rm -rf /usr/lib/python2.7/test
rm -rf /usr/share/perl5
+ echo "==> cleaning nodejs ..."
+ rm -r /usr/lib/node_modules/npm/doc/*
+ rm -r /usr/lib/node_modules/npm/html/doc/*
+ rm -r /usr/lib/node_modules/npm/man/*
find /usr/lib/node_modules -name man -type d -exec rm -rf '{}' \;
find /usr/lib/node_modules -name doc -type d -exec rm -rf '{}' \;
find /usr/lib/node_modules -name html -type d -exec rm -rf '{}' \;
}
+
+bootstrap_clean_myself() {
+ echo ""
+ echo "==> bootstrap: cleaning my self"
+ rm -f /_bootstrap_script.sh
+ rm -f /_bootstrap_post.sh
+ rm -f /run_bootstrap.sh
+}
+
+bootstrap_post_main() {
+ echo "==> post bootstrap ..."
+ bootstrap_hostname
+ bootstrap_timezone
+ bootstrap_locales
+ bootstrap_clean_common
+ bootstrap_clean_base
+ bootstrap_clean_myself
+}
diff --git a/scripts/create_image.sh b/scripts/create_image.sh
index 938fe4d..131028a 100755
--- a/scripts/create_image.sh
+++ b/scripts/create_image.sh
@@ -3,6 +3,4 @@
. ../scripts/rootfs.sh
. ./vars.sh
-rootfs_clean_pacman
-
rootfs_to_docker ${IMAGE_NAME} ${IMAGE_ARGS[@]}
diff --git a/scripts/create_rootfs.sh b/scripts/create_rootfs.sh
index ea6e768..100a23f 100755
--- a/scripts/create_rootfs.sh
+++ b/scripts/create_rootfs.sh
@@ -4,5 +4,4 @@
. ../arch-base/vars.sh
. ./vars.sh
-rootfs_must_root
rootfs_main
diff --git a/scripts/rootfs.sh b/scripts/rootfs.sh
index 608c00c..955aef9 100755
--- a/scripts/rootfs.sh
+++ b/scripts/rootfs.sh
@@ -3,7 +3,7 @@
## Get script directory.
export SCRIPTD=${0:a:h}
-export ROOTFS=arch-rootfs
+export ROOTFS="arch-rootfs"
export ROOTFS_SIZE=400M
## List of packages to be installed
@@ -20,7 +20,13 @@ typeset -A FILES
## docker image.
typeset -A IMAGE_FILES_BAK
-FILES=(${SCRIPTD}/bootstrap.sh ${ROOTFS}/)
+## Files that needed for bootstraping the rootfs.
+typeset -A _FILES
+_FILES=("${SCRIPTD}/_bootstrap_script.sh" "${ROOTFS}/")
+_FILES+=("${SCRIPTD}/_bootstrap_post.sh" "${ROOTFS}/")
+
+_BOOTSTRAP_SCRIPT="/_bootstrap_script.sh"
+_BOOTSTRAP_POST="/_bootstrap_post.sh"
rootfs_must_root() {
if [[ $EUID != 0 ]]; then
@@ -30,12 +36,14 @@ rootfs_must_root() {
}
rootfs_create() {
- echo "==> create rootfs ${ROOTFS}"
+ echo ""
+ echo "==> create rootfs '${ROOTFS}'"
mkdir -p $ROOTFS
}
rootfs_mount() {
- echo "==> mounting ${ROOTFS} as tmpfs"
+ echo ""
+ echo "==> mounting '${ROOTFS}' as tmpfs"
## safety first, make sure we do not mount rootfs recursively
umount -R "$ROOTFS"
mount -t tmpfs -o size=${ROOTFS_SIZE} tmpfs "$ROOTFS"
@@ -50,8 +58,15 @@ rootfs_install() {
}
rootfs_copy() {
+ echo ""
echo "==> copying files ..."
+ for k in "${(@k)_FILES}"; do
+ echo " from $k to $_FILES[$k]"
+ cp $k $_FILES[$k]
+ chown root:root $_FILES[$k]
+ done
+
for k in "${(@k)FILES}"; do
echo " from $k to $FILES[$k]"
if [ -h $k ]; then
@@ -71,19 +86,31 @@ rootfs_bootstrap() {
RUN_BOOTSTRAP="${ROOTFS}/run_bootstrap.sh"
VAR_BOOTSTRAP="${ROOTFS}/vars.sh"
- echo "==> bootstraping ... ${RUN_BOOTSTRAP}"
+ echo ""
+ echo "==> creating bootstrap script '${RUN_BOOTSTRAP}'"
## generate vars for bootstrap
echo '#!/bin/bash' > ${VAR_BOOTSTRAP}
- echo "PKGS_REMOVED=($PKGS_REMOVED)" >> ${VAR_BOOTSTRAP}
## generate bootstrap script.
echo '#!/bin/bash' > ${RUN_BOOTSTRAP}
echo '. ./vars.sh' >> ${RUN_BOOTSTRAP}
- for (( i = 1; i <= ${#BOOTSTRAP_S}; i++ )) do
- echo ". $BOOTSTRAP_S[$i]" >> ${RUN_BOOTSTRAP}
+ echo ". $_BOOTSTRAP_SCRIPT" >> ${RUN_BOOTSTRAP}
+
+ for (( i = 1; i <= ${#BOOTSTRAP_SCRIPTS}; i++ )) do
+ echo ". $BOOTSTRAP_SCRIPTS[$i]" >> ${RUN_BOOTSTRAP}
done
+
+ echo ". $_BOOTSTRAP_POST" >> ${RUN_BOOTSTRAP}
+
+ ## User variables at the end to replace default values.
+ if [ -f ${PWD}/vars.sh ]; then
+ echo ">>> User variables:"
+ cat ${PWD}/vars.sh >> ${VAR_BOOTSTRAP}
+ cat ${VAR_BOOTSTRAP}
+ fi
+
chmod +x ${RUN_BOOTSTRAP}
## run the bootstrap script.
@@ -92,6 +119,7 @@ rootfs_bootstrap() {
}
rootfs_uninstall() {
+ echo ""
echo "==> uninstalling packages ..."
if [[ ${#PKGS_REMOVED} > 0 ]]; then
pacman -r "$ROOTFS" -Rdd --noconfirm ${PKGS_REMOVED}
@@ -99,6 +127,7 @@ rootfs_uninstall() {
}
rootfs_clean_pacman() {
+ echo ""
echo "==> remove pacman db and local ..."
rm -rf "${ROOTFS}/var/lib/pacman/sync/*"
rm -rf "${ROOTFS}/var/lib/pacman/local/*"
@@ -113,6 +142,10 @@ rootfs_clean_pacman() {
## (6) run bootstrap script in new root fs.
##
rootfs_main() {
+ echo "=============================================="
+ echo " ARCH DOCKER: minimalis docker image creation"
+ echo "=============================================="
+ rootfs_must_root
rootfs_clean
rootfs_create
rootfs_mount
@@ -120,9 +153,14 @@ rootfs_main() {
rootfs_copy
rootfs_bootstrap
rootfs_uninstall
+ echo ""
+ echo "==> FINISHED"
+ echo "==> Total size of rootfs:"
+ du -sch $ROOTFS
}
rootfs_backup() {
+ echo ""
echo "==> creating backups ..."
for k in "${(@k)IMAGE_FILES_BAK}"; do
@@ -139,8 +177,12 @@ rootfs_backup() {
## Convert rootfs to docker image.
##
rootfs_to_docker() {
+ rootfs_clean_pacman
rootfs_backup
+ echo ""
+ echo "==> creating docker image '$1' with args '${@:2}' ..."
+
sudo tar --numeric-owner --xattrs --acls -C "$ROOTFS" -c . |
docker import ${@:2} - $1
}
@@ -149,6 +191,9 @@ rootfs_to_docker() {
## Unmount and remove rootfs.
##
rootfs_clean() {
+ echo ""
+ echo "==> unmounting and cleaning previous rootfs ..."
+
sudo umount -R $ROOTFS
rm -f ${ROOTFS}/*
rmdir ${ROOTFS}