[arch-releng] [RFC] [PATCH 1/3] [archiso] Implement own chroot functions in mkarchiso

Gerardo Exequiel Pozzi vmlinuz386 at yahoo.com.ar
Fri Mar 16 01:26:35 EDT 2012


* Remove devtools dependency.
* Better control over what files are touched inside chroot (root-image).
  Now: NONE :)
* Two new commands:
  + init: To install {base} group and other needed packages (syslinux for now)
  + run: If we want to run some command inside chroot
         (mkinitcpio, locale-gen, useradd, etc etc...)
* Renamed command: "created" to "install", says much better what does.

Signed-off-by: Gerardo Exequiel Pozzi <vmlinuz386 at yahoo.com.ar>
---
 archiso/mkarchiso         |  103 +++++++++++++++++++++++++++++++++++++--------
 configs/baseline/build.sh |    7 +--
 configs/releng/build.sh   |   19 ++++++---
 3 files changed, 102 insertions(+), 27 deletions(-)

diff --git a/archiso/mkarchiso b/archiso/mkarchiso
index b3aa5cd..df3d354 100755
--- a/archiso/mkarchiso
+++ b/archiso/mkarchiso
@@ -5,6 +5,7 @@ set -e -u
 app_name=${0##*/}
 arch=$(uname -m)
 pkg_list=""
+run_cmd=""
 quiet="y"
 pacman_conf="/etc/pacman.conf"
 export iso_label="ARCH_$(date +%Y%m)"
@@ -44,6 +45,44 @@ _show_space_usage () {
     _msg_info "Total: ${_total} MiB (100%) | Used: ${_used} MiB (${_pct_u}) | Avail: ${_avail} MiB ($((100 - ${_pct_u%\%}))%)"
 }
 
+_chroot_mount () {
+    mount -t devtmpfs dev "${work_dir}/root-image/dev"
+    mount -t devpts devpts "${work_dir}/root-image/dev/pts"
+    mount -t tmpfs devshm "${work_dir}/root-image/dev/shm"
+    mount -t proc proc "${work_dir}/root-image/proc"
+    mount -t tmpfs run "${work_dir}/root-image/run"
+    mount -t sysfs sys "${work_dir}/root-image/sys"
+    mount -t tmpfs tmp "${work_dir}/root-image/tmp"
+
+    trap '_chroot_umount' EXIT HUP INT TERM
+}
+
+_chroot_umount () {
+    umount "${work_dir}/root-image/tmp"
+    umount "${work_dir}/root-image/sys"
+    umount "${work_dir}/root-image/run"
+    umount "${work_dir}/root-image/proc"
+    umount "${work_dir}/root-image/dev/shm"
+    umount "${work_dir}/root-image/dev/pts"
+    umount "${work_dir}/root-image/dev"
+
+    trap - EXIT HUP INT TERM
+}
+
+_chroot_init() {
+    if [[ ! -d ${work_dir}/root-image/var/cache/pacman ]]; then
+        mkdir -p ${work_dir}/root-image/{dev,proc,run,sys,tmp,var/lib/pacman}
+        _pacman "base"
+        _pacman "syslinux"
+    fi
+}
+
+_chroot_run() {
+    _chroot_mount
+    eval chroot ${work_dir}/root-image "${run_cmd}"
+    _chroot_umount
+}
+
 # Mount a filesystem (trap signals in case of error for unmounting it
 # $1: source image
 # $2: mount-point
@@ -99,6 +138,7 @@ _usage ()
     echo "usage ${app_name} [options] command <command options>"
     echo " general options:"
     echo "    -p PACKAGE(S)    Package(s) to install, can be used multiple times"
+    echo "    -r <command>     Run <command> inside root-image"
     echo "    -C <file>        Config file for pacman. Default ${pacman_conf}"
     echo "    -L <label>       Set a label for the disk"
     echo "    -P <publisher>   Set a publisher for the disk"
@@ -113,9 +153,12 @@ _usage ()
     echo "    -v               Enable verbose output"
     echo "    -h               This message"
     echo " commands:"
-    echo "   create"
-    echo "      create a base directory layout to work with"
-    echo "      includes all specified packages"
+    echo "   init"
+    echo "      Make base layout and install base group"
+    echo "   install"
+    echo "      Install all specified packages (-p)"
+    echo "   run"
+    echo "      run command specified by -r"
     echo "   prepare"
     echo "      build all images"
     echo "   checksum"
@@ -126,7 +169,7 @@ _usage ()
 }
 
 # Shows configuration according to command mode.
-# $1: create | prepare | iso
+# $1: init | install | run | prepare | checksum | iso
 _show_config () {
     local _mode="$1"
     echo
@@ -136,10 +179,16 @@ _show_config () {
     _msg_info "        Working directory:   ${work_dir}"
     _msg_info "   Installation directory:   ${install_dir}"
     case "${_mode}" in
-        create)
+        init)
+            _msg_info "       Pacman config file:   ${pacman_conf}"
+            ;;
+        install)
             _msg_info "       Pacman config file:   ${pacman_conf}"
             _msg_info "                 Packages:   ${pkg_list}"
             ;;
+        run)
+            _msg_info "              Run command:   ${run_cmd}"
+            ;;
         prepare)
             ;;
         checksum)
@@ -159,20 +208,23 @@ _pacman ()
 {
     _msg_info "Installing packages to '${work_dir}/root-image/'..."
 
+    _chroot_mount
+
     if [[ "${quiet}" = "y" ]]; then
-        mkarchroot -n -C "${pacman_conf}" -f "${work_dir}/root-image" $* &> /dev/null
+        pacman -Sy -r "${work_dir}/root-image" --config "${pacman_conf}" --needed --noconfirm $* &> /dev/null
     else
-        mkarchroot -n -C "${pacman_conf}" -f "${work_dir}/root-image" $*
+        pacman -Sy -r "${work_dir}/root-image" --config "${pacman_conf}" --needed --noconfirm $*
     fi
 
-    # Cleanup
-    find "${work_dir}" -name "*.pacnew" -name "*.pacsave" -name "*.pacorig" -delete
+    _chroot_umount
+
     _msg_info "Packages installed successfully!"
 }
 
 # Cleanup root-image
 _cleanup () {
     _msg_info "Cleaning up what we can on root-image"
+
     # remove the initcpio images that were generated for the host system
     if [[ -d "${work_dir}/root-image/boot" ]]; then 
         find "${work_dir}/root-image/boot" -name '*.img' -delete
@@ -201,6 +253,8 @@ _cleanup () {
     if [[ -d "${work_dir}/root-image/tmp" ]]; then
         find "${work_dir}/root-image/tmp" -mindepth 1 -delete
     fi
+    # Delete package pacman related files.
+    find "${work_dir}" -name "*.pacnew" -name "*.pacsave" -name "*.pacorig" -delete
     # Create etc/mtab if not is a symlink.
     if [[ ! -L "${work_dir}/root-image/etc/mtab" ]]; then
         ln -sf "/proc/self/mounts" "${work_dir}/root-image/etc/mtab"
@@ -384,7 +438,7 @@ command_prepare () {
 
 # Install packages on root-image.
 # A basic check to avoid double execution/reinstallation is done via hashing package names.
-command_create () {
+command_install () {
     if [[ ! -f "${pacman_conf}" ]]; then
         _msg_error "Pacman config file '${pacman_conf}' does not exist" 1
     fi
@@ -397,27 +451,36 @@ command_create () {
         _usage 1
     fi
 
-    _show_config create
+    _show_config install
 
     local _pkg_list_hash
     _pkg_list_hash=$(echo ${pkg_list} | sort -u | md5sum | cut -c1-32)
-    if [[ -f "${work_dir}/create.${_pkg_list_hash}" ]]; then
+    if [[ -f "${work_dir}/install.${_pkg_list_hash}" ]]; then
         _msg_info "These packages are already installed, skipping."
     else
-        mkdir -p "${work_dir}/root-image/"
         _pacman "${pkg_list}"
-        : > "${work_dir}/create.${_pkg_list_hash}"
+        : > "${work_dir}/install.${_pkg_list_hash}"
     fi
 }
 
+command_init() {
+    _show_config init
+    _chroot_init
+}
+
+command_run() {
+    _show_config run
+    _chroot_run
+}
 
 if [[ ${EUID} -ne 0 ]]; then
     _msg_error "This script must be run as root." 1
 fi
 
-while getopts 'p:C:L:P:A:D:w:o:vh' arg; do
+while getopts 'p:r:C:L:P:A:D:w:o:vh' arg; do
     case "${arg}" in
         p) pkg_list="${pkg_list} ${OPTARG}" ;;
+        r) run_cmd="${OPTARG}" ;;
         C) pacman_conf="${OPTARG}" ;;
         L) iso_label="${OPTARG}" ;;
         P) iso_publisher="${OPTARG}" ;;
@@ -443,8 +506,14 @@ fi
 command_name="${1}"
 
 case "${command_name}" in
-    create)
-        command_create
+    init)
+        command_init
+        ;;
+    install)
+        command_install
+        ;;
+    run)
+        command_run
         ;;
     prepare)
         command_prepare
diff --git a/configs/baseline/build.sh b/configs/baseline/build.sh
index 1f2251d..f7f7abf 100755
--- a/configs/baseline/build.sh
+++ b/configs/baseline/build.sh
@@ -15,8 +15,7 @@ script_path=$(readlink -f ${0%/*})
 
 # Base installation (root-image)
 make_basefs() {
-    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "base" create
-    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "syslinux" create
+    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" init
 }
 
 # Copy mkinitcpio archiso hooks (root-image)
@@ -33,7 +32,9 @@ make_setup_mkinitcpio() {
 make_boot() {
     if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
         mkdir -p ${work_dir}/iso/${install_dir}/boot/${arch}
-        mkarchroot -n -r "mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/vmlinuz-linux -g /boot/archiso.img" ${work_dir}/root-image
+        mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" \
+            -r 'mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/vmlinuz-linux -g /boot/archiso.img' \
+            run
         cp ${work_dir}/root-image/boot/archiso.img ${work_dir}/iso/${install_dir}/boot/${arch}/archiso.img
         cp ${work_dir}/root-image/boot/vmlinuz-linux ${work_dir}/iso/${install_dir}/boot/${arch}/vmlinuz
         : > ${work_dir}/build.${FUNCNAME}
diff --git a/configs/releng/build.sh b/configs/releng/build.sh
index 42836a9..f206caf 100755
--- a/configs/releng/build.sh
+++ b/configs/releng/build.sh
@@ -15,13 +15,13 @@ script_path=$(readlink -f ${0%/*})
 
 # Base installation (root-image)
 make_basefs() {
-    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "base" create
-    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "memtest86+ syslinux mkinitcpio-nfs-utils nbd curl" create
+    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" init
+    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "memtest86+ mkinitcpio-nfs-utils nbd curl" install
 }
 
 # Additional packages (root-image)
 make_packages() {
-    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "$(grep -v ^# ${script_path}/packages.${arch})" create
+    mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "$(grep -v ^# ${script_path}/packages.${arch})" install
 }
 
 # Copy mkinitcpio archiso hooks (root-image)
@@ -46,7 +46,9 @@ make_boot() {
         local _src=${work_dir}/root-image
         local _dst_boot=${work_dir}/iso/${install_dir}/boot
         mkdir -p ${_dst_boot}/${arch}
-        mkarchroot -n -r "mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/vmlinuz-linux -g /boot/archiso.img" ${_src}
+        mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" \
+            -r 'mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/vmlinuz-linux -g /boot/archiso.img' \
+            run
         mv ${_src}/boot/archiso.img ${_dst_boot}/${arch}/archiso.img
         mv ${_src}/boot/vmlinuz-linux ${_dst_boot}/${arch}/vmlinuz
         cp ${_src}/boot/memtest86+/memtest.bin ${_dst_boot}/memtest
@@ -90,7 +92,6 @@ make_isolinux() {
 }
 
 # Customize installation (root-image)
-# NOTE: mkarchroot should not be executed after this function is executed, otherwise will overwrites some custom files.
 make_customize_root_image() {
     if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
         cp -af ${script_path}/root-image ${work_dir}
@@ -99,8 +100,12 @@ make_customize_root_image() {
         mkdir -p ${work_dir}/root-image/etc/pacman.d
         wget -O ${work_dir}/root-image/etc/pacman.d/mirrorlist http://www.archlinux.org/mirrorlist/all/
         sed -i "s/#Server/Server/g" ${work_dir}/root-image/etc/pacman.d/mirrorlist
-        chroot ${work_dir}/root-image /usr/sbin/locale-gen
-        chroot ${work_dir}/root-image /usr/sbin/useradd -m -p "" -g users -G "audio,disk,optical,wheel" arch
+        mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" \
+            -r 'locale-gen' \
+            run
+        mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" \
+            -r 'useradd -m -p "" -g users -G "audio,disk,optical,wheel" arch' \
+            run
         : > ${work_dir}/build.${FUNCNAME}
     fi
 }
-- 
1.7.9.4



More information about the arch-releng mailing list