build.bash 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #!/usr/bin/env bash
  2. # 本脚本用于构建 PageOS 的镜像文件
  3. # This script is used to build PageOS's image file
  4. # 依赖:archiso aurutils reflector
  5. # dependencies: archiso aurutils reflector
  6. # 设置定量 | Quantities
  7. ## 当前脚本所在目录 | Script directory
  8. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  9. ## 构建目录 | Archiso build directory
  10. BUILD_DIR=$SCRIPT_DIR/build
  11. ## 本地仓库名称 | Local repository name
  12. REPO_NAME=local-repo
  13. ## 本地仓库目录 | Local repository directory
  14. LOCAL_REPO_DIR=$BUILD_DIR/$REPO_NAME
  15. ## 本地仓库数据库文件 | Local repository database file
  16. LOCAL_REPO_PATH=$LOCAL_REPO_DIR/$REPO_NAME.db.tar.gz
  17. ## archiso 工作目录 | Archiso working directory
  18. ARCHISO_BUILD_DIR=$BUILD_DIR/archiso-build
  19. ## 输出目录 | Output directory
  20. OUTPUT_DIR=$SCRIPT_DIR/target
  21. ## 进度文件 | Progress file
  22. PROGRESS_FILE=$BUILD_DIR/progress.log
  23. ## pacman.conf 文件 | pacman.conf file
  24. PACMAN_CONF=$BUILD_DIR/pacman.conf
  25. ## mirrorlist 文件 | Mirrorlist file
  26. MIRRORLIST=$BUILD_DIR/mirrorlist
  27. ## 当前语言 | Current language
  28. CURRENT_LANG=0 # 0: en-US, 1: zh-Hans-CN
  29. ## 是否自动选择 | Auto select
  30. NO_CONFIRM=0
  31. ## 使用默认源 | Use default source
  32. NO_MIRROR_CHECK=0
  33. ## 无效参数 | Invalid input
  34. INVALID_INPUT=""
  35. # 启用严格错误检查 | Strict error checking
  36. set -euo pipefail
  37. # 本地化 | Localization
  38. recho() {
  39. if [ $CURRENT_LANG == 1 ]; then
  40. ## zh-Hans-CN
  41. echo $1;
  42. else
  43. ## en-US
  44. echo $2;
  45. fi
  46. }
  47. # 错误处理函数 | Error handling function
  48. handle_error() {
  49. local lineno=$1
  50. local msg=$2
  51. recho "错误发生在第 $lineno 行: $msg" "Error occurred at line $lineno: $msg"
  52. exit 1
  53. }
  54. # 设置错误捕获 | Error handling trap
  55. trap 'handle_error ${LINENO} "$BASH_COMMAND"' ERR
  56. # 语言检测 | Language detection
  57. if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CURRENT_LANG=1; fi
  58. # 创建 pacman.conf 文件 | Create pacman.conf file
  59. create_pacman_conf() {
  60. if [[ ! -f "$PROGRESS_FILE" ]] || [[ ! -f "$PACMAN_CONF" ]] || ! grep -q "1 创建 pacman.conf 文件" "$PROGRESS_FILE"; then
  61. cat << EOF > "$PACMAN_CONF"
  62. #
  63. # /etc/pacman.conf
  64. #
  65. # See the pacman.conf(5) manpage for option and repository directives
  66. #
  67. # GENERAL OPTIONS
  68. #
  69. [options]
  70. # The following paths are commented out with their default values listed.
  71. # If you wish to use different paths, uncomment and update the paths.
  72. #RootDir = /
  73. #DBPath = /var/lib/pacman/
  74. #CacheDir = /var/cache/pacman/pkg/
  75. #LogFile = /var/log/pacman.log
  76. #GPGDir = /etc/pacman.d/gnupg/
  77. #HookDir = /etc/pacman.d/hooks/
  78. HoldPkg = pacman glibc
  79. #XferCommand = /usr/bin/curl -L -C - -f -o %o %u
  80. #XferCommand = /usr/bin/wget --passive-ftp -c -O %o %u
  81. #CleanMethod = KeepInstalled
  82. Architecture = auto
  83. # Pacman won't upgrade packages listed in IgnorePkg and members of IgnoreGroup
  84. #IgnorePkg =
  85. #IgnoreGroup =
  86. #NoUpgrade =
  87. #NoExtract =
  88. # Misc options
  89. #UseSyslog
  90. #Color
  91. #NoProgressBar
  92. # We cannot check disk space from within a chroot environment
  93. #CheckSpace
  94. #VerbosePkgLists
  95. ParallelDownloads = 5
  96. #DownloadUser = alpm
  97. #DisableSandbox
  98. # By default, pacman accepts packages signed by keys that its local keyring
  99. # trusts (see pacman-key and its man page), as well as unsigned packages.
  100. SigLevel = Required DatabaseOptional
  101. LocalFileSigLevel = Optional
  102. #RemoteFileSigLevel = Required
  103. # NOTE: You must run \`pacman-key --init\` before first using pacman; the local
  104. # keyring can then be populated with the keys of all official Arch Linux
  105. # packagers with \`pacman-key --populate archlinux\`.
  106. #
  107. # REPOSITORIES
  108. # - can be defined here or included from another file
  109. # - pacman will search repositories in the order defined here
  110. # - local/custom mirrors can be added here or in separate files
  111. # - repositories listed first will take precedence when packages
  112. # have identical names, regardless of version number
  113. # - URLs will have \$repo replaced by the name of the current repo
  114. # - URLs will have \$arch replaced by the name of the architecture
  115. #
  116. # Repository entries are of the format:
  117. # [repo-name]
  118. # Server = ServerName
  119. # Include = IncludePath
  120. #
  121. # The header [repo-name] is crucial - it must be present and
  122. # uncommented to enable the repo.
  123. #
  124. # The testing repositories are disabled by default. To enable, uncomment the
  125. # repo name header and Include lines. You can add preferred servers immediately
  126. # after the header, and they will be used before the default mirrors.
  127. #[core-testing]
  128. #Include = $MIRRORLIST
  129. [core]
  130. Include = $MIRRORLIST
  131. #[extra-testing]
  132. #Include = $MIRRORLIST
  133. [extra]
  134. Include = $MIRRORLIST
  135. # If you want to run 32 bit applications on your x86_64 system,
  136. # enable the multilib repositories as required here.
  137. #[multilib-testing]
  138. #Include = $MIRRORLIST
  139. [multilib]
  140. Include = $MIRRORLIST
  141. # An example of a custom package repository. See the pacman manpage for
  142. # tips on creating your own repositories.
  143. #[custom]
  144. #SigLevel = Optional TrustAll
  145. #Server = file:///home/custompkgs
  146. # [chaotic-aur]
  147. # Include = /etc/pacman.d/chaotic-mirrorlist
  148. [archlinuxcn]
  149. Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/\$arch
  150. [$REPO_NAME]
  151. SigLevel = Optional TrustAll
  152. Server = file://$LOCAL_REPO_DIR
  153. EOF
  154. echo "1 创建 pacman.conf 文件" >> "$PROGRESS_FILE"
  155. fi
  156. }
  157. # 更新软件源 | Update software sources
  158. update_mirrorlist() {
  159. if [[ $NO_MIRROR_CHECK -eq 0 ]]; then
  160. recho "正在更新软件源……" "Updating mirrorlist..."
  161. reflector --sort rate --threads 128 --save $MIRRORLIST || {
  162. recho "软件源更新失败" "Mirrorlist update failed"
  163. exit 1
  164. }
  165. fi
  166. }
  167. # 提取并准备 AUR 包 | Extract and prepare AUR packages
  168. get_aur_packages() {
  169. # 确保 packages.x86_64 文件存在 | Ensure packages.x86_64 file exists
  170. if [[ ! -f "$SCRIPT_DIR/packages.x86_64" ]]; then
  171. recho "错误:找不到 packages.x86_64 文件" "Error: packages.x86_64 file not found"
  172. exit 1
  173. fi
  174. # 初始化本地 AUR 仓库 | Initialize local AUR repository
  175. if [[ ! -f "$PROGRESS_FILE" ]] || [[ ! -d "$BUILD_DIR/blankdb" ]] || ! grep -q "2 初始化本地 AUR 仓库" "$PROGRESS_FILE"; then
  176. recho "正在初始化本地 AUR 仓库..." "Initializing local AUR repository..."
  177. if [[ -d "$BUILD_DIR/blankdb" ]]; then
  178. sudo rm -rf "$BUILD_DIR/blankdb"
  179. fi
  180. mkdir -p "$BUILD_DIR/blankdb"
  181. sudo chown :alpm "$BUILD_DIR/blankdb"
  182. chmod 755 "$BUILD_DIR/blankdb"
  183. if [ -d "$LOCAL_REPO_DIR" ]; then
  184. sudo rm -rf "$LOCAL_REPO_DIR"
  185. fi
  186. mkdir -p "$LOCAL_REPO_DIR"
  187. # 【因为本地仓库还未初始化完毕,以下这一行无法获取本地仓库数据库】
  188. # sudo pacman --config "$PACMAN_CONF" -Syw --cachedir "$LOCAL_REPO_DIR" --dbpath "$BUILD_DIR/blankdb" --noconfirm base
  189. sudo pacman -Syw --cachedir "$LOCAL_REPO_DIR" --dbpath "$BUILD_DIR/blankdb" --noconfirm base
  190. repo-add "$LOCAL_REPO_PATH" "$LOCAL_REPO_DIR"/*[^sig] || {
  191. recho "无法初始化本地 AUR 仓库" "Failed to initialize local AUR repository"
  192. exit 1
  193. }
  194. echo "2 初始化本地 AUR 仓库" >> "$PROGRESS_FILE"
  195. fi
  196. # 安装 archlinuxcn 源的钥 | Install the key of archlinuxcn source
  197. sudo pacman-key --lsign-key "[email protected]" || {
  198. recho "无法签名 archlinuxcn 源的钥" "Failed to sign the key of archlinuxcn source"
  199. exit 1
  200. }
  201. # sudo pacman -Sy archlinuxcn-keyring --noconfirm || {
  202. # recho "无法安装 archlinuxcn 源的钥" "Failed to install the key of archlinuxcn source"
  203. # exit 1
  204. # }
  205. # 处理 packages.x86_64 中 AUR 包的流程 | Processing the flow of AUR packages in packages.x86_64
  206. if [[ ! -f "$PROGRESS_FILE" ]] || [[ ! -d "$LOCAL_REPO_DIR" ]] || ! grep -q "3 处理 packages.x86_64 中 AUR 包的流程" "$PROGRESS_FILE"; then
  207. recho "正在处理 packages.x86_64 文件……" "Processing packages.x86_64 file..."
  208. while read -r pkg; do
  209. # 跳过空行和注释 | Skip empty lines and comments
  210. [[ -z "$pkg" || "$pkg" == \#* ]] && continue
  211. # 判断是否是 AUR 包并同步 | Determine whether it is an AUR package and synchronize
  212. recho "正在处理包: $pkg" "Processing package: $pkg"
  213. pkg_clean=$(echo "$pkg" | sed 's/#.*$//' | xargs)
  214. if ! pacman -Ss "$pkg_clean" &> /dev/null; then
  215. recho "正在同步 AUR 包 $pkg 到本地仓库……" "Syncing AUR package $pkg to local repository..."
  216. aur sync -d "$REPO_NAME" --root "$LOCAL_REPO_DIR" --noconfirm --no-view --pacman-conf "$PACMAN_CONF" "$pkg" || {
  217. recho "无法同步 AUR 包: $pkg" "Failed to sync AUR package: $pkg"
  218. exit 1
  219. }
  220. fi
  221. done < $SCRIPT_DIR/packages.x86_64
  222. echo "3 处理 packages.x86_64 中 AUR 包的流程" >> "$PROGRESS_FILE"
  223. fi
  224. }
  225. # 处理传入参数 | Processing input parameters
  226. for i in $@; do
  227. if [ "$i" == "--noconfirm" ]; then
  228. NO_CONFIRM=1
  229. elif [ "$i" == "--no-mirror-check" ]; then
  230. NO_MIRROR_CHECK=1
  231. elif [ "$i" == "--help" -o "$i" == "-h" ]; then
  232. recho "本脚本用于构建 PageOS 的镜像文件。" "This script is used to build PageOS's image file."
  233. recho "用法:$0 [--noconfirm] [--no-mirror-check] [--help]" "Usage: $0 [--noconfirm] [--no-mirror-check] [--help]"
  234. exit 0
  235. else
  236. INVALID_INPUT+="$i "
  237. fi
  238. done
  239. if [ -n "$INVALID_INPUT" ]; then
  240. recho "无效参数:" "Invalid argument:"
  241. for i in $INVALID_INPUT; do
  242. echo " $i"
  243. done
  244. recho "可运行 $0 --help 以显示所有可用参数。" "Run $0 --help to see all available argument."
  245. exit 1
  246. fi
  247. # 检查进度文件是否存在 | Check if the progress file exists
  248. if [[ ! -f "$PROGRESS_FILE" ]] || ! grep -q "0 初始化" "$PROGRESS_FILE"; then
  249. if [ ! -d "$BUILD_DIR" ]; then
  250. mkdir -p "$BUILD_DIR"
  251. fi
  252. echo "0 初始化" > "$PROGRESS_FILE"
  253. else
  254. recho "进度文件 $PROGRESS_FILE 已存在。" "Progress file $PROGRESS_FILE already exists."
  255. # 处理 --noconfirm
  256. if [[ $NO_CONFIRM -eq 1 ]]; then
  257. recho "自动选择不继续之前的进度(--noconfirm)。" "Automatically choosing not to continue previous progress (--noconfirm)."
  258. if [ -d "$BUILD_DIR" ]; then
  259. sudo rm -rf "$BUILD_DIR"
  260. fi
  261. mkdir -p "$BUILD_DIR" && echo "0 初始化" > "$PROGRESS_FILE"
  262. else
  263. read -p "$(recho "是否继续之前的进度?(y/N): " "Do you want to continue from the previous progress? (y/N): ")" choice
  264. case "$choice" in
  265. y|Y ) recho "保留现有进度。" "Continuing from the previous progress.";;
  266. n|N|* )
  267. if [ -d "$BUILD_DIR" ]; then
  268. sudo rm -rf "$BUILD_DIR"
  269. fi
  270. mkdir -p "$BUILD_DIR" && echo "0 初始化" > "$PROGRESS_FILE"
  271. ;;
  272. esac
  273. fi
  274. fi
  275. # 创建构建目录 | Create build directory
  276. if [ ! -d "$ARCHISO_BUILD_DIR" ]; then
  277. mkdir -p "$ARCHISO_BUILD_DIR"
  278. fi
  279. # 检查输出目录是否存在 | Check if the output directory exists
  280. if [ ! -d "$OUTPUT_DIR" ]; then
  281. mkdir -p "$OUTPUT_DIR"
  282. fi
  283. # 创建 pacman.conf 文件 | Create pacman.conf file
  284. create_pacman_conf
  285. # 询问用户是否需要更新软件源 | Ask the user whether they need to update the software source
  286. if [[ ! -f "$MIRRORLIST" ]]; then
  287. update_mirrorlist
  288. else
  289. # 处理 --noconfirm | Process --noconfirm
  290. if [[ $NO_CONFIRM -eq 1 ]]; then
  291. recho "自动选择不更新软件源(--noconfirm)。" "Automatically choosing not to update mirrorlist (--noconfirm)."
  292. recho "保留现有软件源。" "Keep the existing mirrorlist."
  293. else
  294. read -p "$(recho "是否需要更新软件源?(y/N): " "Do you need to update the mirrorlist? (y/N): ")" choice
  295. case "$choice" in
  296. y|Y ) update_mirrorlist;;
  297. n|N|* ) recho "保留现有软件源。" "Keep the existing mirrorlist.";;
  298. esac
  299. fi
  300. fi
  301. # 提取并准备 AUR 包 | Extract and prepare AUR packages
  302. get_aur_packages
  303. # 运行 mkarchiso 命令以编译 archiso | Run the mkarchiso command to compile archiso
  304. recho "正在编译 ISO 文件……" "Compiling ISO file..."
  305. sudo mkarchiso -v -C "$PACMAN_CONF" -w "$ARCHISO_BUILD_DIR" -o "$OUTPUT_DIR" "$SCRIPT_DIR"