archvortex

fluxar build.sh

Feb 18th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 9.46 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e -u
  4.  
  5. iso_name=fluxar
  6. iso_label="FLUXAR"
  7. iso_version=$(date +%Y.%m.%d)
  8. install_dir=arch
  9. arch=$(uname -m)
  10. work_dir=work
  11. out_dir=out
  12. verbose=""
  13.  
  14. script_path=$(readlink -f ${0%/*})
  15.  
  16. # Base installation (root-image)
  17. make_basefs() {
  18.     mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "base" create
  19.     mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "memtest86+ syslinux" create
  20. }
  21.  
  22. # Additional packages (root-image)
  23. make_packages() {
  24.     mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -p "$(grep -v ^# ${script_path}/packages.${arch})" create
  25. }
  26.  
  27. # Customize installation (root-image)
  28. make_customize_root_image() {
  29.     if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
  30.         cp -af ${script_path}/root-image ${work_dir}
  31.         chmod 750 ${work_dir}/root-image/etc/sudoers.d
  32.         chmod 440 ${work_dir}/root-image/etc/sudoers.d/g_wheel
  33.         chroot ${work_dir}/root-image /usr/sbin/locale-gen
  34.         chroot ${work_dir}/root-image /usr/sbin/useradd --create-home --skel /etc/skel --password "" --gid users --groups "storage,optical,audio,video,network,wheel,power,lp,log" --shell /bin/bash live
  35.         chroot ${work_dir}/root-image /bin/chown live /home/live &> /dev/null
  36.         : > ${work_dir}/build.${FUNCNAME}
  37.     fi
  38. }
  39.  
  40. # Copy mkinitcpio archiso hooks (root-image)
  41. make_setup_mkinitcpio() {
  42.    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
  43.         local _hook
  44.         for _hook in archiso; do
  45.             cp /lib/initcpio/hooks/${_hook} ${work_dir}/root-image/lib/initcpio/hooks
  46.             cp /lib/initcpio/install/${_hook} ${work_dir}/root-image/lib/initcpio/install
  47.         done
  48.         : > ${work_dir}/build.${FUNCNAME}
  49.    fi
  50. }
  51.  
  52. # Prepare ${install_dir}/boot/
  53. make_boot() {
  54.     if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
  55.         local _src=${work_dir}/root-image
  56.         local _dst_boot=${work_dir}/iso/${install_dir}/boot
  57.         mkdir -p ${_dst_boot}/${arch}
  58.         mkinitcpio \
  59.             -c ${script_path}/mkinitcpio.conf \
  60.             -b ${_src} \
  61.             -k /boot/vmlinuz-linux \
  62.             -g ${_dst_boot}/${arch}/archiso.img
  63.         mv ${_src}/boot/vmlinuz-linux ${_dst_boot}/${arch}/vmlinuz
  64.         cp ${_src}/boot/memtest86+/memtest.bin ${_dst_boot}/memtest
  65.         cp ${_src}/usr/share/licenses/common/GPL2/license.txt ${_dst_boot}/memtest.COPYING
  66.         : > ${work_dir}/build.${FUNCNAME}
  67.     fi
  68. }
  69.  
  70. # Prepare /${install_dir}/boot/syslinux
  71. make_syslinux() {
  72.     if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
  73.         local _src_syslinux=${work_dir}/root-image/usr/lib/syslinux
  74.         local _dst_syslinux=${work_dir}/iso/${install_dir}/boot/syslinux
  75.         mkdir -p ${_dst_syslinux}
  76.         sed "s|%ARCHISO_LABEL%|${iso_label}|g;
  77.             s|%INSTALL_DIR%|${install_dir}|g;
  78.             s|%ARCH%|${arch}|g" ${script_path}/syslinux/syslinux.cfg > ${_dst_syslinux}/syslinux.cfg
  79.         cp ${script_path}/syslinux/splash.png ${_dst_syslinux}
  80.         cp ${_src_syslinux}/*.c32 ${_dst_syslinux}
  81.         cp ${_src_syslinux}/*.com ${_dst_syslinux}
  82.         cp ${_src_syslinux}/*.0 ${_dst_syslinux}
  83.         cp ${_src_syslinux}/memdisk ${_dst_syslinux}
  84.         # Remove useless shortcuts
  85.         rm ${work_dir}/root-image/usr/share/applications/avahi* &>/dev/null
  86.         rm ${work_dir}/root-image/usr/share/applications/bvnc* &>/dev/null
  87.         rm ${work_dir}/root-image/usr/share/applications/bssh* &>/dev/null
  88.         rm ${work_dir}/root-image/usr/share/applications/7z* &>/dev/null
  89.         rm ${work_dir}/root-image/usr/share/applications/qv4* &>/dev/null
  90.         rm ${work_dir}/root-image/usr/share/applications/dconf-editor.desktop &>/dev/null
  91.         rm ${work_dir}/root-image/etc/xdg/autostart/nm-applet.desktop &>/dev/null
  92.         # End of removing useless shortcuts
  93.         mkdir -p ${_dst_syslinux}/hdt
  94.         wget -O - http://pciids.sourceforge.net/v2.2/pci.ids | gzip -9 > ${_dst_syslinux}/hdt/pciids.gz
  95.         cat ${work_dir}/root-image/lib/modules/*-ARCH/modules.alias | gzip -9 > ${_dst_syslinux}/hdt/modalias.gz
  96.         : > ${work_dir}/build.${FUNCNAME}
  97.     fi
  98. }
  99.  
  100. # Prepare /isolinux
  101. make_isolinux() {
  102.     if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
  103.         mkdir -p ${work_dir}/iso/isolinux
  104.         sed "s|%INSTALL_DIR%|${install_dir}|g" ${script_path}/isolinux/isolinux.cfg > ${work_dir}/iso/isolinux/isolinux.cfg
  105.         cp ${work_dir}/root-image/usr/lib/syslinux/isolinux.bin ${work_dir}/iso/isolinux/
  106.         cp ${work_dir}/root-image/usr/lib/syslinux/isohdpfx.bin ${work_dir}/iso/isolinux/
  107.         : > ${work_dir}/build.${FUNCNAME}
  108.     fi
  109. }
  110.  
  111. # Process aitab
  112. # args: $1 (core | netinstall)
  113. make_aitab() {
  114.     local _iso_type=${1}
  115.     if [[ ! -e ${work_dir}/build.${FUNCNAME}_${_iso_type} ]]; then
  116.         sed "s|%ARCH%|${arch}|g" ${script_path}/aitab.${_iso_type} > ${work_dir}/iso/${install_dir}/aitab
  117.         : > ${work_dir}/build.${FUNCNAME}_${_iso_type}
  118.     fi
  119. }
  120.  
  121. # Build all filesystem images specified in aitab (.fs .fs.sfs .sfs)
  122. make_prepare() {
  123.     mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" prepare
  124. }
  125.  
  126. # Build ISO
  127. # args: $1 (core | netinstall)
  128. make_iso() {
  129.     local _iso_type=${1}
  130.     mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" checksum
  131.     mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -L "${iso_label}" -o "${out_dir}" iso "${iso_name}-${iso_version}-${arch}.iso"
  132. }
  133.  
  134. purge_single ()
  135. {
  136.     if [[ -d ${work_dir} ]]; then
  137.         find ${work_dir} -mindepth 1 -maxdepth 1 \
  138.             ! -path ${work_dir}/iso -prune \
  139.             | xargs rm -rf
  140.     fi
  141. }
  142.  
  143. clean_single ()
  144. {
  145.     rm -rf ${work_dir}
  146.     rm -f ${out_dir}/${iso_name}-${iso_version}-*-${arch}.iso
  147. }
  148.  
  149. make_common_single() {
  150.     make_basefs
  151.     make_packages
  152.     make_customize_root_image
  153.     make_setup_mkinitcpio
  154.     make_boot
  155.     make_syslinux
  156.     make_isolinux
  157.     make_aitab $1
  158.     make_prepare $1
  159.     make_iso $1
  160. }
  161.  
  162. _usage ()
  163. {
  164.     echo "usage ${0} [options] command <command options>"
  165.     echo
  166.     echo " General options:"
  167.     echo "    -N <iso_name>      Set an iso filename (prefix)"
  168.     echo "                        Default: ${iso_name}"
  169.     echo "    -V <iso_version>   Set an iso version (in filename)"
  170.     echo "                        Default: ${iso_version}"
  171.     echo "    -L <iso_label>     Set an iso label (disk label)"
  172.     echo "                        Default: ${iso_label}"
  173.     echo "    -D <install_dir>   Set an install_dir (directory inside iso)"
  174.     echo "                        Default: ${install_dir}"
  175.     echo "    -w <work_dir>      Set the working directory"
  176.     echo "                        Default: ${work_dir}"
  177.     echo "    -o <out_dir>       Set the output directory"
  178.     echo "                        Default: ${out_dir}"
  179.     echo "    -v                 Enable verbose output"
  180.     echo "    -h                 This help message"
  181.     echo
  182.     echo " Commands:"
  183.     echo "   build <mode> <type>"
  184.     echo "      Build selected .iso by <mode> and <type>"
  185.     echo "   purge <mode>"
  186.     echo "      Clean working directory except iso/ directory of build <mode>"
  187.     echo "   clean <mode>"
  188.     echo "      Clean working directory and .iso file in output directory of build <mode>"
  189.     echo
  190.     echo " Command options:"
  191.     echo "         <mode> Valid value 'single'"
  192.     echo "         <type> Valid value 'core'"
  193.     exit ${1}
  194. }
  195.  
  196. if [[ ${EUID} -ne 0 ]]; then
  197.     echo "This script must be run as root."
  198.     _usage 1
  199. fi
  200.  
  201. while getopts 'N:V:L:D:w:o:vh' arg; do
  202.     case "${arg}" in
  203.         N) iso_name="${OPTARG}" ;;
  204.         V) iso_version="${OPTARG}" ;;
  205.         L) iso_label="${OPTARG}" ;;
  206.         D) install_dir="${OPTARG}" ;;
  207.         w) work_dir="${OPTARG}" ;;
  208.         o) out_dir="${OPTARG}" ;;
  209.         v) verbose="-v" ;;
  210.         h|?) _usage 0 ;;
  211.         *)
  212.             _msg_error "Invalid argument '${arg}'" 0
  213.             _usage 1
  214.             ;;
  215.     esac
  216. done
  217.  
  218. shift $((OPTIND - 1))
  219.  
  220. if [[ $# -lt 1 ]]; then
  221.     echo "No command specified"
  222.     _usage 1
  223. fi
  224. command_name="${1}"
  225.  
  226. if [[ $# -lt 2 ]]; then
  227.     echo "No command mode specified"
  228.     _usage 1
  229. fi
  230. command_mode="${2}"
  231.  
  232. if [[ ${command_name} == "build" ]]; then
  233.     if [[ $# -lt 3 ]]; then
  234.         echo "No build type specified"
  235.         _usage 1
  236.     fi
  237. command_type="${3}"
  238. fi
  239.  
  240. if [[ ${command_mode} == "single" ]]; then
  241.     work_dir=${work_dir}/${arch}
  242. fi
  243.  
  244. case "${command_name}" in
  245.     build)
  246.         case "${command_mode}" in
  247.             single)
  248.                 case "${command_type}" in
  249.                     core)
  250.                         make_common_single core
  251.                         ;;
  252.                     *)
  253.                         echo "Invalid build type '${command_type}'"
  254.                         _usage 1
  255.                         ;;
  256.                 esac
  257.                 ;;
  258.             *)
  259.                 echo "Invalid build mode '${command_mode}'"
  260.                 _usage 1
  261.                 ;;
  262.         esac
  263.         ;;
  264.     purge)
  265.         case "${command_mode}" in
  266.             single)
  267.                 purge_single
  268.                 ;;
  269.             *)
  270.                 echo "Invalid purge mode '${command_mode}'"
  271.                 _usage 1
  272.                 ;;
  273.         esac
  274.         ;;
  275.     clean)
  276.         case "${command_mode}" in
  277.             single)
  278.                 clean_single
  279.                 ;;
  280.             *)
  281.                 echo "Invalid clean mode '${command_mode}'"
  282.                 _usage 1
  283.                 ;;
  284.         esac
  285.         ;;
  286.     *)
  287.         echo "Invalid command name '${command_name}'"
  288.         _usage 1
  289.         ;;
  290. esac
Advertisement
Add Comment
Please, Sign In to add comment