A fully functional server installation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
12KB

  1. #!/bin/bash
  2. set -e -u
  3. iso_name=archlinux
  4. iso_label="ARCH_$(date +%Y%m)"
  5. iso_publisher="Arch Linux <http://www.archlinux.org>"
  6. iso_application="Arch Linux Live/Rescue CD"
  7. iso_version=$(date +%Y.%m.%d)
  8. install_dir=arch
  9. work_dir=work
  10. out_dir=out
  11. gpg_key=
  12. verbose=""
  13. script_path=$(readlink -f ${0%/*})
  14. umask 0022
  15. _usage ()
  16. {
  17. echo "usage ${0} [options]"
  18. echo
  19. echo " General options:"
  20. echo " -N <iso_name> Set an iso filename (prefix)"
  21. echo " Default: ${iso_name}"
  22. echo " -V <iso_version> Set an iso version (in filename)"
  23. echo " Default: ${iso_version}"
  24. echo " -L <iso_label> Set an iso label (disk label)"
  25. echo " Default: ${iso_label}"
  26. echo " -P <publisher> Set a publisher for the disk"
  27. echo " Default: '${iso_publisher}'"
  28. echo " -A <application> Set an application name for the disk"
  29. echo " Default: '${iso_application}'"
  30. echo " -D <install_dir> Set an install_dir (directory inside iso)"
  31. echo " Default: ${install_dir}"
  32. echo " -w <work_dir> Set the working directory"
  33. echo " Default: ${work_dir}"
  34. echo " -o <out_dir> Set the output directory"
  35. echo " Default: ${out_dir}"
  36. echo " -v Enable verbose output"
  37. echo " -h This help message"
  38. exit ${1}
  39. }
  40. # Helper function to run make_*() only one time per architecture.
  41. run_once() {
  42. if [[ ! -e ${work_dir}/build.${1} ]]; then
  43. $1
  44. touch ${work_dir}/build.${1}
  45. fi
  46. }
  47. # Setup custom pacman.conf with current cache directories.
  48. make_pacman_conf() {
  49. local _cache_dirs
  50. _cache_dirs=($(pacman -v 2>&1 | grep '^Cache Dirs:' | sed 's/Cache Dirs:\s*//g'))
  51. sed -r "s|^#?\\s*CacheDir.+|CacheDir = $(echo -n ${_cache_dirs[@]})|g" ${script_path}/pacman.conf > ${work_dir}/pacman.conf
  52. }
  53. # Base installation, plus needed packages (airootfs)
  54. make_basefs() {
  55. mkarchiso ${verbose} -w "${work_dir}/x86_64" -C "${work_dir}/pacman.conf" -D "${install_dir}" init
  56. mkarchiso ${verbose} -w "${work_dir}/x86_64" -C "${work_dir}/pacman.conf" -D "${install_dir}" -p "haveged intel-ucode amd-ucode memtest86+ mkinitcpio-nfs-utils nbd zsh efitools" install
  57. }
  58. # Additional packages (airootfs)
  59. make_packages() {
  60. mkarchiso ${verbose} -w "${work_dir}/x86_64" -C "${work_dir}/pacman.conf" -D "${install_dir}" -p "$(grep -h -v ^# ${script_path}/packages.x86_64)" install
  61. }
  62. # Copy mkinitcpio archiso hooks and build initramfs (airootfs)
  63. make_setup_mkinitcpio() {
  64. local _hook
  65. mkdir -p ${work_dir}/x86_64/airootfs/etc/initcpio/hooks
  66. mkdir -p ${work_dir}/x86_64/airootfs/etc/initcpio/install
  67. for _hook in archiso archiso_shutdown archiso_pxe_common archiso_pxe_nbd archiso_pxe_http archiso_pxe_nfs archiso_loop_mnt; do
  68. cp /usr/lib/initcpio/hooks/${_hook} ${work_dir}/x86_64/airootfs/etc/initcpio/hooks
  69. cp /usr/lib/initcpio/install/${_hook} ${work_dir}/x86_64/airootfs/etc/initcpio/install
  70. done
  71. sed -i "s|/usr/lib/initcpio/|/etc/initcpio/|g" ${work_dir}/x86_64/airootfs/etc/initcpio/install/archiso_shutdown
  72. cp /usr/lib/initcpio/install/archiso_kms ${work_dir}/x86_64/airootfs/etc/initcpio/install
  73. cp /usr/lib/initcpio/archiso_shutdown ${work_dir}/x86_64/airootfs/etc/initcpio
  74. cp ${script_path}/mkinitcpio.conf ${work_dir}/x86_64/airootfs/etc/mkinitcpio-archiso.conf
  75. gnupg_fd=
  76. if [[ ${gpg_key} ]]; then
  77. gpg --export ${gpg_key} >${work_dir}/gpgkey
  78. exec 17<>${work_dir}/gpgkey
  79. fi
  80. ARCHISO_GNUPG_FD=${gpg_key:+17} mkarchiso ${verbose} -w "${work_dir}/x86_64" -C "${work_dir}/pacman.conf" -D "${install_dir}" -r 'mkinitcpio -c /etc/mkinitcpio-archiso.conf -k /boot/vmlinuz-linux -g /boot/archiso.img' run
  81. if [[ ${gpg_key} ]]; then
  82. exec 17<&-
  83. fi
  84. }
  85. # Customize installation (airootfs)
  86. make_customize_airootfs() {
  87. cp -af ${script_path}/airootfs ${work_dir}/x86_64
  88. cp ${script_path}/pacman.conf ${work_dir}/x86_64/airootfs/etc
  89. curl -o ${work_dir}/x86_64/airootfs/etc/pacman.d/mirrorlist 'https://www.archlinux.org/mirrorlist/?country=all&protocol=http&use_mirror_status=on'
  90. lynx -dump -nolist 'https://wiki.archlinux.org/index.php/Installation_Guide?action=render' >> ${work_dir}/x86_64/airootfs/root/install.txt
  91. mkarchiso ${verbose} -w "${work_dir}/x86_64" -C "${work_dir}/pacman.conf" -D "${install_dir}" -r '/root/customize_airootfs.sh' run
  92. rm ${work_dir}/x86_64/airootfs/root/customize_airootfs.sh
  93. }
  94. # Prepare kernel/initramfs ${install_dir}/boot/
  95. make_boot() {
  96. mkdir -p ${work_dir}/iso/${install_dir}/boot/x86_64
  97. cp ${work_dir}/x86_64/airootfs/boot/archiso.img ${work_dir}/iso/${install_dir}/boot/x86_64/archiso.img
  98. cp ${work_dir}/x86_64/airootfs/boot/vmlinuz-linux ${work_dir}/iso/${install_dir}/boot/x86_64/vmlinuz
  99. }
  100. # Add other aditional/extra files to ${install_dir}/boot/
  101. make_boot_extra() {
  102. cp ${work_dir}/x86_64/airootfs/boot/memtest86+/memtest.bin ${work_dir}/iso/${install_dir}/boot/memtest
  103. cp ${work_dir}/x86_64/airootfs/usr/share/licenses/common/GPL2/license.txt ${work_dir}/iso/${install_dir}/boot/memtest.COPYING
  104. cp ${work_dir}/x86_64/airootfs/boot/intel-ucode.img ${work_dir}/iso/${install_dir}/boot/intel_ucode.img
  105. cp ${work_dir}/x86_64/airootfs/usr/share/licenses/intel-ucode/LICENSE ${work_dir}/iso/${install_dir}/boot/intel_ucode.LICENSE
  106. cp ${work_dir}/x86_64/airootfs/boot/amd-ucode.img ${work_dir}/iso/${install_dir}/boot/amd_ucode.img
  107. cp ${work_dir}/x86_64/airootfs/usr/share/licenses/amd-ucode/LICENSE ${work_dir}/iso/${install_dir}/boot/amd_ucode.LICENSE
  108. }
  109. # Prepare /${install_dir}/boot/syslinux
  110. make_syslinux() {
  111. _uname_r=$(file -b ${work_dir}/x86_64/airootfs/boot/vmlinuz-linux| awk 'f{print;f=0} /version/{f=1}' RS=' ')
  112. mkdir -p ${work_dir}/iso/${install_dir}/boot/syslinux
  113. for _cfg in ${script_path}/syslinux/*.cfg; do
  114. sed "s|%ARCHISO_LABEL%|${iso_label}|g;
  115. s|%INSTALL_DIR%|${install_dir}|g" ${_cfg} > ${work_dir}/iso/${install_dir}/boot/syslinux/${_cfg##*/}
  116. done
  117. cp ${script_path}/syslinux/splash.png ${work_dir}/iso/${install_dir}/boot/syslinux
  118. cp ${work_dir}/x86_64/airootfs/usr/lib/syslinux/bios/*.c32 ${work_dir}/iso/${install_dir}/boot/syslinux
  119. cp ${work_dir}/x86_64/airootfs/usr/lib/syslinux/bios/lpxelinux.0 ${work_dir}/iso/${install_dir}/boot/syslinux
  120. cp ${work_dir}/x86_64/airootfs/usr/lib/syslinux/bios/memdisk ${work_dir}/iso/${install_dir}/boot/syslinux
  121. mkdir -p ${work_dir}/iso/${install_dir}/boot/syslinux/hdt
  122. gzip -c -9 ${work_dir}/x86_64/airootfs/usr/share/hwdata/pci.ids > ${work_dir}/iso/${install_dir}/boot/syslinux/hdt/pciids.gz
  123. gzip -c -9 ${work_dir}/x86_64/airootfs/usr/lib/modules/${_uname_r}/modules.alias > ${work_dir}/iso/${install_dir}/boot/syslinux/hdt/modalias.gz
  124. }
  125. # Prepare /isolinux
  126. make_isolinux() {
  127. mkdir -p ${work_dir}/iso/isolinux
  128. sed "s|%INSTALL_DIR%|${install_dir}|g" ${script_path}/isolinux/isolinux.cfg > ${work_dir}/iso/isolinux/isolinux.cfg
  129. cp ${work_dir}/x86_64/airootfs/usr/lib/syslinux/bios/isolinux.bin ${work_dir}/iso/isolinux/
  130. cp ${work_dir}/x86_64/airootfs/usr/lib/syslinux/bios/isohdpfx.bin ${work_dir}/iso/isolinux/
  131. cp ${work_dir}/x86_64/airootfs/usr/lib/syslinux/bios/ldlinux.c32 ${work_dir}/iso/isolinux/
  132. }
  133. # Prepare /EFI
  134. make_efi() {
  135. mkdir -p ${work_dir}/iso/EFI/boot
  136. cp ${work_dir}/x86_64/airootfs/usr/share/efitools/efi/PreLoader.efi ${work_dir}/iso/EFI/boot/bootx64.efi
  137. cp ${work_dir}/x86_64/airootfs/usr/share/efitools/efi/HashTool.efi ${work_dir}/iso/EFI/boot/
  138. cp ${work_dir}/x86_64/airootfs/usr/lib/systemd/boot/efi/systemd-bootx64.efi ${work_dir}/iso/EFI/boot/loader.efi
  139. mkdir -p ${work_dir}/iso/loader/entries
  140. cp ${script_path}/efiboot/loader/loader.conf ${work_dir}/iso/loader/
  141. cp ${script_path}/efiboot/loader/entries/uefi-shell-v2-x86_64.conf ${work_dir}/iso/loader/entries/
  142. cp ${script_path}/efiboot/loader/entries/uefi-shell-v1-x86_64.conf ${work_dir}/iso/loader/entries/
  143. sed "s|%ARCHISO_LABEL%|${iso_label}|g;
  144. s|%INSTALL_DIR%|${install_dir}|g" \
  145. ${script_path}/efiboot/loader/entries/archiso-x86_64-usb.conf > ${work_dir}/iso/loader/entries/archiso-x86_64.conf
  146. # EFI Shell 2.0 for UEFI 2.3+
  147. curl -o ${work_dir}/iso/EFI/shellx64_v2.efi https://raw.githubusercontent.com/tianocore/edk2/UDK2018/ShellBinPkg/UefiShell/X64/Shell.efi
  148. # EFI Shell 1.0 for non UEFI 2.3+
  149. curl -o ${work_dir}/iso/EFI/shellx64_v1.efi https://raw.githubusercontent.com/tianocore/edk2/UDK2018/EdkShellBinPkg/FullShell/X64/Shell_Full.efi
  150. }
  151. # Prepare efiboot.img::/EFI for "El Torito" EFI boot mode
  152. make_efiboot() {
  153. mkdir -p ${work_dir}/iso/EFI/archiso
  154. truncate -s 64M ${work_dir}/iso/EFI/archiso/efiboot.img
  155. mkfs.fat -n ARCHISO_EFI ${work_dir}/iso/EFI/archiso/efiboot.img
  156. mkdir -p ${work_dir}/efiboot
  157. mount ${work_dir}/iso/EFI/archiso/efiboot.img ${work_dir}/efiboot
  158. mkdir -p ${work_dir}/efiboot/EFI/archiso
  159. cp ${work_dir}/iso/${install_dir}/boot/x86_64/vmlinuz ${work_dir}/efiboot/EFI/archiso/vmlinuz.efi
  160. cp ${work_dir}/iso/${install_dir}/boot/x86_64/archiso.img ${work_dir}/efiboot/EFI/archiso/archiso.img
  161. cp ${work_dir}/iso/${install_dir}/boot/intel_ucode.img ${work_dir}/efiboot/EFI/archiso/intel_ucode.img
  162. cp ${work_dir}/iso/${install_dir}/boot/amd_ucode.img ${work_dir}/efiboot/EFI/archiso/amd_ucode.img
  163. mkdir -p ${work_dir}/efiboot/EFI/boot
  164. cp ${work_dir}/x86_64/airootfs/usr/share/efitools/efi/PreLoader.efi ${work_dir}/efiboot/EFI/boot/bootx64.efi
  165. cp ${work_dir}/x86_64/airootfs/usr/share/efitools/efi/HashTool.efi ${work_dir}/efiboot/EFI/boot/
  166. cp ${work_dir}/x86_64/airootfs/usr/lib/systemd/boot/efi/systemd-bootx64.efi ${work_dir}/efiboot/EFI/boot/loader.efi
  167. mkdir -p ${work_dir}/efiboot/loader/entries
  168. cp ${script_path}/efiboot/loader/loader.conf ${work_dir}/efiboot/loader/
  169. cp ${script_path}/efiboot/loader/entries/uefi-shell-v2-x86_64.conf ${work_dir}/efiboot/loader/entries/
  170. cp ${script_path}/efiboot/loader/entries/uefi-shell-v1-x86_64.conf ${work_dir}/efiboot/loader/entries/
  171. sed "s|%ARCHISO_LABEL%|${iso_label}|g;
  172. s|%INSTALL_DIR%|${install_dir}|g" \
  173. ${script_path}/efiboot/loader/entries/archiso-x86_64-cd.conf > ${work_dir}/efiboot/loader/entries/archiso-x86_64.conf
  174. cp ${work_dir}/iso/EFI/shellx64_v2.efi ${work_dir}/efiboot/EFI/
  175. cp ${work_dir}/iso/EFI/shellx64_v1.efi ${work_dir}/efiboot/EFI/
  176. umount -d ${work_dir}/efiboot
  177. }
  178. # Build airootfs filesystem image
  179. make_prepare() {
  180. cp -a -l -f ${work_dir}/x86_64/airootfs ${work_dir}
  181. mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" pkglist
  182. mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" ${gpg_key:+-g ${gpg_key}} prepare
  183. rm -rf ${work_dir}/airootfs
  184. # rm -rf ${work_dir}/x86_64/airootfs (if low space, this helps)
  185. }
  186. # Build ISO
  187. make_iso() {
  188. mkarchiso ${verbose} -w "${work_dir}" -D "${install_dir}" -L "${iso_label}" -P "${iso_publisher}" -A "${iso_application}" -o "${out_dir}" iso "${iso_name}-${iso_version}-x86_64.iso"
  189. }
  190. if [[ ${EUID} -ne 0 ]]; then
  191. echo "This script must be run as root."
  192. _usage 1
  193. fi
  194. while getopts 'N:V:L:P:A:D:w:o:g:vh' arg; do
  195. case "${arg}" in
  196. N) iso_name="${OPTARG}" ;;
  197. V) iso_version="${OPTARG}" ;;
  198. L) iso_label="${OPTARG}" ;;
  199. P) iso_publisher="${OPTARG}" ;;
  200. A) iso_application="${OPTARG}" ;;
  201. D) install_dir="${OPTARG}" ;;
  202. w) work_dir="${OPTARG}" ;;
  203. o) out_dir="${OPTARG}" ;;
  204. g) gpg_key="${OPTARG}" ;;
  205. v) verbose="-v" ;;
  206. h) _usage 0 ;;
  207. *)
  208. echo "Invalid argument '${arg}'"
  209. _usage 1
  210. ;;
  211. esac
  212. done
  213. mkdir -p ${work_dir}
  214. run_once make_pacman_conf
  215. run_once make_basefs
  216. run_once make_packages
  217. run_once make_setup_mkinitcpio
  218. run_once make_customize_airootfs
  219. run_once make_boot
  220. run_once make_boot_extra
  221. run_once make_syslinux
  222. run_once make_isolinux
  223. run_once make_efi
  224. run_once make_efiboot
  225. run_once make_prepare
  226. run_once make_iso