From 7aa08d6583be57195d53f3b7337b6264ff0d600f Mon Sep 17 00:00:00 2001 From: Mhd Sulhan Date: Fri, 29 Jan 2016 12:17:54 +0700 Subject: 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 --- scripts/_bootstrap_post.sh | 6 ++ scripts/_bootstrap_script.sh | 137 +++++++++++++++++++++++++++++++++++++++++++ scripts/bootstrap.sh | 107 --------------------------------- scripts/create_image.sh | 2 - scripts/create_rootfs.sh | 1 - scripts/rootfs.sh | 61 ++++++++++++++++--- 6 files changed, 196 insertions(+), 118 deletions(-) create mode 100755 scripts/_bootstrap_post.sh create mode 100755 scripts/_bootstrap_script.sh delete mode 100755 scripts/bootstrap.sh (limited to 'scripts') 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_script.sh b/scripts/_bootstrap_script.sh new file mode 100755 index 0000000..341ab52 --- /dev/null +++ b/scripts/_bootstrap_script.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +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 | + xargs file | + sed -n '/executable .*not stripped/s/: TAB .*//p' | + xargs -rt strip --strip-unneeded +} + +strip_lib() { + find /usr/lib -type f \( -perm -0100 \) -print | + xargs file | + sed -n '/executable .*not stripped/s/: TAB .*//p' | + xargs -rt strip --strip-unneeded +} + +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/* + rm -rf /usr/share/man/* + rm -rf /usr/share/info/* + rm -rf /var/cache/pacman/pkg/* + rm -rf /var/log/* + rm -f /vars.sh +} + +bootstrap_hostname() { + echo "" + echo "==> set hostname to '${HOSTNAME}' ..." + echo ${HOSTNAME} > /etc/hostname +} + +bootstrap_timezone() { + echo "" + echo "==> set timezone to '${TIMEZONE}'..." + cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime +} + +bootstrap_locales() { + echo "" + echo "==> set locales ..." + + echo "#" > /etc/locale.gen + for i in "${LOCALES[@]}"; do + echo "$i" >> /etc/locale.gen + done + + echo "==> generate locale ..." + /usr/bin/locale-gen + + echo "==> set locale preferences ..." + echo "LANG=${BOOT_LANG}" > "$rootfs"/etc/locale.conf + echo "LC_MESSAGES=C" >> "$rootfs"/etc/locale.conf +} + +bootstrap_remove_packages() { + echo "" + echo "==> remove unneeded packages ..." + for pkg in ${PKGS_REMOVED[@]}; do + echo " removing $pkg" + pacman -Rdd --noconfirm $pkg + done +} + +bootstrap_clean_base() { + echo "" + echo "==> cleaning base ..." + ## Remove all charmaps except UTF-8. + find /usr/share/i18n/charmaps/ \! -name "UTF-8.gz" -delete + ## Remove all locales except en_GB and en_US. + find /usr/share/i18n/locales/ \! -name "en_GB" \! -name "en_US" -delete + ## Remove all terminfo excetp ansi,cygwin,linux,screen-256color,vt100,vt220, + ## and xterm. + find /usr/share/terminfo/ \ + \! -name ansi \ + \! -name cygwin \ + \! -name linux \ + \! -name screen-256color \ + \! -name vt100 \ + \! -name vt220 \ + \! -name xterm \ + -delete + ## Remove all unneeded doc. + rm -rf /usr/share/texinfo/* + rm -rf /usr/share/zoneinfo/* + rm -rf /usr/share/iana-etc/* + rm -rf /usr/share/gtk-doc/* + rm -rf /usr/share/readline/* + + rm -r /usr/share/icu/* + 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/bootstrap.sh b/scripts/bootstrap.sh deleted file mode 100755 index c186b78..0000000 --- a/scripts/bootstrap.sh +++ /dev/null @@ -1,107 +0,0 @@ -#!/bin/bash - -export LANG=C.UTF-8 -export HOSTNAME="arch-base" -export BOOT_LANG=en_GB.UTF-8 -export PKG_REMOVED=() - -strip_bin() { - find /usr/bin -type f \( -perm -0100 \) -print | - xargs file | - sed -n '/executable .*not stripped/s/: TAB .*//p' | - xargs -rt strip --strip-unneeded -} - -strip_lib() { - find /usr/lib -type f \( -perm -0100 \) -print | - xargs file | - sed -n '/executable .*not stripped/s/: TAB .*//p' | - xargs -rt strip --strip-unneeded -} - -bootstrap_clean_common() { - echo "==> cleaning ..." - strip_bin - strip_lib - rm -rf /usr/share/doc/* - rm -rf /usr/share/licenses/* - rm -rf /usr/share/locale/* - rm -rf /usr/share/man/* - 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 ${HOSTNAME} > /etc/hostname -} - -bootstrap_timezone() { - echo "==> set timezone to UTC ..." - cp /usr/share/zoneinfo/UTC /etc/localtime -} - -bootstrap_locales() { - echo "==> set locales ..." - echo "en_GB.UTF-8 UTF-8" > /etc/locale.gen - echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen - - echo "==> generate locale ..." - /usr/bin/locale-gen - - echo "==> set locale preferences ..." - echo "LANG=${BOOT_LANG}" > "$rootfs"/etc/locale.conf - echo "LC_MESSAGES=C" >> "$rootfs"/etc/locale.conf -} - -bootstrap_remove_packages() { - echo "==> remove unneeded packages ..." - for pkg in ${PKGS_REMOVED[@]}; do - echo " removing $pkg" - pacman -Rdd --noconfirm $pkg - done -} - -bootstrap_clean_base() { - echo "==> cleaning base ..." - ## Remove all charmaps except UTF-8. - find /usr/share/i18n/charmaps/ \! -name "UTF-8.gz" -delete - ## Remove all locales except en_GB and en_US. - find /usr/share/i18n/locales/ \! -name "en_GB" \! -name "en_US" -delete - ## Remove all terminfo excetp ansi,cygwin,linux,screen-256color,vt100,vt220, - ## and xterm. - find /usr/share/terminfo/ \ - \! -name ansi \ - \! -name cygwin \ - \! -name linux \ - \! -name screen-256color \ - \! -name vt100 \ - \! -name vt220 \ - \! -name xterm \ - -delete - ## Remove all unneeded doc. - rm -rf /usr/share/texinfo/* - rm -rf /usr/share/zoneinfo/* - 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 - - 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 '{}' \; -} 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} -- cgit v1.3