2
0

rebrand.bash 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/usr/bin/env bash
  2. # 设置定量 | Quantities
  3. ## 仓库所在目录 | Repository directory
  4. REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
  5. ## 当前语言 | Current language
  6. CURRENT_LANG=0 ### 0: en-US, 1: zh-Hans-CN
  7. ## 用户界面 ID | User界面 ID
  8. UI_ID=$1
  9. ## 登录页面直链 | Login page direct link
  10. LOGIN_URL=$2
  11. ## 登录页面路径 | Login page path
  12. LOGIN_PATH=""
  13. ## 系统名称 | System name
  14. SYS_NAME=$3
  15. # 本地化 | Localization
  16. recho() {
  17. if [ "$CURRENT_LANG" == "1" ]; then
  18. ## zh-Hans-CN
  19. echo -e "$1";
  20. else
  21. ## en-US
  22. echo -e "$2";
  23. fi
  24. }
  25. # 验证系统名称是否符合 Linux 用户名格式 | Validate system name conforms to Linux username format
  26. validate_sys_name() {
  27. local name="$1"
  28. # 检查是否为空 | Check if empty
  29. if [ -z "$name" ]; then
  30. recho "错误: 系统名称不能为空" "Error: System name cannot be empty"
  31. return 1
  32. fi
  33. # 检查长度 (1-32 字符) | Check length (1-32 characters)
  34. if [ ${#name} -lt 1 ] || [ ${#name} -gt 32 ]; then
  35. recho "错误: 系统名称长度必须在 1-32 个字符之间" "Error: System name must be between 1-32 characters"
  36. return 1
  37. fi
  38. # 检查是否以字母开头 | Check if starts with a letter
  39. if ! [[ "$name" =~ ^[a-zA-Z] ]]; then
  40. recho "错误: 系统名称必须以字母开头" "Error: System name must start with a letter"
  41. return 1
  42. fi
  43. # 检查是否只包含字母、数字、下划线和连字符 | Check if contains only letters, numbers, underscores and hyphens
  44. if ! [[ "$name" =~ ^[a-zA-Z0-9_-]+$ ]]; then
  45. recho "错误: 系统名称只能包含字母、数字、下划线和连字符" "Error: System name can only contain letters, numbers, underscores and hyphens"
  46. return 1
  47. fi
  48. # 检查是否以连字符结尾 | Check if ends with a hyphen
  49. if [[ "$name" =~ -$ ]]; then
  50. recho "错误: 系统名称不能以连字符结尾" "Error: System name cannot end with a hyphen"
  51. return 1
  52. fi
  53. # 检查是否全部是数字 | Check if all numbers
  54. if [[ "$name" =~ ^[0-9]+$ ]]; then
  55. recho "错误: 系统名称不能全部是数字" "Error: System name cannot be all numbers"
  56. return 1
  57. fi
  58. # 检查是否包含大写字母,并给出建议 | Check if contains uppercase letters and give suggestion
  59. if [[ "$name" =~ [A-Z] ]]; then
  60. local lowercase_name=$(echo "$name" | tr '[:upper:]' '[:lower:]')
  61. recho "警告: 系统名称包含大写字母。虽然 Linux 支持大写字母用户名,但建议使用小写字母以获得更好的兼容性。" "Warning: System name contains uppercase letters. While Linux supports uppercase usernames, it's recommended to use lowercase for better compatibility."
  62. recho "建议: 考虑使用 '$lowercase_name' 替代 '$name'" "Suggestion: Consider using '$lowercase_name' instead of '$name'"
  63. # 不直接返回错误,只给出警告
  64. fi
  65. # 检查是否是保留的用户名 | Check if it's a reserved username
  66. local reserved_names=("root" "daemon" "bin" "sys" "sync" "games" "man" "lp" "mail" "news" "uucp" "proxy" "www-data" "backup" "list" "irc" "gnats" "nobody" "systemd-network" "systemd-resolve" "systemd-timesync" "messagebus" "syslog" "_apt" "tss" "uuidd" "tcpdump" "avahi-autoipd" "usbmux" "rtkit" "cups-pk-helper" "dnsmasq" "avahi" "kernoops" "saned" "pulse" "rdma" "sshd" "polkitd" "colord" "geoclue" "Debian-exim" "systemd-coredump" "lightdm" "speech-dispatcher")
  67. for reserved in "${reserved_names[@]}"; do
  68. if [ "$name" = "$reserved" ]; then
  69. recho "错误: '$name' 是系统保留的用户名" "Error: '$name' is a reserved system username"
  70. return 1
  71. fi
  72. done
  73. return 0
  74. }
  75. # 语言检测 | Language detection
  76. if [ $(echo ${LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CURRENT_LANG=1; fi
  77. # 检查依赖命令 | Check dependency command
  78. if ! command -v curl &> /dev/null; then
  79. recho "错误: 未找到 curl 命令" "Error: curl command not found"
  80. exit 1
  81. fi
  82. if ! command -v sed &> /dev/null; then
  83. recho "错误: 未找到 sed 命令" "Error: sed command not found"
  84. exit 1
  85. fi
  86. # 检查参数数量 | Check number of parameters
  87. if [ $# -gt 3 ]; then
  88. recho "错误: 参数过多。用法: $0 [UI_ID] [LOGIN_URL] [SYS_NAME]" "Error: Too many arguments. Usage: $0 [UI_ID] [LOGIN_URL] [SYS_NAME]"
  89. exit 1
  90. fi
  91. # 如果登录页面直链不为空,下载登录页面直链的文件至指定位置 | If the login page direct link is not empty, download the file to the specified location
  92. if [ -n "$LOGIN_URL" ]; then
  93. # 创建目标目录(如果不存在)| Create the destination directory (if it does not exist)
  94. DEST_DIR="$REPO_DIR/airootfs/usr/local/lib/pageos-greet"
  95. if [ ! -d "$DEST_DIR" ]; then
  96. recho "正在创建目录: $DEST_DIR" "Creating directory: $DEST_DIR"
  97. mkdir -p "$DEST_DIR"
  98. if [ $? -ne 0 ]; then
  99. recho "错误: 无法创建目录 $DEST_DIR" "Error: Failed to create directory $DEST_DIR"
  100. exit 1
  101. fi
  102. fi
  103. # 下载登录页面 | Download login page
  104. recho "正在下载登录页面: $LOGIN_URL" "Downloading login page: $LOGIN_URL"
  105. curl -s -f -o "$REPO_DIR/airootfs/usr/local/lib/pageos-greet/login-page.html" "$LOGIN_URL"
  106. if [ $? -ne 0 ]; then
  107. recho "错误: 无法下载登录页面 $LOGIN_URL" "Error: Failed to download login page $LOGIN_URL"
  108. exit 1
  109. fi
  110. recho "已下载登录页面: $LOGIN_URL" "Downloaded login page: $LOGIN_URL"
  111. LOGIN_PATH=" /usr/local/lib/pageos-greet/login-page.html"
  112. fi
  113. # 如果用户界面 ID 为空,则设为 pageos-ui | If the user interface ID is empty, set it to pageos-ui
  114. if [ -z "$UI_ID" ]; then
  115. UI_ID="pageos-ui"
  116. fi
  117. # 如果用户界面 ID 不为 pageos-ui 或登录页面路径不为空,替换 airootfs/etc/greetd/config.toml 中 pageos-session 的参数 | If the user interface ID is not pageos-ui or the login page path is not empty, replace the parameter pageos-session in airootfs/etc/greetd/config.toml
  118. if [ "$UI_ID" != "pageos-ui" ] || [ -n "$LOGIN_PATH" ]; then
  119. CONFIG_FILE="$REPO_DIR/airootfs/etc/greetd/config.toml"
  120. # airootfs/usr/local/bin/pageos-pkgr repo install --repo airootfs/etc/skel/.local/share/pageos $UI_ID
  121. if [ -n "$UI_ID" ] && [ "$UI_ID" != "pageos-ui" ]; then
  122. recho "正在安装用户界面: $UI_ID" "Installing user interface: $UI_ID"
  123. "$REPO_DIR/airootfs/usr/local/bin/pageos-pkgr" repo update --repo "$REPO_DIR/airootfs/etc/skel/.local/share/pageos"
  124. "$REPO_DIR/airootfs/usr/local/bin/pageos-pkgr" repo install --repo "$REPO_DIR/airootfs/etc/skel/.local/share/pageos" "$UI_ID"
  125. "$REPO_DIR/airootfs/usr/local/bin/pageos-pkgr" repo clean --repo "$REPO_DIR/airootfs/etc/skel/.local/share/pageos"
  126. if [ $? -ne 0 ]; then
  127. recho "错误: 无法安装用户界面 $UI_ID" "Error: Failed to install user interface $UI_ID"
  128. exit 1
  129. fi
  130. fi
  131. # 检查配置文件是否存在 | Check if the configuration file exists
  132. if [ ! -f "$CONFIG_FILE" ]; then
  133. recho "错误: 配置文件不存在: $CONFIG_FILE" "Error: Config file does not exist: $CONFIG_FILE"
  134. exit 1
  135. fi
  136. # 检查配置文件中是否存在要替换的行 | Check if the line to be replaced exists in the configuration file
  137. if ! grep -q 'command = "pageos-session .*"' "$CONFIG_FILE"; then
  138. recho "错误: 配置文件中未找到要替换的命令行" "Error: Command line to replace not found in config file"
  139. exit 1
  140. fi
  141. # 替换配置文件中的命令 | Replace the command in the configuration file
  142. sed -i "s|command = \"pageos-session .*\"|command = \"pageos-session $UI_ID$LOGIN_PATH\"|" "$CONFIG_FILE"
  143. if [ $? -ne 0 ]; then
  144. recho "错误: 无法更新配置文件" "Error: Failed to update config file"
  145. exit 1
  146. fi
  147. recho "已将用户界面 ID 设置为: $UI_ID" "User interface ID has been set to: $UI_ID"
  148. fi
  149. # 如果系统名称不为空且不等于 pageos,验证其格式 | If the system name is not empty and not equal to pageos, validate its format
  150. if [ -n "$SYS_NAME" ] && [ "$SYS_NAME" != "pageos" ]; then
  151. # 验证系统名称格式 | Validate system name format
  152. if ! validate_sys_name "$SYS_NAME"; then
  153. exit 1
  154. fi
  155. fi
  156. # 如果系统名称不为空,替换 profiledef.sh 中的 iso_name | If the system name is not empty, replace iso_name in profiledef.sh
  157. if [ -n "$SYS_NAME" ] && [ "$SYS_NAME" != "pageos" ]; then
  158. PROFILEDEF_FILE="$REPO_DIR/profiledef.sh"
  159. # 检查配置文件是否存在 | Check if the configuration file exists
  160. if [ ! -f "$PROFILEDEF_FILE" ]; then
  161. recho "错误: 配置文件不存在: $PROFILEDEF_FILE" "Error: Config file does not exist: $PROFILEDEF_FILE"
  162. exit 1
  163. fi
  164. # 检查配置文件中是否存在要替换的行 | Check if the line to be replaced exists in the configuration file
  165. if ! grep -q 'iso_name="[^"]*"' "$PROFILEDEF_FILE"; then
  166. recho "错误: 配置文件中未找到要替换的 iso_name 行" "Error: iso_name line to replace not found in config file"
  167. exit 1
  168. fi
  169. # 替换配置文件中的 iso_name | Replace the iso_name in the configuration file
  170. sed -i "s|iso_name=\"[^\"]*\"|iso_name=\"$SYS_NAME\"|" "$PROFILEDEF_FILE"
  171. if [ $? -ne 0 ]; then
  172. recho "错误: 无法更新配置文件" "Error: Failed to update config file"
  173. exit 1
  174. fi
  175. recho "已将 ISO 名称设置为: $SYS_NAME" "ISO name has been set to: $SYS_NAME"
  176. fi
  177. # 如果系统名称不为空,替换系统文件中的用户名 | If the system name is not empty, replace the username in system files
  178. if [ -n "$SYS_NAME" ] && [ "$SYS_NAME" != "pageos" ]; then
  179. # 定义需要替换的文件列表 | Define the list of files to be replaced
  180. PASSWD_FILE="$REPO_DIR/airootfs/etc/passwd"
  181. GSHADOW_FILE="$REPO_DIR/airootfs/etc/gshadow"
  182. HOSTNAME_FILE="$REPO_DIR/airootfs/etc/hostname"
  183. SHADOW_FILE="$REPO_DIR/airootfs/etc/shadow"
  184. GROUP_FILE="$REPO_DIR/airootfs/etc/group"
  185. # 替换 passwd 文件中的用户名和主目录 | Replace username and home directory in passwd file
  186. if [ -f "$PASSWD_FILE" ]; then
  187. recho "正在替换 $PASSWD_FILE 中的用户名" "Replacing username in $PASSWD_FILE"
  188. # 替换用户名和主目录中的 pageos
  189. sed -i "s|^pageos:x:1000:1000::/home/pageos:|$SYS_NAME:x:1000:1000::/home/$SYS_NAME:|" "$PASSWD_FILE"
  190. if [ $? -eq 0 ]; then
  191. recho "已成功替换 $PASSWD_FILE 中的用户名" "Successfully replaced username in $PASSWD_FILE"
  192. else
  193. recho "错误: 无法替换 $PASSWD_FILE 中的用户名" "Error: Failed to replace username in $PASSWD_FILE"
  194. fi
  195. else
  196. recho "错误: 文件不存在: $PASSWD_FILE" "Error: File does not exist: $PASSWD_FILE"
  197. fi
  198. # 替换 gshadow 文件中的用户名 | Replace username in gshadow file
  199. if [ -f "$GSHADOW_FILE" ]; then
  200. recho "正在替换 $GSHADOW_FILE 中的用户名" "Replacing username in $GSHADOW_FILE"
  201. sed -i "s|^pageos:!*::|$SYS_NAME:!*::|" "$GSHADOW_FILE"
  202. if [ $? -eq 0 ]; then
  203. recho "已成功替换 $GSHADOW_FILE 中的用户名" "Successfully replaced username in $GSHADOW_FILE"
  204. else
  205. recho "错误: 无法替换 $GSHADOW_FILE 中的用户名" "Error: Failed to replace username in $GSHADOW_FILE"
  206. fi
  207. else
  208. recho "错误: 文件不存在: $GSHADOW_FILE" "Error: File does not exist: $GSHADOW_FILE"
  209. fi
  210. # 替换 hostname 文件中的主机名 | Replace hostname in hostname file
  211. if [ -f "$HOSTNAME_FILE" ]; then
  212. recho "正在替换 $HOSTNAME_FILE 中的主机名" "Replacing hostname in $HOSTNAME_FILE"
  213. sed -i "s|^pageos$|$SYS_NAME|" "$HOSTNAME_FILE"
  214. if [ $? -eq 0 ]; then
  215. recho "已成功替换 $HOSTNAME_FILE 中的主机名" "Successfully replaced hostname in $HOSTNAME_FILE"
  216. else
  217. recho "错误: 无法替换 $HOSTNAME_FILE 中的主机名" "Error: Failed to replace hostname in $HOSTNAME_FILE"
  218. fi
  219. else
  220. recho "错误: 文件不存在: $HOSTNAME_FILE" "Error: File does not exist: $HOSTNAME_FILE"
  221. fi
  222. # 替换 shadow 文件中的用户名 | Replace username in shadow file
  223. if [ -f "$SHADOW_FILE" ]; then
  224. recho "正在替换 $SHADOW_FILE 中的用户名" "Replacing username in $SHADOW_FILE"
  225. sed -i "s|^pageos:|$SYS_NAME:|" "$SHADOW_FILE"
  226. if [ $? -eq 0 ]; then
  227. recho "已成功替换 $SHADOW_FILE 中的用户名" "Successfully replaced username in $SHADOW_FILE"
  228. else
  229. recho "错误: 无法替换 $SHADOW_FILE 中的用户名" "Error: Failed to replace username in $SHADOW_FILE"
  230. fi
  231. else
  232. recho "错误: 文件不存在: $SHADOW_FILE" "Error: File does not exist: $SHADOW_FILE"
  233. fi
  234. # 替换 group 文件中的用户名 | Replace username in group file
  235. if [ -f "$GROUP_FILE" ]; then
  236. recho "正在替换 $GROUP_FILE 中的用户名" "Replacing username in $GROUP_FILE"
  237. # 替换组名和组成员中的 pageos
  238. sed -i "s|:pageos|:$SYS_NAME|g" "$GROUP_FILE"
  239. sed -i "s|^pageos:x:1000:|$SYS_NAME:x:1000:|" "$GROUP_FILE"
  240. if [ $? -eq 0 ]; then
  241. recho "已成功替换 $GROUP_FILE 中的用户名" "Successfully replaced username in $GROUP_FILE"
  242. else
  243. recho "错误: 无法替换 $GROUP_FILE 中的用户名" "Error: Failed to replace username in $GROUP_FILE"
  244. fi
  245. else
  246. recho "错误: 文件不存在: $GROUP_FILE" "Error: File does not exist: $GROUP_FILE"
  247. fi
  248. recho "已将系统文件中的用户名替换为: $SYS_NAME" "Username in system files has been replaced with: $SYS_NAME"
  249. fi