2026-03-10 00:09:05 +08:00
#!/usr/bin/env sh
# shellcheck shell=bash
2023-09-16 20:00:05 +08:00
# shellcheck disable=SC2086
2026-03-10 00:09:05 +08:00
# nixos 默认的配置不会生成 /bin/bash,因此需要用 /usr/bin/env
# alpine 默认没有 bash,因此 shebang 用 sh,再 exec 切换到 bash
2023-06-18 21:27:22 +08:00
set -eE
2023-05-03 22:22:21 +08:00
confhome = https://raw.githubusercontent.com/bin456789/reinstall/main
2025-04-04 07:50:22 +08:00
confhome_cn = https://cnb.cool/bin456789/reinstall/-/git/raw/main
2024-12-18 23:43:34 +08:00
# confhome_cn=https://www.ghproxy.cc/https://raw.githubusercontent.com/bin456789/reinstall/main
2022-09-25 21:57:47 +08:00
2024-09-06 23:00:28 +08:00
# 用于判断 reinstall.sh 和 trans.sh 是否兼容
2025-10-31 07:50:40 +08:00
SCRIPT_VERSION = 4BACD833-A585-23BA-6CBB-9AA4E08E0004
2024-11-23 23:50:11 +08:00
2024-12-06 22:50:05 +08:00
# 记录要用到的 windows 程序,运行时输出删除 \r
WINDOWS_EXES = 'cmd powershell wmic reg diskpart netsh bcdedit mountvol'
2024-09-06 23:00:28 +08:00
2026-03-10 00:09:05 +08:00
BOOT_ENTEY_START_MARK = '### BEGIN reinstall.sh ###'
BOOT_ENTEY_END_MARK = '### END reinstall.sh ###'
# 临时目录
# 不用 /tmp,因为 /tmp 挂载在内存的话,可能不够空间
tmp = /reinstall-tmp
2024-12-06 22:50:05 +08:00
# 强制 linux 程序输出英文,防止 grep 不到想要的内容
2024-02-24 15:06:24 +08:00
# https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
export LC_ALL = C
2024-05-29 21:30:22 +08:00
# 处理部分用户用 su 切换成 root 导致环境变量没 sbin 目录
2025-02-15 00:25:37 +08:00
# 也能处理 cygwin bash 没有添加 -l 运行 reinstall.sh
2024-06-06 20:48:44 +08:00
# 不要漏了最后的 $PATH,否则会找不到 windows 系统程序例如 diskpart
2024-05-29 21:30:22 +08:00
export PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
2025-12-04 19:43:52 +08:00
# 如果不是 bash 的话,继续执行会有语法错误,因此在这里判断是否 bash
2026-04-12 18:10:09 +08:00
if [ -z " $BASH " ] ||
# el 的 sh 是 bash 运行在 posix 模式,依然有 $BASH 和 $BASH_VERSION
{ [ -n " $BASH " ] && [ -n " $POSIXLY_CORRECT " ] ; } ; then
2026-03-10 00:09:05 +08:00
if ! command -v bash >/dev/null; then
if [ -f /etc/alpine-release ] ; then
if ! apk add bash; then
echo "Error while install bash." >& 2
exit 1
fi
else
echo "Please run this script with bash." >& 2
2025-12-04 19:43:52 +08:00
exit 1
fi
fi
2026-03-10 00:09:05 +08:00
exec bash " $0 " " $@ "
2025-12-04 19:43:52 +08:00
fi
2026-03-10 00:09:05 +08:00
# 好像跟 trap SIGINT 有冲突
2024-10-18 23:34:54 +08:00
# 记录日志,过滤含有 password 的行
2026-03-10 00:09:05 +08:00
# exec > >(tee >(grep -iv password >>/reinstall.log)) 2>&1
2024-09-06 23:00:28 +08:00
THIS_SCRIPT = $( readlink -f " $0 " )
2023-09-16 20:00:05 +08:00
trap 'trap_err $LINENO $?' ERR
trap_err() {
line_no = $1
ret_no = $2
error "Line $line_no return $ret_no "
2024-09-06 23:00:28 +08:00
sed -n " $line_no " p " $THIS_SCRIPT "
2023-09-16 20:00:05 +08:00
}
2023-06-18 21:27:22 +08:00
2026-03-10 00:09:05 +08:00
is_in_windows() {
[ " $( uname -o) " = Cygwin ] || [ " $( uname -o) " = Msys ]
}
if is_in_windows; then
reinstall_____ = '.\reinstall.bat'
else
reinstall_____ = 'sh reinstall.sh'
fi
2023-05-03 22:22:21 +08:00
usage_and_exit() {
2026-09-11 00:11:38 +08:00
# kali 官网的 202x.x iso 安装后,apt 源是 kali-rolling
# 微软商店的 wsl kali, apt 源是 kali-last-snapshot
2023-07-28 22:53:55 +08:00
cat <<EOF
2025-01-13 23:19:38 +08:00
Usage: $reinstall_____ anolis 7|8|23
opencloudos 8|9|23
2025-06-13 21:41:24 +08:00
rocky 8|9|10
2025-09-29 21:00:08 +08:00
oracle 8|9|10
2025-05-29 20:48:30 +08:00
almalinux 8|9|10
2024-12-06 22:50:07 +08:00
centos 9|10
2025-12-04 19:43:54 +08:00
fnos 1
2026-06-09 23:08:15 +08:00
fygoos 1
2026-06-12 22:15:57 +08:00
nixos 26.05
2026-04-30 22:08:15 +08:00
fedora 43|44
2025-08-10 08:02:22 +08:00
debian 9|10|11|12|13
2026-05-11 20:20:33 +08:00
opensuse 16.0|tumbleweed
2026-04-30 22:08:15 +08:00
openeuler 20.03|22.03|24.03
2026-06-11 00:12:39 +08:00
alpine 3.21|3.22|3.23|3.24
2026-09-11 00:11:38 +08:00
kali last-snapshot|rolling
2026-04-24 00:12:09 +08:00
ubuntu 18.04|20.04|22.04|24.04|26.04 [--minimal]
2024-10-18 23:58:04 +08:00
arch
gentoo
2025-02-24 22:05:26 +08:00
aosc
2025-05-16 19:06:09 +08:00
redhat --img="http://access.cdn.redhat.com/xxx.qcow2"
2025-02-21 00:30:36 +08:00
dd --img="http://xxx.com/yyy.zzz" (raw image stores in raw/vhd/tar/gz/xz/zst)
windows --image-name="windows xxx yyy" --lang=xx-yy
windows --image-name="windows xxx yyy" --iso="http://xxx.com/xxx.iso"
2024-10-18 23:58:04 +08:00
netboot.xyz
2026-03-10 00:09:05 +08:00
reset
2023-12-16 16:41:34 +08:00
2025-04-28 23:18:27 +08:00
Options: For Linux/Windows:
2026-05-29 07:25:16 +08:00
[--username USERNAME]
2026-03-08 23:50:50 +08:00
[--password PASSWORD]
[--ssh-key KEY]
[--ssh-port PORT]
[--web-port PORT]
[--frpc-config PATH]
2025-04-28 23:18:27 +08:00
For Windows Only:
2024-10-18 23:58:04 +08:00
[--allow-ping]
2026-03-08 23:50:50 +08:00
[--rdp-port PORT]
2026-09-10 01:06:52 +08:00
[--add-driver INF_OR_DIR] (only for iso installation)
[--no-auto-drivers] (only for iso installation)
2024-10-12 23:07:01 +08:00
2026-09-07 19:21:11 +08:00
For Linux Only:
2026-09-10 01:06:52 +08:00
[--no-cloud-kernel] (only for Debian/Ubuntu/Alpine)
2026-09-07 19:21:11 +08:00
Manual: https://github.com/bin456789/reinstall
2024-04-03 21:53:52 +08:00
2023-07-28 22:53:55 +08:00
EOF
2023-05-03 22:22:21 +08:00
exit 1
}
2022-09-25 21:57:47 +08:00
2023-06-18 21:27:22 +08:00
info() {
2025-03-02 21:57:06 +08:00
local msg
if [ " $1 " = false ] ; then
shift
msg = $*
else
2025-05-16 19:06:07 +08:00
msg = "***** $( to_upper <<< " $* " ) *****"
2025-03-02 21:57:06 +08:00
fi
2025-05-16 19:06:07 +08:00
echo_color_text '\e[32m' " $msg " >& 2
2023-09-16 20:00:05 +08:00
}
warn() {
2025-05-16 19:06:07 +08:00
local msg
if [ " $1 " = false ] ; then
shift
msg = $*
else
msg = "Warning: $* "
fi
echo_color_text '\e[33m' " $msg " >& 2
2023-06-18 21:27:22 +08:00
}
error() {
2024-10-18 23:58:04 +08:00
echo_color_text '\e[31m' "***** ERROR *****" >& 2
2025-03-02 21:57:06 +08:00
echo_color_text '\e[31m' " $* " >& 2
2023-09-16 20:00:05 +08:00
}
echo_color_text() {
color = " $1 "
shift
plain = "\e[0m"
echo -e " $color$*$plain "
2023-06-18 21:27:22 +08:00
}
2023-06-04 22:10:16 +08:00
error_and_exit() {
2023-06-18 21:27:22 +08:00
error " $@ "
2023-06-04 22:10:16 +08:00
exit 1
}
2025-10-31 07:50:41 +08:00
show_dd_password_tips() {
warn false "
This password is only used for SSH access to view logs during the installation.
Password of the image will NOT modify.
密码仅用于安装过程中通过 SSH 查看日志。
镜像的密码不会被修改。
"
}
2025-10-31 07:50:45 +08:00
show_url_in_args() {
while [ $# -gt 0 ] ; do
case " $1 " in
[ Hh][ Tt][ Tt][ Pp][ Ss] ://* | [ Hh][ Tt][ Tt][ Pp] ://* | [ Mm][ Aa][ Gg][ Nn][ Ee][ Tt] :*) echo " $1 " ;;
esac
shift
done
}
2023-06-18 21:27:22 +08:00
curl() {
2025-03-06 21:04:50 +08:00
is_have_cmd curl || install_pkg curl
2025-10-31 07:50:45 +08:00
# 显示 url
show_url_in_args " $@ " >& 2
2023-09-10 22:23:02 +08:00
# 添加 -f, --fail,不然 404 退出码也为0
2023-10-22 19:07:12 +08:00
# 32位 cygwin 已停止更新,证书可能有问题,先添加 --insecure
2024-03-05 23:01:24 +08:00
# centos 7 curl 不支持 --retry-connrefused --retry-all-errors
# 因此手动 retry
for i in $( seq 5) ; do
if command curl --insecure --connect-timeout 10 -f " $@ " ; then
return
else
ret = $?
2024-07-09 23:37:51 +08:00
# 403 404 错误,或者达到重试次数
if [ $ret -eq 22 ] || [ $i -eq 5 ] ; then
2024-03-05 23:01:24 +08:00
return $ret
fi
2024-07-09 23:37:51 +08:00
sleep 1
2024-03-05 23:01:24 +08:00
fi
done
2023-06-18 21:27:22 +08:00
}
2025-02-24 22:05:27 +08:00
mask2cidr() {
local x = ${ 1 ##*255. }
set -- 0^^^128^192^224^240^248^252^254^ $(( ( ${# 1 } - ${# x } ) * 2 )) ${ x %%.* }
x = ${ 1 %% " $3 " * }
echo $(( $2 + ( ${# x } / 4 )) )
}
2023-05-03 22:22:21 +08:00
is_in_china() {
2025-02-06 18:47:17 +08:00
[ " $force_cn " = 1 ] && return 0
2024-08-07 21:46:45 +08:00
if [ -z " $_loc " ] ; then
2025-02-21 00:30:36 +08:00
# www.cloudflare.com/dash.cloudflare.com 国内访问的是美国服务器,而且部分地区被墙
2025-03-27 12:02:44 +08:00
# 没有ipv6 www.visa.cn
# 没有ipv6 www.bose.cn
# 没有ipv6 www.garmin.com.cn
2025-02-24 22:05:27 +08:00
# 备用 www.prologis.cn
# 备用 www.autodesk.com.cn
# 备用 www.keysight.com.cn
2025-03-27 12:02:44 +08:00
if ! _loc = $( curl -L http://www.qualcomm.cn/cdn-cgi/trace | grep '^loc=' | cut -d= -f2 | grep .) ; then
2024-08-07 21:46:45 +08:00
error_and_exit "Can not get location."
fi
2025-03-06 20:45:31 +08:00
echo "Location: $_loc " >& 2
2023-05-13 00:14:46 +08:00
fi
2024-08-07 21:46:45 +08:00
[ " $_loc " = CN ]
2023-05-03 22:22:21 +08:00
}
2023-06-18 21:27:22 +08:00
is_in_windows() {
[ " $( uname -o) " = Cygwin ] || [ " $( uname -o) " = Msys ]
}
2023-07-15 23:25:42 +08:00
is_in_alpine() {
[ -f /etc/alpine-release ]
}
2023-07-18 00:22:51 +08:00
is_use_cloud_image() {
[ -n " $cloud_image " ] && [ " $cloud_image " = 1 ]
}
2024-09-03 19:01:32 +08:00
is_force_use_installer() {
[ -n " $installer " ] && [ " $installer " = 1 ]
}
2023-07-28 21:27:16 +08:00
is_use_dd() {
[ " $distro " = dd ]
}
2023-09-10 22:23:03 +08:00
2024-09-09 22:56:13 +08:00
is_boot_in_separate_partition() {
mount | grep -q ' on /boot type '
}
2023-07-23 11:46:31 +08:00
is_os_in_btrfs() {
2024-09-09 22:56:13 +08:00
mount | grep -q ' on / type btrfs '
2023-07-23 11:46:31 +08:00
}
2023-08-01 22:13:45 +08:00
is_os_in_subvol() {
subvol = $( awk '($2=="/") { print $i }' /proc/mounts | grep -o 'subvol=[^ ]*' | cut -d= -f2)
[ " $subvol " != / ]
}
get_os_part() {
awk '($2=="/") { print $1 }' /proc/mounts
}
2025-04-15 23:40:58 +08:00
umount_all() {
# windows defender 打开时,cygwin 运行 mount 很慢,但 cat /proc/mounts 很快
if mount_lists = $( mount | grep -w "on $1 " | awk '{print $3}' | grep .) ; then
# alpine 没有 -R
if umount --help 2>& 1 | grep -wq -- '-R' ; then
umount -R " $1 "
else
echo " $mount_lists " | tac | xargs -n1 umount
fi
fi
}
2023-08-01 22:13:45 +08:00
cp_to_btrfs_root() {
2024-04-03 21:51:18 +08:00
mount_dir = $tmp /reinstall-btrfs-root
2023-08-01 22:13:45 +08:00
if ! grep -q $mount_dir /proc/mounts; then
mkdir -p $mount_dir
mount " $( get_os_part) " $mount_dir -t btrfs -o subvol = /
2023-07-23 11:46:31 +08:00
fi
2025-04-15 23:40:58 +08:00
cp -rf " $@ " " $mount_dir "
2023-07-23 11:46:31 +08:00
}
2023-07-15 23:30:13 +08:00
is_host_has_ipv4_and_ipv6() {
2023-09-10 22:23:03 +08:00
host = $1
2023-07-15 23:30:13 +08:00
install_pkg dig
# dig会显示cname结果,cname结果以.结尾,grep -v '\.$' 用于去除 cname 结果
res = $( dig +short $host A $host AAAA | grep -v '\.$' )
# 有.表示有ipv4地址,有:表示有ipv6地址
grep -q \. <<< $res && grep -q : <<< $res
}
2023-11-09 22:27:07 +08:00
is_netboot_xyz() {
[ " $distro " = netboot.xyz ]
}
2023-12-04 22:19:59 +08:00
is_alpine_live() {
[ " $distro " = alpine ] && [ " $hold " = 1 ]
}
2023-11-09 22:27:07 +08:00
is_have_initrd() {
! is_netboot_xyz
}
2024-05-03 21:37:07 +08:00
is_use_firmware() {
2024-05-08 22:17:33 +08:00
# shellcheck disable=SC2154
[ " $nextos_distro " = debian ] && ! is_virt
2024-05-03 21:37:07 +08:00
}
2024-10-12 23:07:01 +08:00
is_digit() {
[[ " $1 " = ~ ^[ 0-9] +$ ]]
}
is_port_valid() {
is_digit " $1 " && [ " $1 " -ge 1 ] && [ " $1 " -le 65535 ]
}
2023-07-15 23:30:13 +08:00
get_host_by_url() {
cut -d/ -f3 <<< $1
}
2026-02-13 00:03:36 +08:00
get_scheme_and_host_by_url() {
cut -d/ -f1-3 <<< $1
}
2024-06-10 22:17:49 +08:00
get_function() {
declare -f " $1 "
}
2023-10-22 19:07:12 +08:00
get_function_content() {
declare -f " $1 " | sed '1d;2d;$d'
}
2023-09-16 20:00:05 +08:00
insert_into_file() {
2025-10-31 07:50:40 +08:00
local file = $1
local location = $2
local regex_to_find = $3
shift 3
2023-09-16 20:00:05 +08:00
2025-10-31 07:50:40 +08:00
if ! [ -f " $file " ] ; then
error_and_exit "File not found: $file "
fi
# 默认 grep -E
if [ $# -eq 0 ] ; then
set -- -E
fi
line_num = $( grep " $@ " -n " $regex_to_find " " $file " | cut -d: -f1)
2023-10-22 19:07:12 +08:00
found_count = $( echo " $line_num " | wc -l)
if [ ! " $found_count " -eq 1 ] ; then
2023-09-16 20:00:05 +08:00
return 1
fi
2023-10-22 19:07:12 +08:00
case " $location " in
before) line_num = $(( line_num - 1 )) ;;
after) ;;
*) return 1 ;;
esac
2023-09-16 20:00:05 +08:00
sed -i " ${ line_num } r /dev/stdin" " $file "
}
2023-06-04 22:10:16 +08:00
test_url() {
2023-09-16 20:00:06 +08:00
test_url_real false " $@ "
}
test_url_grace() {
test_url_real true " $@ "
}
test_url_real() {
grace = $1
url = $2
2024-09-07 20:53:00 +08:00
expect_types = $3
2023-09-16 20:00:06 +08:00
var_to_eval = $4
2023-06-18 21:27:22 +08:00
info test url
2023-06-05 19:50:21 +08:00
2023-09-16 20:00:06 +08:00
failed() {
$grace && return 1
error_and_exit " $@ "
}
2023-09-10 22:23:02 +08:00
2024-07-14 23:00:52 +08:00
tmp_file = $tmp /img-test
2023-10-22 16:38:21 +08:00
2024-08-19 00:42:12 +08:00
# TODO: 好像无法识别 nixos 官方源的跳转
2023-10-22 16:38:21 +08:00
# 有的服务器不支持 range,curl会下载整个文件
2024-09-06 23:00:32 +08:00
# 所以用 head 限制 1M
# 过滤 curl 23 错误(head 限制了大小)
2023-10-22 16:38:21 +08:00
# 也可用 ulimit -f 但好像 cygwin 不支持
2024-09-06 23:00:32 +08:00
# ${PIPESTATUS[n]} 表示第n个管道的返回值
2024-03-17 00:08:48 +08:00
echo $url
for i in $( seq 5 -1 0) ; do
if command curl --insecure --connect-timeout 10 -Lfr 0-1048575 " $url " \
2024-09-06 23:00:32 +08:00
1> >( exec head -c 1048576 >$tmp_file ) \
2024-08-24 17:28:17 +08:00
2> >( exec grep -v 'curl: (23)' >& 2) ; then
2024-03-17 00:08:48 +08:00
break
else
ret = $?
msg = " $url not accessible"
case $ret in
2025-01-01 17:38:30 +08:00
22)
# 403 404
# 这里的 failed 虽然返回 1,但是不会中断脚本,因此要手动 return
failed " $msg "
return " $ret "
;;
23)
# 限制了空间
break
;;
*)
# 其他错误
if [ $i -eq 0 ] ; then
failed " $msg "
return " $ret "
fi
;;
2024-03-17 00:08:48 +08:00
esac
sleep 1
2023-10-22 16:38:21 +08:00
fi
2024-03-17 00:08:48 +08:00
done
2023-06-04 22:10:16 +08:00
2024-09-07 20:53:00 +08:00
# 如果要检查文件类型
if [ -n " $expect_types " ] ; then
2023-07-15 23:28:20 +08:00
install_pkg file
2024-09-07 20:53:00 +08:00
real_type = $( file_enhanced $tmp_file )
2024-10-24 21:28:50 +08:00
echo "File type: $real_type "
2023-06-05 19:50:21 +08:00
2025-03-06 20:45:31 +08:00
# debian 9 ubuntu 16.04-20.04 可能会将 iso 识别成 raw
2025-03-04 00:34:49 +08:00
for type in $expect_types $( [ " $expect_types " = iso ] && echo raw) ; do
2024-10-24 21:28:50 +08:00
if [[ ." $real_type " = *." $type " ]] ; then
# 如果要设置变量
if [ -n " $var_to_eval " ] ; then
IFS = . read -r " ${ var_to_eval ? } " " ${ var_to_eval } _warp" <<< " $real_type "
fi
return
fi
done
2024-09-06 23:00:32 +08:00
2024-10-24 21:28:50 +08:00
failed " $url
Expected type: $expect_types
Actually type: $real_type "
2024-09-07 20:53:00 +08:00
fi
}
fix_file_type() {
# gzip的mime有很多种写法
# centos7中显示为 x-gzip,在其他系统中显示为 gzip,可能还有其他
# 所以不用mime判断
# https://www.digipres.org/formats/sources/tika/formats/#application/gzip
2024-10-24 21:28:50 +08:00
# centos 7 上的 file 显示 qcow2 的 mime 为 application/octet-stream
# file debian-12-genericcloud-amd64.qcow2
# debian-12-genericcloud-amd64.qcow2: QEMU QCOW Image (v3), 2147483648 bytes
# file --mime debian-12-genericcloud-amd64.qcow2
# debian-12-genericcloud-amd64.qcow2: application/octet-stream; charset=binary
2024-09-07 20:53:00 +08:00
# --extension 不靠谱
# file -b /reinstall-tmp/img-test --mime-type
# application/x-qemu-disk
# file -b /reinstall-tmp/img-test --extension
# ???
2024-10-24 21:28:50 +08:00
# 1. 删除,;#
# DOS/MBR boot sector; partition 1: ...
# gzip compressed data, was ...
# # ISO 9660 CD-ROM filesystem data... (有些 file 版本开头输出有井号)
2024-09-07 20:53:00 +08:00
2024-10-24 21:28:50 +08:00
# 2. 删除开头的空格
# 3. 删除无意义的单词 POSIX, Unicode, UTF-8, ASCII
# POSIX tar archive (GNU)
# Unicode text, UTF-8 text
# UTF-8 Unicode text, with very long lines
# ASCII text
# 4. 下面两种都是 raw
2024-09-07 20:53:00 +08:00
# DOS/MBR boot sector
# x86 boot sector; partition 1: ...
2024-10-24 21:28:50 +08:00
sed -E \
-e 's/[,;#]//g' \
-e 's/^[[:space:]]*//' \
-e 's/(POSIX|Unicode|UTF-8|ASCII)//gi' \
2026-03-10 00:09:05 +08:00
-e 's/^DOS\/MBR boot sector/raw/i' \
-e 's/^x86 boot sector/raw/i' \
-e 's/^Zstandard/zstd/i' \
-e 's/^UDF/iso/i' \
-e 's/^Windows imaging \(WIM\) image/wim/i' |
2024-10-24 21:28:50 +08:00
awk '{print $1}' | to_lower
2024-09-07 20:53:00 +08:00
}
2024-10-24 21:28:50 +08:00
# 不用 file -z,因为
# 1. file -z 只能看透一层
# 2. alpine file -z 无法看透部分镜像(前1M),例如:
# guajibao-win10-ent-ltsc-2021-x64-cn-efi.vhd.gz
# guajibao-win7-sp1-ent-x64-cn-efi.vhd.gz
# win7-ent-sp1-x64-cn-efi.vhd.gz
# 还要注意 centos 7 没有 -Z 只有 -z
2024-09-07 20:53:00 +08:00
file_enhanced() {
2024-10-24 21:28:50 +08:00
file = $1
2024-09-07 20:53:00 +08:00
2024-10-24 21:28:50 +08:00
full_type =
while true; do
type = " $( file -b $file | fix_file_type) "
full_type = " $type . $full_type "
case " $type " in
xz | gzip | zstd)
install_pkg " $type "
$type -dc <" $file " | head -c 1048576 >" $file .inside"
mv -f " $file .inside" " $file "
;;
tar)
install_pkg " $type "
# 隐藏 gzip: unexpected end of file 提醒
tar xf " $file " -O 2>/dev/null | head -c 1048576 >" $file .inside"
mv -f " $file .inside" " $file "
;;
*)
break
;;
esac
done
# shellcheck disable=SC2001
echo " $full_type " | sed 's/\.$//'
2023-06-04 22:10:16 +08:00
}
2026-04-25 23:16:52 +08:00
# trans.sh 有相同方法
2023-06-18 21:27:22 +08:00
add_community_repo_for_alpine() {
2026-04-25 23:16:52 +08:00
local ver mirror
2024-11-23 23:50:13 +08:00
2026-04-25 23:16:52 +08:00
# 先检查原来的 repo 是不是 edge 或者 latest-stable
if grep -q "^http.*/edge/main $" /etc/apk/repositories; then
ver = edge
elif grep -q "^http.*/latest-stable/main $" /etc/apk/repositories; then
ver = latest-stable
2023-08-25 13:04:06 +08:00
else
2026-04-25 23:16:52 +08:00
ver = v$( cut -d. -f1,2 </etc/alpine-release)
2023-08-25 13:04:06 +08:00
fi
2026-04-25 23:16:52 +08:00
if ! grep -q "^http.*/ $ver /community $" /etc/apk/repositories; then
2023-10-27 11:50:06 +08:00
mirror = $( grep '^http.*/main$' /etc/apk/repositories | sed 's,/[^/]*/main$,,' | head -1)
2026-04-25 23:16:52 +08:00
echo $mirror /$ver /community >>/etc/apk/repositories
2023-07-05 21:57:27 +08:00
fi
2023-06-18 21:27:22 +08:00
}
2026-03-10 00:09:05 +08:00
is_in_container() {
{ is_have_cmd systemd-detect-virt && systemd-detect-virt -qc; } ||
[ -d /proc/vz ] ||
{ [ -f /proc/1/environ ] && grep -q container = lxc /proc/1/environ; }
2023-10-22 16:39:35 +08:00
}
2024-11-23 23:50:11 +08:00
# 使用 | del_br ,但返回 del_br 之前返回值
run_with_del_cr() {
2024-11-29 22:13:53 +08:00
if false; then
# ash 不支持 PIPESTATUS[n]
res = $( " $@ " ) && ret = 0 || ret = $?
echo " $res " | del_cr
return $ret
else
" $@ " | del_cr
return ${ PIPESTATUS [0] }
fi
2024-11-23 23:50:11 +08:00
}
run_with_del_cr_template() {
2025-03-10 22:23:37 +08:00
if get_function _$exe >/dev/null; then
2024-11-23 23:50:11 +08:00
run_with_del_cr _$exe " $@ "
else
run_with_del_cr command $exe " $@ "
fi
}
2025-03-10 22:23:37 +08:00
wmic() {
2024-11-23 23:50:11 +08:00
if is_have_cmd wmic; then
# 如果参数没有 GET,添加 GET,防止以下报错
# wmic memorychip /format:list
# 此级别的开关异常。
has_get = false
for i in " $@ " ; do
# 如果参数有 GET
if [ " $( to_upper <<< " $i " ) " = GET ] ; then
has_get = true
break
fi
done
# 输出为 /format:list 格式
if $has_get ; then
command wmic " $@ " /format:list
else
command wmic " $@ " get /format:list
fi
return
fi
# powershell wmi 默认参数
local namespace = 'root\cimv2'
local class =
local filter =
local props =
# namespace
if [[ " $( to_upper <<< " $1 " ) " = /NAMESPACE* ]] ; then
# 删除引号,删除 \\
namespace = $( cut -d: -f2 <<< " $1 " | sed -e "s/[\"']//g" -e 's/\\\\//g' )
shift
fi
# class
if [[ " $( to_upper <<< " $1 " ) " = PATH ]] ; then
class = $2
shift 2
else
2026-03-10 00:09:05 +08:00
# wmic alias list brief
2024-11-23 23:50:11 +08:00
case " $( to_lower <<< " $1 " ) " in
nicconfig) class = Win32_NetworkAdapterConfiguration ;;
memorychip) class = Win32_PhysicalMemory ;;
*) class = Win32_$1 ;;
esac
shift
fi
# filter
if [[ " $( to_upper <<< " $1 " ) " = WHERE ]] ; then
filter = $2
shift 2
fi
# props
if [[ " $( to_upper <<< " $1 " ) " = GET ]] ; then
props = $2
shift 2
fi
if ! [ -f " $tmp /wmic.ps1" ] ; then
curl -Lo " $tmp /wmic.ps1" " $confhome /wmic.ps1"
fi
powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass \
-File " $( cygpath -w " $tmp /wmic.ps1" ) " \
2024-11-29 22:13:53 +08:00
-Namespace " $namespace " \
-Class " $class " \
2026-04-28 07:33:08 +08:00
${ filter :+ "-Filter" } ${ filter :+ " $filter " } \
${ props :+ "-Properties" } ${ props :+ " $props " }
2024-11-23 23:50:11 +08:00
}
2023-06-18 21:27:22 +08:00
is_virt() {
2023-11-09 22:27:07 +08:00
if [ -z " $_is_virt " ] ; then
if is_in_windows; then
# https://github.com/systemd/systemd/blob/main/src/basic/virt.c
# https://sources.debian.org/src/hw-detect/1.159/hw-detect.finish-install.d/08hw-detect/
2026-09-06 11:53:44 +08:00
vmstr = 'VMware|VirtualBox|VMW|Hyper-V|Bochs|QEMU|KVM|OpenStack|KubeVirt|innotek|Xen|HVM|Parallels|BHYVE|OVMF'
vmstr += '|virt|Virtual|Virtualization'
2023-11-09 22:27:07 +08:00
for name in ComputerSystem BIOS BaseBoard; do
2024-11-23 23:50:11 +08:00
if wmic $name | grep -Eiw $vmstr ; then
2023-11-09 22:27:07 +08:00
_is_virt = true
break
fi
done
2024-01-26 20:57:12 +08:00
2025-01-01 17:38:31 +08:00
# 用运行 windows ,肯定够内存运行 alpine lts netboot
# 何况还能停止 modloop
2024-01-26 20:57:12 +08:00
# 没有风扇和温度信息,大概是虚拟机
2025-01-01 17:38:31 +08:00
# 阿里云 倚天710 arm 有温度传感器
# ovh KS-LE-3 没有风扇和温度信息?
if false && [ -z " $_is_virt " ] &&
2024-11-23 23:50:11 +08:00
! wmic /namespace:'\\root\cimv2' PATH Win32_Fan 2>/dev/null | grep -q ^Name &&
! wmic /namespace:'\\root\wmi' PATH MSAcpi_ThermalZoneTemperature 2>/dev/null | grep -q ^Name; then
2024-01-26 20:57:12 +08:00
_is_virt = true
2023-11-09 22:27:07 +08:00
fi
else
# aws t4g debian 11
# systemd-detect-virt: 为 none,即使装了dmidecode
# virt-what: 未装 deidecode时结果为空,装了deidecode后结果为aws
# 所以综合两个命令的结果来判断
if is_have_cmd systemd-detect-virt && systemd-detect-virt -v; then
_is_virt = true
fi
2024-01-27 23:17:50 +08:00
2023-11-09 22:27:07 +08:00
if [ -z " $_is_virt " ] ; then
# debian 安装 virt-what 不会自动安装 dmidecode,因此结果有误
install_pkg dmidecode virt-what
# virt-what 返回值始终是0,所以用是否有输出作为判断
if [ -n " $( virt-what) " ] ; then
_is_virt = true
fi
fi
2023-06-18 21:27:22 +08:00
fi
2023-11-09 22:27:07 +08:00
if [ -z " $_is_virt " ] ; then
_is_virt = false
fi
2024-10-24 21:28:50 +08:00
echo "VM: $_is_virt "
2023-06-18 21:27:22 +08:00
fi
2023-11-09 22:27:07 +08:00
$_is_virt
2023-06-18 21:27:22 +08:00
}
2025-05-29 20:48:30 +08:00
is_cpu_supports_x86_64_v3() {
2025-01-01 17:38:33 +08:00
# 用 ld.so/cpuid/coreinfo.exe 更准确
# centos 7 /usr/lib64/ld-linux-x86-64.so.2 没有 --help
# alpine gcompat /lib/ld-linux-x86-64.so.2 没有 --help
# https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels
# https://learn.microsoft.com/sysinternals/downloads/coreinfo
# abm = popcnt + lzcnt
# /proc/cpuinfo 不显示 lzcnt, 可用 abm 代替,但 cygwin 也不显示 abm
# /proc/cpuinfo 不显示 osxsave, 故用 xsave 代替
2026-09-10 01:06:52 +08:00
# 在 32 位 cygwin 上也能正常识别
2025-01-01 17:38:33 +08:00
need_flags = "avx avx2 bmi1 bmi2 f16c fma movbe xsave"
had_flags = $( grep -m 1 ^flags /proc/cpuinfo | awk -F': ' '{print $2}' )
for flag in $need_flags ; do
if ! grep -qw $flag <<< " $had_flags " ; then
2025-05-29 20:48:30 +08:00
return 1
2025-01-01 17:38:33 +08:00
fi
done
}
2025-05-29 20:48:30 +08:00
assert_cpu_supports_x86_64_v3() {
if ! is_cpu_supports_x86_64_v3; then
error_and_exit "Could not install $distro $releasever because the CPU does not support x86-64-v3."
fi
}
2026-09-06 11:31:38 +08:00
get_http_log_url() {
echo "http://IP $( [ " ${ web_port :- 80 } " = 80 ] || echo :$web_port ) $web_path "
}
2026-08-06 22:13:07 +08:00
# 判断语言字符是否合法,允许全名和缩写
is_valid_lang_chars() {
[[ " $1 " = ~ ^[ A-Za-z_-] +$ ]]
2024-04-05 22:45:59 +08:00
}
2026-08-06 22:13:07 +08:00
lang_convert() {
local out_format = $1
local in = $lang
if ! is_valid_lang_chars " $in " ; then
2024-04-05 22:45:59 +08:00
return
fi
2026-08-06 22:13:07 +08:00
# 如果是 fallback_ 开头的,先得到 fallback 后的 cc-cc
if [[ " $out_format " = fallback_* ]] ; then
in = $( lang_convert_inner " $in " fallback_cc-cc)
# 如果要输出 fallback_cc-cc ,直接输出
if [ " $out_format " = fallback_cc-cc ] ; then
printf '%s' " $in "
return
fi
# 去除 fallback_ 前缀
out_format = ${ out_format #fallback_ }
fi
2024-04-05 22:45:59 +08:00
2026-08-06 22:13:07 +08:00
# 尝试转换成目标格式
local out
out = $( lang_convert_inner " $in " " $out_format " )
# 如果转换成功,则表示输入的语言在列表里面
if [ -n " $out " ] ; then
printf '%s' " $out "
else
# 如果没有,则手动截取
case " $out_format " in
cc) cut -d- -f1 <<< " $in " ;;
cc-cc) cut -d- -f1-2 <<< " $in " ;;
cc-cc-cc) cut -d- -f1-3 <<< " $in " ;;
esac
2024-04-05 22:45:59 +08:00
fi
}
2026-08-06 22:13:07 +08:00
lang_convert_inner() {
local in = $1
local out_format = $2
2024-04-05 22:45:59 +08:00
2026-08-06 22:13:07 +08:00
if ! is_valid_lang_chars " $in " ; then
2024-04-05 22:45:59 +08:00
return
fi
2026-08-06 22:13:07 +08:00
# 可能得到 / ,用 is_valid_lang_chars 过滤掉
local out
if out = $(
2026-08-15 22:40:23 +08:00
lang_table_with_head | sed 1d | to_lower | awk \
2026-08-06 22:13:07 +08:00
-v val = " $in " \
-v c1 = " $( get_col_number cc) " \
-v c2 = " $( get_col_number cc-cc) " \
-v c3 = " $( get_col_number cc-cc-cc) " \
-v c4 = " $( get_col_number full_language) " \
-v cout = " $( get_col_number " $out_format " ) " \
'$c1 == val || $c2 == val || $c3 == val || $c4 == val { print $cout }'
) && is_valid_lang_chars " $out " ; then
printf '%s' " $out "
fi
2024-04-05 22:45:59 +08:00
}
2026-08-06 22:13:07 +08:00
get_col_number() {
local col_name = $1
# 找出第一行,用 xargs -n 1 转成列,然后用 grep 找到行号,再用 cut 取出行号
2026-08-15 22:40:23 +08:00
lang_table_with_head | head -1 | xargs -n 1 | grep -Fxn " $col_name " | cut -d: -f1
2026-08-06 22:13:07 +08:00
}
2026-08-15 22:40:23 +08:00
lang_table_with_head() {
2026-08-06 22:13:07 +08:00
# 没有 gb mx 开头的镜像,列出它们作用是,用户输入时识别成 en-gb es-mx
# ca 并非对应 fr-ca
# pt 对应 pt-br,而不是 pt-pt
# uk 对应乌克兰语而不是英国,如果用户输入 uk ,要识别成乌克兰
# 第 5 列是可回落的语言
cat <<EOF
cc cc-cc cc-cc-cc full_language fallback_cc-cc
ar ar-sa / Arabic
bg bg-bg / Bulgarian
cs cs-cz / Czech
da da-dk / Danish
de de-de / German
el el-gr / Greek
gb en-gb / Eng_Intl en-us
en en-us / English
es es-es / Spanish
mx es-mx / Spanish_Latam
et et-ee / Estonian
fi fi-fi / Finnish
/ fr-ca / FrenchCanadian fr-fr
fr fr-fr / French
he he-il / Hebrew
hr hr-hr / Croatian
hu hu-hu / Hungarian
it it-it / Italian
ja ja-jp / Japanese
ko ko-kr / Korean
lt lt-lt / Lithuanian
lv lv-lv / Latvian
no nb-no / Norwegian
nl nl-nl / Dutch
pl pl-pl / Polish
pp pt-pt / Portuguese
pt pt-br / Brazilian pt-pt
ro ro-ro / Romanian
ru ru-ru / Russian
sk sk-sk / Slovak
sl sl-si / Slovenian
sr sr-latn sr-latn-rs Serbian_Latin
sv sv-se / Swedish
th th-th / Thai
tr tr-tr / Turkish
uk uk-ua / Ukrainian
cn zh-cn / ChnSimp
tw zh-tw / ChnTrad
hk zh-hk / ChnTrad_Hong_Kong zh-tw
EOF
2024-04-05 22:45:59 +08:00
}
parse_windows_image_name() {
2026-08-15 22:40:23 +08:00
# 将名称改成内部名称
# home basic 改为 homebasic
# home premium 改为 homepremium
# windows server 2008 server 改为 windows longhorn server
# 注意 windows server 2008 r2 serverdatacenter 不用改
image_name = $(
<<< " $image_name " sed \
-e 's/^windows server 2008 server/windows longhorn server/' \
-e 's/home basic$/homebasic/' \
-e 's/home premium$/homepremium/'
)
2024-04-05 22:45:59 +08:00
set -- $image_name
if ! [ " $1 " = windows ] ; then
return 1
fi
shift
if [ " $1 " = server ] ; then
server = server
shift
fi
2024-05-23 10:40:33 +08:00
2024-04-05 22:45:59 +08:00
version = $1
shift
if [ " $1 " = r2 ] ; then
version += " r2"
shift
fi
edition =
2024-07-07 17:07:18 +08:00
while [ $# -gt 0 ] ; do
case " $1 " in
2024-04-05 22:45:59 +08:00
# windows 10 enterprise n ltsc 2021
k | n | kn) ;;
*)
if [ -n " $edition " ] ; then
edition += " "
fi
edition += " $1 "
;;
esac
shift
done
2026-08-15 22:40:23 +08:00
# longhorn 改成 server 2008 用于 iso 查找
if [ " $version " = longhorn ] && [[ " $edition " = server* ]] ; then
server = server
version = 2008
fi
2024-04-05 22:45:59 +08:00
}
2026-08-15 22:40:23 +08:00
is_have_arm64_version() {
2024-04-05 22:45:59 +08:00
case " $version " in
2026-08-15 22:40:23 +08:00
# win8.x 有 arm32 版本,但是我们不支持 arm32
2024-04-05 22:45:59 +08:00
10)
case " $edition " in
2025-10-31 07:50:43 +08:00
home | 'home single language' | pro | education | enterprise | 'pro education' | 'pro for workstations' ) return ;;
2024-04-05 22:45:59 +08:00
'iot enterprise' ) return ;;
2025-10-31 07:50:43 +08:00
# arm ltsc 只有 2021 有 iso
2024-05-22 22:32:43 +08:00
'enterprise ltsc 2021' | 'iot enterprise ltsc 2021' ) return ;;
2024-04-05 22:45:59 +08:00
esac
;;
2026-08-04 21:10:05 +08:00
11)
return
;;
2024-04-05 22:45:59 +08:00
esac
return 1
}
find_windows_iso() {
parse_windows_image_name || error_and_exit "--image-name wrong: $image_name "
2025-11-04 22:17:11 +08:00
if ! { [ " $version " = 8 ] || [ " $version " = 8.1 ] ; } && [ -z " $edition " ] ; then
2026-05-29 07:25:17 +08:00
error_and_exit "Windows Edition is not specified."
2024-04-05 22:45:59 +08:00
fi
if [ -z " $lang " ] ; then
lang = en-us
fi
2026-08-06 22:13:07 +08:00
# 用户输入的语言最优先
langs = $lang
langs += " $( lang_convert cc-cc-cc) $( lang_convert cc-cc) $( lang_convert cc) "
langs += " $( lang_convert fallback_cc-cc-cc) $( lang_convert fallback_cc-cc) $( lang_convert fallback_cc) "
langs = $( xargs -n 1 <<< " $langs " | awk '!seen[$0]++' | xargs)
full_langs = " $( lang_convert full_language) $( lang_convert fallback_full_language) "
full_langs = $( xargs -n 1 <<< " $full_langs " | awk '!seen[$0]++' | xargs)
2024-04-05 22:45:59 +08:00
case " $basearch " in
2026-08-04 21:10:05 +08:00
x86) # 备用,查找功能目前不支持 32 位
arch_win = x86
arch_win_vlsc = '32-?bit'
;;
2026-02-12 22:54:47 +08:00
x86_64)
arch_win = x64
2026-08-04 21:10:05 +08:00
arch_win_vlsc = '64-?bit'
2026-02-12 22:54:47 +08:00
;;
aarch64)
arch_win = arm64
arch_win_vlsc = arm64
;;
2024-04-05 22:45:59 +08:00
esac
get_windows_iso_link
}
2025-05-16 20:49:02 +08:00
get_windows_iso_link() {
2024-04-05 23:21:07 +08:00
get_label_msdn() {
2026-08-04 21:10:05 +08:00
case " $version " in
2026-08-15 22:40:23 +08:00
vista)
case " $edition " in
starter)
case " $arch_win " in
x86) echo _ ;;
esac
;;
homebasic | homepremium | ultimate)
echo _
;;
business | enterprise)
# ntriver 的 iso 是 vlsc 的
;;
esac
;;
7)
case " $edition " in
starter)
case " $arch_win " in
x86) echo starter ;;
esac
;;
homebasic)
case " $arch_win " in
# ntriver 没有单独的 win7 homebasic x64 iso
# 可从 homepremium iso 获取
x86) echo "home basic" ;;
x64) echo "home premium" ;;
esac
;;
homepremium)
echo "home premium"
;;
professional | enterprise | ultimate)
echo " $edition "
;;
esac
;;
2026-08-04 21:10:05 +08:00
8 | 8.1)
case " $edition " in
'' ) # windows 8.x core
echo _
;;
pro | enterprise)
echo " $edition "
2024-04-05 22:45:59 +08:00
;;
esac
2026-08-04 21:10:05 +08:00
;;
10)
case " $edition " in
home | 'home single language' )
echo 'consumer editions'
;;
pro | enterprise)
echo 'business editions'
;;
education | 'pro education' | 'pro for workstations' )
case " $arch_win " in
arm64) echo 'consumer editions' ;; # 只能从 consumer 获取
x86 | x64) echo 'business editions' ;; # iso 更小
2024-04-05 22:45:59 +08:00
esac
;;
2026-08-04 21:10:05 +08:00
# iot
'iot enterprise' )
echo 'iot enterprise'
;;
# ltsc
'enterprise 2015 ltsb' | 'enterprise 2016 ltsb' | 'enterprise ltsc 2019' | 'enterprise ltsc 2021' )
case " $arch_win " in
arm64) echo "iot $edition " ;; # 只能从 iot ltsc iso 获取
x86 | x64) echo " $edition " ;;
esac
;;
# iot ltsc
'iot enterprise 2015 ltsb' | 'iot enterprise 2016 ltsb' | 'iot enterprise ltsc 2019' | 'iot enterprise ltsc 2021' )
echo " $edition "
;;
esac
;;
11)
case " $edition " in
home | 'home single language' )
echo 'consumer editions'
;;
pro | enterprise)
echo 'business editions'
;;
education | 'pro education' | 'pro for workstations' )
2025-10-31 07:50:43 +08:00
# arm business iso 都没有 education, pro education, pro for workstations
# 即使它的名字包含 EDU
# SW_DVD9_Win_Pro_10_22H2.31_Arm64_English_Pro_Ent_EDU_N_MLF_X24-05074.ISO
# en-us_windows_11_business_editions_version_25h2_arm64_dvd_8afc9b39.iso
2026-08-04 21:10:05 +08:00
case " $arch_win " in
arm64) echo 'consumer editions' ;; # 只能从 consumer 获取
x64) echo 'business editions' ;; # iso 更小
2024-04-05 22:45:59 +08:00
esac
;;
2026-08-04 21:10:05 +08:00
# iot
'iot enterprise' | 'iot enterprise subscription' )
echo 'iot enterprise'
;;
# ltsc
'enterprise ltsc 2024' )
case " $arch_win " in
arm64) echo "iot $edition " ;; # 只能从 iot ltsc iso 获取
x64) echo " $edition " ;;
esac
;;
# iot ltsc
'iot enterprise ltsc 2024' | 'iot enterprise subscription ltsc 2024' )
echo 'iot enterprise ltsc 2024'
;;
2024-04-05 22:45:59 +08:00
esac
2026-08-04 21:10:05 +08:00
;;
2026-09-06 11:53:44 +08:00
2008 | '2008 r2' )
case " $edition " in
serverweb | serverwebcore) echo _ ;;
serverstandard | serverstandardcore) echo _ ;;
serverenterprise | serverenterprisecore) echo _ ;;
serverdatacenter | serverdatacentercore) echo _ ;;
esac
;;
2026-08-15 22:40:23 +08:00
2012 | '2012 r2' | 2016 | 2019 | 2022 | 2025)
2026-08-04 21:10:05 +08:00
case " $edition " in
2026-08-15 22:40:23 +08:00
serverstandard | serverstandardcore) echo _ ;;
serverdatacenter | serverdatacentercore) echo _ ;;
2026-08-04 21:10:05 +08:00
esac
;;
esac
2024-04-05 23:21:07 +08:00
}
2024-04-05 22:45:59 +08:00
2024-04-05 23:21:07 +08:00
get_label_vlsc() {
2024-04-05 22:45:59 +08:00
case " $version " in
2026-08-04 21:10:05 +08:00
# SW_DVD9_Win_Pro_11_25H2_Arm64_Arabic_Pro_Ent_EDU_N_MLF_X24-13113.iso
# SW_DVD5_WIN_ENT_LTSB_10_2015_64BIT_Arabic_MLF_X20-26578.ISO
# SW_DVD5_WIN_ENT_LTSB_2016_64BIT_Arabic_MLF_X21-07425.ISO
# SW_DVD5_WIN_ENT_LTSC_2019_64-bit_Arabic_MLF_X21-96407.ISO
# SW_DVD9_WIN_ENT_LTSC_2021_64BIT_ChnSimp_MLF_X22-84402.ISO
# SW_DVD9_WIN_ENT_LTSC_2024_64-bit_Arabic_MLF_X23-70037.ISO
# SW_DVD5_Win_10_IOT_Enterprise_2015_LTSB_64Bit_EMB_English_OEM_X20-20063.IMG
# SW_DVD5_Win10_IoT_Enterprise_LTSB_1607_64-bit_EMB_English_OEM_X21-05293.IMG
# SW_DVD9_Win_11_IoT_Enterprise_LTSC_24H2_64-Bit_English_X23-70076.ISO
2026-08-15 22:40:23 +08:00
vista)
case " $edition " in
business | enterprise) echo " $edition " ;;
esac
;;
2026-08-04 21:10:05 +08:00
10)
2024-04-05 22:45:59 +08:00
case " $edition " in
2024-05-22 22:32:43 +08:00
pro | education | enterprise | 'pro education' | 'pro for workstations' ) echo pro ;;
2026-08-04 21:10:05 +08:00
'enterprise 2015 ltsb' ) echo 'ent ltsb 10 2015' ;;
'enterprise 2016 ltsb' ) echo 'ent ltsb 2016' ;;
'enterprise ltsc 2019' ) echo 'ent ltsc 2019' ;;
'enterprise ltsc 2021' ) echo 'ent ltsc 2021' ;;
'iot enterprise 2015 ltsb' ) echo 'iot enterprise 2015 ltsb' ;; # √
'iot enterprise 2016 ltsb' ) echo 'iot enterprise ltsb 1607' ;; # √
'iot enterprise ltsc 2019' ) echo 'iot enterprise ltsc 2019' ;; # 没找到
'iot enterprise ltsc 2021' ) echo 'iot enterprise ltsc 2021' ;; # 没找到
2024-04-05 22:45:59 +08:00
esac
;;
2026-08-04 21:10:05 +08:00
11)
case " $edition " in
pro | education | enterprise | 'pro education' | 'pro for workstations' ) echo pro ;;
'enterprise ltsc 2024' ) echo 'ent ltsc 2024' ;;
'iot enterprise ltsc 2024' | 'iot enterprise subscription ltsc 2024' ) echo 'iot enterprise ltsc 24h2' ;; # √
esac
2026-02-12 22:54:47 +08:00
;;
2024-04-05 22:45:59 +08:00
esac
2024-04-05 23:21:07 +08:00
}
2025-10-28 23:21:41 +08:00
# msdl 没有每月发布的 iso
# msdl 只有 consumer 版本,因此里面的 pro 版本不是 vl 版
# 8.1 没有每月发布的 iso,因此优先从 msdl 下载
# win10 22h2 arm 有每月发布的 iso,因此不从 msdl 下载
# win10/11 ltsc 没有每月发布的 iso,但是 msdl 没有 ltsc 版本
2025-05-16 20:49:02 +08:00
get_label_msdl() {
2026-03-16 22:50:42 +08:00
:
2025-05-16 20:49:02 +08:00
}
2026-08-04 21:10:05 +08:00
get_page_url() {
2026-08-15 22:40:23 +08:00
if [ " $server " = 'server' ] ; then
echo https://ntriver.org/download-windows-server-${ version / /- }
elif is_ltsc; then
echo https://ntriver.org/download-windows-ltsc
2024-04-05 22:45:59 +08:00
else
2026-08-15 22:40:23 +08:00
echo https://ntriver.org/download-windows-$version
2024-04-05 22:45:59 +08:00
fi
2024-04-10 21:41:46 +08:00
}
is_ltsc() {
grep -Ewq 'ltsb|ltsc' <<< " $edition "
}
2026-05-29 15:35:48 +08:00
# 部分 bash 例如 ubuntu 22.04 不支持 $() 里面嵌套case,所以定义成函数
2024-04-10 21:41:46 +08:00
label_msdn = $( get_label_msdn)
2025-05-16 20:49:02 +08:00
label_msdl = $( get_label_msdl)
2024-04-10 21:41:46 +08:00
label_vlsc = $( get_label_vlsc)
2026-08-04 21:10:05 +08:00
page_url = $( get_page_url)
2024-04-05 22:45:59 +08:00
info "Find windows iso"
echo "Version: $version "
echo "Edition: $edition "
echo "Label msdn: $label_msdn "
2025-05-16 20:49:02 +08:00
echo "Label msdl: $label_msdl "
2024-04-05 22:45:59 +08:00
echo "Label vlsc: $label_vlsc "
2026-08-04 21:10:05 +08:00
echo "Page: $page_url "
2026-08-06 22:13:07 +08:00
echo "Languages: $langs $full_langs "
2024-04-05 22:45:59 +08:00
echo
2025-10-31 07:50:43 +08:00
# 先判断是否能自动查找该版本
# 再判断是否支持 arm
# 这样可以在输入错误 Edition 时例如 windows 11 enterprise ltsc 2021
# 显示名称错误,而不是显示该版本不支持 arm
2026-08-04 21:10:05 +08:00
if [ -z " $page_url " ] || { [ -z " $label_msdn " ] && [ -z " $label_msdl " ] && [ -z " $label_vlsc " ] ; } ; then
2025-10-31 07:50:43 +08:00
error_and_exit "Not support find this iso. Check if --image-name is wrong. Or set --iso manually."
fi
2026-08-15 22:40:23 +08:00
if [ " $basearch " = aarch64 ] && ! is_have_arm64_version; then
error_and_exit "No ARM64 iso for this Windows Version or Edition."
2024-04-05 22:45:59 +08:00
fi
2025-05-16 20:49:02 +08:00
if [ -n " $label_msdl " ] ; then
2025-10-31 07:50:45 +08:00
iso = $( curl -L " $page_url " | grep -ioP 'https://[^ ]+?#[0-9]+' | head -1 | grep .)
2024-04-06 21:35:48 +08:00
else
2026-02-13 00:03:36 +08:00
http_to_host = $( get_scheme_and_host_by_url " $page_url " )
http_to_current_dir = $( dirname " $page_url " )
2026-08-04 21:10:05 +08:00
curl -L " $page_url " | tr -d '\n' | # 合成一行
if [[ " $page_url " = ~ massgrave.dev ]] ; then
sed -e 's,<a ,\n<a ,g' -e 's,</a>,</a>\n,g' | # 使每个 <a></a> 占一行
grep -Ei '\.(iso|img)</a>$' | # 找出是 iso 或 img 的行
sed -E 's,<a href="?([^" ]+)"?.+>(.+)</a>,\2 \1,' # 提取文件名和链接
else
sed -e 's,<td><strong>,\n<td><strong>,g' -e 's,</a>,</a>\n,g' | # 使每个镜像占一行
grep -Ei '\.(iso|img)</strong>' | # 找出是 iso 或 img 的行
sed -E 's,<td><strong>([^<]+).+<a href="?([^" ]+)"?.+,\1 \2,' # 提取文件名和链接
fi |
2026-02-13 00:03:36 +08:00
# 如果链接是 / 开头,则补全域名
# 如果链接非 https:// 开头,则补全域名和目录
2026-08-04 21:10:05 +08:00
sed -E "s, (/), $http_to_host \1," |
awk '{if ($2 !~ /^https?:\/\//) $2 = "' $http_to_current_dir /'" $2; print}' |
2025-05-16 20:49:02 +08:00
2026-08-04 21:10:05 +08:00
# 如果不是 ltsc ,应该先去除 ltsc 链接,否则最终链接有 ltsc 的
# 例如查找 windows 10 iot enterprise,会得到
# en-us_windows_10_iot_enterprise_ltsc_2021_arm64_dvd_e8d4fc46.iso
# en-us_windows_10_iot_enterprise_version_22h2_arm64_dvd_39566b6b.iso
if is_ltsc; then
grep -Ei '_lts[bc]_'
else
grep -Ei -v '_lts[bc]_'
fi >$tmp /win.list
2025-05-16 20:49:02 +08:00
get_windows_iso_link_inner
2024-04-06 21:35:48 +08:00
fi
2024-04-05 22:45:59 +08:00
}
get_shortest_line() {
awk '(NR == 1 || length($0) < length(shortest)) { shortest = $0 } END { print shortest }'
}
2026-01-07 23:10:09 +08:00
get_shortest_line_by_field() {
local field = $1
awk "(NR == 1 || length(\$ $field ) < length(field)) { line = \$0; field = \$ $field } END { print line }"
}
2026-08-04 21:10:05 +08:00
get_best_windows_iso_line() {
local lines
lines = $( cat)
2026-09-06 11:53:44 +08:00
# 排除 debug 版
lines = $( echo " $lines " | grep -Ei -v '_(symbols|debug|debugging|checked)_' )
2026-08-04 21:10:05 +08:00
# 在所有符合的 iso 中
# 先选择 win10/11 大版本更新的 (version 26h1) 或者有 sp 版本的 (sp1, windows_8.1_with_update_)
# 再选择有日期更新的 (updated_july_2026)
# 再选择 vl
# 再按版本号排序选择最新版
# 但是也有例外
# zh-cn_windows_server_2019_x64_dvd_19d65722.iso 2022-11-15
# cn_windows_server_2019_updated_april_2021_x64_dvd_a6dae187.iso 2021-04-20
2026-09-06 11:53:44 +08:00
for key in '(version_[0-9h]{4}|sp[1-9]|service_pack|with_update)' 'updated' 'vl' ; do
if grep_lines = $( grep -Ei "_ ${ key } _" <<< " $lines " ) ; then
2026-08-04 21:10:05 +08:00
lines = $grep_lines
fi
done
echo " $lines " | sort -Vr | head -1
}
2025-05-16 20:49:02 +08:00
get_windows_iso_link_inner() {
2024-04-05 22:45:59 +08:00
regexs =()
# msdn
2024-04-06 21:35:48 +08:00
if [ -n " $label_msdn " ] ; then
if [ " $label_msdn " = _ ] ; then
label_msdn =
fi
for lang in $langs ; do
2026-08-04 21:10:05 +08:00
# en_windows_vista_sp2_x64_dvd_342267.iso
# cn_windows_vista_with_sp2_x64_dvd_x15-36322.iso
# en_windows_8_x64_dvd_915440.iso
# en_windows_8.1_pro_vl_with_update_x64_dvd_6050880.iso
# en_windows_8.1_with_update_x64_dvd_6051480.iso
# en_windows_8.1_n_with_update_x64_dvd_6051677.iso
# en-us_windows_10_iot_enterprise_version_22h2_arm64_dvd_39566b6b.iso
# en-us_windows_11_consumer_editions_version_26h1_updated_july_2026_x64_dvd_f69a9a1e.iso
# en-us_windows_server_2025_updated_july_2026_x64_dvd_4e6f5a42.iso
local prefix =
2024-04-06 21:35:48 +08:00
for i in ${ lang } windows ${ server } ${ version } ${ label_msdn } ; do
if [ -n " $i " ] ; then
2026-08-04 21:10:05 +08:00
prefix += " ${ i } _"
2024-04-06 21:35:48 +08:00
fi
done
2026-08-04 21:10:05 +08:00
# 用于准确匹配,例如防止 2012 匹配到 2012 r2
# 首先匹配 label 后面紧接着这些关键字的
# 然后匹配 label 后面紧接着 x64/arm64 的
# 最后模糊匹配
regexs +=( " ${ prefix } (version|vl|with|updated|sp[0-9])_.* ${ arch_win } .*\.(iso|img)" )
regexs +=( " ${ prefix }${ arch_win } .*\.(iso|img)" )
regexs +=( " ${ prefix } .* ${ arch_win } .*\.(iso|img)" )
2024-04-05 22:45:59 +08:00
done
2024-04-06 21:35:48 +08:00
fi
2024-04-05 22:45:59 +08:00
# vlsc
2026-08-15 22:40:23 +08:00
# SW_DVD5_Windows_Vista_Business_64BIT_Arabic_Full_Int_SP2_MLF_X15-40038.ISO
# SW_DVD5_SA_Win_Vista_Enterprise_64BIT_Arabic_Full_Int_SP2_MLF_X15-40408.ISO
2026-02-12 22:54:47 +08:00
# SW_DVD5_Win_10_IOT_Enterprise_2015_LTSB_64Bit_EMB_English_OEM_X20-20063.IMG
# SW_DVD9_Win_Pro_10_22H2.15_Arm64_English_Pro_Ent_EDU_N_MLF_X23-67223.ISO
# SWDVD9_WinSrvSTDCORE2025_24H2.16_64Bit_English_DC_STD_MLF_RTMUpdJan26_X24-26760.iso
2026-03-10 00:09:05 +08:00
# 先判断 full_lang 是否为空
# 因为假如用户输入的 lang 不正确,full_lang 就为空,正则表达式就无法只匹配当前语言
2026-08-06 22:13:07 +08:00
for full_lang in $full_langs ; do
if [ -n " $label_vlsc " ] && [ -n " $full_lang " ] ; then
2026-08-15 22:40:23 +08:00
regexs +=( "sw_?dvd[59]_(SA_)?win(dows)?_? ${ label_vlsc } _? ${ version } _.* ${ arch_win_vlsc } .*_ ${ full_lang } .*\.(iso|img)" )
regexs +=( "sw_?dvd[59]_(SA_)?win_?(dows)? ${ version } _ ${ label_vlsc } _?.* ${ arch_win_vlsc } .*_ ${ full_lang } .*\.(iso|img)" )
2026-08-06 22:13:07 +08:00
# LTSC 没有 windows 主版本号
# SW_DVD5_WIN_ENT_LTSB_10_2015_64BIT_Arabic_MLF_X20-26578.ISO # 将 ENT_LTSB_10_2015 视为 label
# SW_DVD5_WIN_ENT_LTSB_2016_64BIT_Arabic_MLF_X21-07425.ISO # 将 ENT_LTSB_2016 视为 label
if is_ltsc; then
2026-08-15 22:40:23 +08:00
regexs +=( "sw_?dvd[59]_(SA_)?win(dows)?_? ${ label_vlsc } _?.* ${ arch_win_vlsc } .*_ ${ full_lang } .*\.(iso|img)" )
2026-08-06 22:13:07 +08:00
fi
2026-08-04 21:10:05 +08:00
fi
2026-08-06 22:13:07 +08:00
done
2024-04-05 22:45:59 +08:00
# 查找
for regex in " ${ regexs [@] } " ; do
regex = ${ regex // /_ }
2024-05-22 22:32:43 +08:00
echo "looking for: $regex " >& 2
2026-08-04 21:10:05 +08:00
local matched_lines
if matched_lines = $( grep -Ei "^ $regex " " $tmp /win.list" ) ; then
info "ISO Matched"
cat -n <<< " $matched_lines " >& 2
if line = $( echo " $matched_lines " | get_best_windows_iso_line | grep .) &&
iso = $( awk '{print $2}' <<< " $line " | grep .) ; then
info "ISO Selected"
echo " $line " >& 2
return
fi
2024-04-05 22:45:59 +08:00
fi
done
2024-11-29 22:13:53 +08:00
error_and_exit "Could not find iso for this windows edition or language."
2024-04-05 22:45:59 +08:00
}
2026-06-19 10:53:30 +08:00
set_var() {
# eval 不安全
# 仅 bash 可用
printf -v " $1 " "%s" " $2 "
# 或者
# IFS= read -r "$1" <<<"$2"
}
2023-05-03 22:22:21 +08:00
setos() {
2023-05-13 00:14:46 +08:00
local step = $1
local distro = $2
local releasever = $3
2023-06-18 21:27:22 +08:00
info set $step $distro $releasever
2023-05-13 00:14:46 +08:00
2026-06-19 10:53:30 +08:00
set_osvar() {
set_var " ${ step } _ $1 " " $2 "
}
2023-11-09 22:27:07 +08:00
setos_netboot.xyz() {
if is_efi; then
2023-12-02 22:46:16 +08:00
if [ " $basearch " = aarch64 ] ; then
2026-06-19 10:53:30 +08:00
set_osvar efi https://boot.netboot.xyz/ipxe/netboot.xyz-arm64.efi
2023-11-09 22:27:07 +08:00
else
2026-06-19 10:53:30 +08:00
set_osvar efi https://boot.netboot.xyz/ipxe/netboot.xyz.efi
2023-11-09 22:27:07 +08:00
fi
else
2026-06-19 10:53:30 +08:00
set_osvar vmlinuz https://boot.netboot.xyz/ipxe/netboot.xyz.lkrn
2023-11-09 22:27:07 +08:00
fi
}
2023-05-13 00:14:46 +08:00
setos_alpine() {
2023-12-16 16:41:34 +08:00
is_virt && flavour = virt || flavour = lts
2023-10-08 22:10:05 +08:00
# 不要用https 因为甲骨文云arm initramfs阶段不会从硬件同步时钟,导致访问https出错
if is_in_china; then
2024-08-17 23:33:13 +08:00
mirror = http://mirror.nju.edu.cn/alpine/v$releasever
2023-05-13 00:14:46 +08:00
else
2023-10-08 22:10:05 +08:00
mirror = http://dl-cdn.alpinelinux.org/alpine/v$releasever
2023-05-13 00:14:46 +08:00
fi
2026-06-19 10:53:30 +08:00
set_osvar vmlinuz " $mirror /releases/ $basearch /netboot/vmlinuz- $flavour "
set_osvar initrd " $mirror /releases/ $basearch /netboot/initramfs- $flavour "
set_osvar modloop " $mirror /releases/ $basearch /netboot/modloop- $flavour "
set_osvar repo " $mirror /main"
2023-05-13 00:14:46 +08:00
}
2023-05-03 22:22:21 +08:00
2023-05-17 22:54:26 +08:00
setos_debian() {
2024-10-20 19:38:17 +08:00
is_debian_elts() {
2026-09-06 11:53:44 +08:00
[ " $releasever " -le 11 ]
2024-07-14 22:11:53 +08:00
}
2025-10-31 07:50:45 +08:00
if [ " $releasever " -le 9 ] && [ " $basearch " = aarch64 ] ; then
error_and_exit "Debian $releasever ELTS does not support aarch64."
fi
2025-02-06 18:47:17 +08:00
# 用此标记要是否 elts, 用于安装后修改 elts/etls-cn 源
# shellcheck disable=SC2034
is_debian_elts && elts = 1 || elts = 0
2023-07-18 00:22:51 +08:00
case " $releasever " in
2024-07-14 22:11:53 +08:00
9) codename = stretch ;;
10) codename = buster ;;
2023-09-10 22:23:03 +08:00
11) codename = bullseye ;;
12) codename = bookworm ;;
2025-08-10 08:02:22 +08:00
13) codename = trixie ;;
14) codename = forky ;;
15) codename = duke ;;
2023-07-18 00:22:51 +08:00
esac
2025-02-06 18:47:17 +08:00
if ! is_use_cloud_image && is_debian_elts && is_in_china; then
warn "
Due to the lack of Debian Freexian ELTS instaler mirrors in China, the installation time may be longer.
Continue?
由于没有 Debian Freexian ELTS 国内安装源,安装时间可能会比较长。
继续安装?
"
read -r -p '[y/N]: '
if ! [[ " $REPLY " = [ Yy] ]] ; then
exit
fi
fi
# udeb_mirror 安装时的源
# deb_mirror 安装后要修改成的源
if is_debian_elts; then
if is_in_china; then
# https://github.com/tuna/issues/issues/1999
# nju 也没同步
udeb_mirror = deb.freexian.com/extended-lts
deb_mirror = mirror.nju.edu.cn/debian-elts
initrd_mirror = mirror.nju.edu.cn/debian-archive/debian
else
# 按道理不应该用官方源,但找不到其他源
udeb_mirror = deb.freexian.com/extended-lts
deb_mirror = deb.freexian.com/extended-lts
initrd_mirror = archive.debian.org/debian
fi
else
if is_in_china; then
# ftp.cn.debian.org 不在国内还严重丢包
# https://www.itdog.cn/ping/ftp.cn.debian.org
mirror = mirror.nju.edu.cn/debian
else
mirror = deb.debian.org/debian # fastly
fi
udeb_mirror = $mirror
deb_mirror = $mirror
initrd_mirror = $mirror
fi
# 云镜像和 firmware 下载源
2024-05-03 21:37:07 +08:00
if is_in_china; then
2024-11-30 19:31:13 +08:00
cdimage_mirror = https://mirror.nju.edu.cn/debian-cdimage
2024-05-03 21:37:07 +08:00
else
2024-05-22 22:32:43 +08:00
cdimage_mirror = https://cdimage.debian.org/images # 在瑞典,不是 cdn
# cloud.debian.org 同样在瑞典,不是 cdn
2024-05-03 21:37:07 +08:00
fi
2026-09-07 19:21:11 +08:00
# kernel
if [ " $no_cloud_kernel " = 1 ] ; then
flavour =
else
is_virt && flavour = -cloud || flavour =
# debian 10 云内核 vultr efi vnc 没有显示
# 现在改成手动 --no-cloud-kernel 规避
# [ "$releasever" -le 10 ] && flavour=
# 甲骨文 arm64 cloud 内核 vnc 没有显示
[ " $basearch_alt " = arm64 ] && flavour =
fi
2025-01-10 00:43:25 +08:00
2023-07-18 00:22:51 +08:00
if is_use_cloud_image; then
# cloud image
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
# https://salsa.debian.org/cloud-team/debian-cloud-images/-/tree/master/config_space/bookworm/files/etc/default/grub.d
# cloud 包括各种奇怪的优化,例如不显示 grub 菜单
# 因此使用 nocloud
if false; then
is_virt && ci_type = genericcloud || ci_type = generic
else
ci_type = nocloud
fi
img = $cdimage_mirror /cloud/$codename /latest/debian-$releasever -$ci_type -$basearch_alt .qcow2
2025-01-10 00:43:26 +08:00
fi
2026-09-06 11:31:35 +08:00
set_osvar img " $img "
2023-05-17 22:54:26 +08:00
else
2023-07-18 00:22:51 +08:00
# 传统安装
2025-02-06 18:47:17 +08:00
initrd_dir = dists/$codename /main/installer-$basearch_alt /current/images/netboot/debian-installer/$basearch_alt
2023-08-27 23:52:06 +08:00
2026-06-19 10:53:30 +08:00
set_osvar udeb_mirror " $udeb_mirror "
set_osvar vmlinuz "https:// $initrd_mirror / $initrd_dir /linux"
set_osvar initrd "https:// $initrd_mirror / $initrd_dir /initrd.gz"
set_osvar ks " $confhome /debian.cfg"
set_osvar firmware " $cdimage_mirror /unofficial/non-free/firmware/ $codename /current/firmware.cpio.gz"
set_osvar codename " $codename "
2024-05-03 21:37:07 +08:00
fi
2025-02-06 18:47:17 +08:00
# 官方安装和云镜像都会用到的
2026-06-19 10:53:30 +08:00
set_osvar deb_mirror " $deb_mirror "
set_osvar kernel "linux-image $flavour - $basearch_alt "
2024-05-03 21:37:07 +08:00
}
setos_kali() {
if is_use_cloud_image; then
:
else
# 传统安装
if is_in_china; then
2024-08-17 23:33:13 +08:00
hostname = mirror.nju.edu.cn
2024-05-03 21:37:07 +08:00
else
2026-05-29 07:25:17 +08:00
# http.kali.org (geoip 重定向) 到 kali.download (cf) 或最近的站点
# 文档还说 which is guaranteed to be up-to-date
# 但是目测有可能重定义到一个拉黑了部分 IP 的服务器
# 因此这里用 kali.download (cf)
# https://www.kali.org/docs/community/kali-linux-mirrors/
# https://www.kali.org/docs/general-use/kali-apt-sources/
2024-07-14 22:11:53 +08:00
hostname = kali.download
2024-05-03 21:37:07 +08:00
fi
2026-09-06 11:31:38 +08:00
codename = kali-$releasever
2024-07-14 22:11:53 +08:00
mirror = http://$hostname /kali/dists/$codename /main/installer-$basearch_alt /current/images/netboot/debian-installer/$basearch_alt
2024-05-03 21:37:07 +08:00
is_virt && flavour = -cloud || flavour =
2026-06-19 10:53:30 +08:00
set_osvar vmlinuz " $mirror /linux"
set_osvar initrd " $mirror /initrd.gz"
set_osvar ks " $confhome /debian.cfg"
set_osvar deb_mirror " $hostname /kali"
set_osvar udeb_mirror " $hostname /kali"
set_osvar codename " $codename "
set_osvar kernel "linux-image $flavour - $basearch_alt "
2024-05-03 21:37:07 +08:00
# 缺少 firmware 下载
2023-05-17 22:54:26 +08:00
fi
}
2023-05-03 22:22:21 +08:00
setos_ubuntu() {
2023-11-28 21:47:07 +08:00
case " $releasever " in
2024-07-23 00:15:22 +08:00
18.04) codename = bionic ;;
2023-11-28 21:47:07 +08:00
20.04) codename = focal ;;
22.04) codename = jammy ;;
2024-04-25 22:24:46 +08:00
24.04) codename = noble ;;
2026-04-24 00:12:09 +08:00
26.04) codename = resolute ;;
2023-11-28 21:47:07 +08:00
esac
2023-07-18 00:22:51 +08:00
if is_use_cloud_image; then
# cloud image
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
if is_in_china; then
# 有的源没有 releases 镜像
# https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/releases/
# https://unicom.mirrors.ustc.edu.cn/ubuntu-cloud-images/releases/
# https://mirror.nju.edu.cn/ubuntu-cloud-images/releases/
2024-05-22 22:32:43 +08:00
2026-09-06 11:31:35 +08:00
ci_mirror = https://mirror.nju.edu.cn/ubuntu-cloud-images
else
ci_mirror = https://cloud-images.ubuntu.com
fi
2024-07-23 00:15:22 +08:00
2026-09-06 11:31:35 +08:00
# 以下版本有 minimal 镜像
# amd64 所有
# arm64 24.04 和以上
is_have_minimal_image() {
[ " $basearch_alt " = amd64 ] || [ " ${ releasever %.* } " -ge 24 ]
}
2024-08-11 23:05:53 +08:00
2026-09-06 11:31:35 +08:00
basearch_img = $basearch_alt
if [ " $basearch_alt " = amd64 ] && [ " ${ releasever %.* } " -ge 26 ] && is_cpu_supports_x86_64_v3; then
basearch_img = amd64v3
fi
2024-07-23 00:15:22 +08:00
2026-09-06 11:31:35 +08:00
if [ " $minimal " = 1 ] ; then
if ! is_have_minimal_image; then
error_and_exit "Minimal cloud image is not available for $releasever $basearch_alt ."
fi
img = $ci_mirror /minimal/releases/$codename /release/ubuntu-$releasever -minimal-cloudimg-$basearch_img .img
else
# 用 codename 而不是 releasever,可减少一次跳转
img = $ci_mirror /releases/$codename /release/ubuntu-$releasever -server-cloudimg-$basearch_img .img
2025-03-06 20:45:31 +08:00
fi
2023-11-28 21:47:07 +08:00
fi
2026-09-06 11:31:35 +08:00
set_osvar img " $img "
2023-07-18 00:22:51 +08:00
else
# 传统安装
2023-10-08 22:10:05 +08:00
if is_in_china; then
case " $basearch " in
2024-08-17 23:33:13 +08:00
"x86_64" ) mirror = https://mirror.nju.edu.cn/ubuntu-releases/$releasever ;;
"aarch64" ) mirror = https://mirror.nju.edu.cn/ubuntu-cdimage/releases/$releasever /release ;;
2023-10-08 22:10:05 +08:00
esac
2023-07-18 00:22:51 +08:00
else
2023-10-08 22:10:05 +08:00
case " $basearch " in
"x86_64" ) mirror = https://releases.ubuntu.com/$releasever ;;
"aarch64" ) mirror = https://cdimage.ubuntu.com/releases/$releasever /release ;;
esac
2023-05-03 22:22:21 +08:00
fi
2023-07-18 00:22:51 +08:00
# iso
2025-04-12 15:44:20 +08:00
filename = $( curl -L $mirror / | grep -oP "ubuntu- $releasever .*?-live-server- $basearch_alt .iso" |
2024-11-29 22:13:51 +08:00
sort -uV | tail -1 | grep .)
2023-07-18 00:22:51 +08:00
iso = $mirror /$filename
2023-12-02 22:46:16 +08:00
# 在 ubuntu 20.04 上,file 命令检测 ubuntu 22.04 iso 结果是 DOS/MBR boot sector
2025-03-04 00:34:49 +08:00
test_url " $iso " iso
2026-06-19 10:53:30 +08:00
set_osvar iso " $iso "
2023-07-18 00:22:51 +08:00
# ks
2026-06-19 10:53:30 +08:00
set_osvar ks " $confhome /deprecated/ubuntu.yaml"
2026-06-19 10:53:30 +08:00
set_osvar minimal " $minimal "
2023-07-18 00:22:51 +08:00
fi
2023-05-03 22:22:21 +08:00
}
2023-07-22 22:09:24 +08:00
setos_arch() {
2024-03-31 00:32:05 +08:00
if [ " $basearch " = "x86_64" ] ; then
if is_in_china; then
2024-08-17 23:33:13 +08:00
mirror = https://mirror.nju.edu.cn/archlinux
2024-03-31 00:32:05 +08:00
else
2024-05-22 22:32:43 +08:00
mirror = https://geo.mirror.pkgbuild.com # geoip
2024-03-31 00:32:05 +08:00
fi
2023-07-22 22:09:24 +08:00
else
2024-03-31 00:32:05 +08:00
if is_in_china; then
2024-08-17 23:33:13 +08:00
mirror = https://mirror.nju.edu.cn/archlinuxarm
2024-03-31 00:32:05 +08:00
else
# https 证书有问题
2024-05-22 22:32:43 +08:00
mirror = http://mirror.archlinuxarm.org # geoip
2024-03-31 00:32:05 +08:00
fi
fi
if is_use_cloud_image; then
# cloud image
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
img = $mirror /images/latest/Arch-Linux-x86_64-cloudimg.qcow2
fi
set_osvar img " $img "
2024-03-31 00:32:05 +08:00
else
# 传统安装
case " $basearch " in
x86_64) dir = "core/os/ $basearch " ;;
aarch64) dir = " $basearch /core" ;;
esac
test_url $mirror /$dir /core.db gzip
2026-06-19 10:53:30 +08:00
set_osvar mirror " $mirror "
2023-07-22 22:09:24 +08:00
fi
}
2023-08-01 22:23:21 +08:00
2024-08-19 00:42:12 +08:00
setos_nixos() {
if is_in_china; then
mirror = https://mirror.nju.edu.cn/nix-channels
else
mirror = https://nixos.org/channels
fi
if is_use_cloud_image; then
:
else
# 传统安装
2024-08-23 00:15:16 +08:00
# 该服务器文件缓存 miss 时会响应 206 + Location 头
2024-10-24 21:28:50 +08:00
# 但 curl 这种情况不会重定向,所以添加 text 类型让它不要报错
test_url $mirror /nixos-$releasever /store-paths.xz 'xz text'
2026-06-19 10:53:30 +08:00
set_osvar mirror " $mirror "
2024-08-19 00:42:12 +08:00
fi
}
2023-09-16 20:00:07 +08:00
setos_gentoo() {
if is_in_china; then
2024-08-17 23:33:13 +08:00
mirror = https://mirror.nju.edu.cn/gentoo
2023-09-16 20:00:07 +08:00
else
2024-05-22 22:32:43 +08:00
mirror = https://distfiles.gentoo.org # cdn77
2023-09-16 20:00:07 +08:00
fi
2025-02-06 18:47:21 +08:00
dir = releases/$basearch_alt /autobuilds
2023-09-16 20:00:07 +08:00
2025-02-06 18:47:21 +08:00
if is_use_cloud_image; then
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
# 使用 systemd 且没有 cloud-init
prefix = di-$basearch_alt -console
filename = $( curl -L $mirror /$dir /latest-$prefix .txt | grep '.qcow2' | awk '{print $1}' | grep .)
img = $mirror /$dir /$filename
fi
test_url " $img " 'qemu'
2024-03-28 00:16:05 +08:00
else
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
prefix = stage3-$basearch_alt -systemd
filename = $( curl -L $mirror /$dir /latest-$prefix .txt | grep '.tar.xz' | awk '{print $1}' | grep .)
img = $mirror /$dir /$filename
test_url " $img " 'tar.xz'
fi
2024-03-28 00:16:05 +08:00
fi
2026-09-06 11:31:35 +08:00
set_osvar img " $img "
2023-09-16 20:00:07 +08:00
}
2023-08-01 22:23:21 +08:00
setos_opensuse() {
2023-09-10 22:23:04 +08:00
# https://download.opensuse.org/
2025-03-10 22:23:37 +08:00
# curl 会跳转到最近的镜像源,但可能会被镜像源 block
# aria2 会跳转使用 metalink
# https://downloadcontent.opensuse.org # 德国
# https://downloadcontentcdn.opensuse.org # fastly cdn
2023-09-10 22:23:04 +08:00
2023-12-02 23:10:05 +08:00
# 很多国内源缺少 aarch64 tumbleweed appliances
# https://download.opensuse.org/ports/aarch64/tumbleweed/appliances/
# https://mirrors.ustc.edu.cn/opensuse/ports/aarch64/tumbleweed/appliances/
# https://mirrors.tuna.tsinghua.edu.cn/opensuse/ports/aarch64/tumbleweed/appliances/
2023-09-10 22:23:04 +08:00
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
if is_in_china; then
mirror = https://mirror.nju.edu.cn/opensuse
2023-09-10 22:23:04 +08:00
else
2026-09-06 11:31:35 +08:00
mirror = https://downloadcontentcdn.opensuse.org
2023-08-01 22:23:21 +08:00
fi
2026-09-06 11:31:35 +08:00
if [ " $releasever " = tumbleweed ] ; then
# tumbleweed
if [ " $basearch " = aarch64 ] ; then
dir = ports/aarch64/tumbleweed/appliances
else
dir = tumbleweed/appliances
fi
file = openSUSE-Tumbleweed-Minimal-VM.$basearch -Cloud.qcow2
else
# leap
dir = distribution/leap/$releasever /appliances
case " $releasever " in
16.0) file = Leap-$releasever -Minimal-VM.$basearch -Cloud.qcow2 ;;
# 16.0) file=Leap-$releasever-Minimal-VM.$basearch-kvm$(if [ "$basearch" = x86_64 ]; then echo '-and-xen'; fi).qcow2 ;;
esac
2026-09-06 11:53:44 +08:00
# kvm 镜像里面没有 cloud-init
# 但不够 Cloud 镜像通用?
# https://src.opensuse.org/pool/kiwi-templates-Minimal/src/branch/factory/Minimal.kiwi
2026-09-06 11:31:35 +08:00
# https://build.opensuse.org/projects/Virtualization:Appliances:Images:openSUSE-Tumbleweed/packages/kiwi-templates-Minimal/files/Minimal.kiwi
fi
img = $mirror /$dir /$file
2025-04-10 23:38:36 +08:00
fi
2026-09-06 11:31:35 +08:00
set_osvar img " $img "
2023-08-01 22:23:21 +08:00
}
2023-05-25 20:15:12 +08:00
setos_windows() {
2026-01-07 23:10:09 +08:00
auto_find_iso = false
2024-04-05 22:45:59 +08:00
if [ -z " $iso " ] ; then
2026-01-07 23:10:09 +08:00
auto_find_iso = true
2024-09-06 23:00:35 +08:00
echo "iso url is not set. Attempting to find it automatically."
2024-04-05 22:45:59 +08:00
find_windows_iso
fi
2025-02-21 00:30:33 +08:00
if [[ " $iso " = magnet:* ]] ; then
: # 不测试磁力链接
else
2026-08-15 22:40:23 +08:00
local iso_is_tested = false
local iso_is_direct_link = false
2026-01-07 23:10:09 +08:00
if $auto_find_iso ; then
2026-08-04 21:10:05 +08:00
# 目前自动获取 iso 肯定不是直连,因此先关闭直连测试
if false && test_url_grace " $iso " iso 2>/dev/null; then
2026-01-07 23:10:09 +08:00
iso_is_tested = true
2026-08-15 22:40:23 +08:00
iso_is_direct_link = true
2026-08-24 23:01:43 +08:00
elif [[ $( echo " $iso " | to_lower) = ~ ^https://ntriver.org/drive\? .*\. ( iso| img) $ ]] ; then
2026-08-15 22:40:23 +08:00
info "get direct link"
2026-08-24 23:01:43 +08:00
local iso_name = ${ iso ##* \? }
2026-08-15 22:40:23 +08:00
local direct_link
if direct_link = $( curl -L "https://delivery-api.ntriver.org/generate-link?filename= $iso_name " |
grep -oE '"url":"[^"]+"' | cut -d: -f2- | tr -d '"' | grep .) ; then
echo "Direct link: $direct_link " >& 2
iso = " $direct_link "
iso_is_direct_link = true
else
warn false "Failed to get direct link for $iso "
fi
fi
# 需要用户输入直链的情况
if ! $iso_is_direct_link ; then
2026-01-07 23:10:09 +08:00
info "Set Direct link"
# MobaXterm 不支持
# printf '\e]8;;http://example.com\e\\This is a link\e]8;;\e\\\n'
2025-01-01 17:38:31 +08:00
2026-01-07 23:10:09 +08:00
# MobaXterm 不显示为超链接
# info false "请在浏览器中打开 $iso 获取直链并粘贴到这里。"
# info false "Please open $iso in browser to get the direct link and paste it here."
2025-05-16 19:06:07 +08:00
2026-01-07 23:10:09 +08:00
echo "请在浏览器中打开 $iso 获取直链并粘贴到这里。"
echo "Please open $iso in browser to get the direct link and paste it here."
IFS = read -r -p "Direct Link: " iso
if [ -z " $iso " ] ; then
error_and_exit "ISO Link is empty."
2026-08-04 21:10:05 +08:00
elif ! grep -Eiq '^https?://' <<< " $iso " ; then
error_and_exit "ISO Link is invalid."
2026-01-07 23:10:09 +08:00
fi
2025-02-21 00:30:33 +08:00
fi
fi
2026-01-07 23:10:09 +08:00
if ! $iso_is_tested ; then
test_url " $iso " iso
fi
2025-02-21 00:30:33 +08:00
# 判断 iso 架构是否兼容
# https://gitlab.com/libosinfo/osinfo-db/-/tree/main/data/os/microsoft.com?ref_type=heads
# uupdump linux 下合成的标签是 ARM64, windows下合成的标签是 A64
if file -b " $tmp /img-test" | grep -Eq '_(A64|ARM64)' ; then
iso_arch = arm64
2025-01-01 17:38:31 +08:00
else
2025-02-21 00:30:33 +08:00
iso_arch = x86_or_x64
fi
if ! {
{ [ " $basearch " = x86_64 ] && [ " $iso_arch " = x86_or_x64 ] ; } ||
{ [ " $basearch " = aarch64 ] && [ " $iso_arch " = arm64 ] ; }
} ; then
warn "
The current machine is $basearch , but it seems the ISO is for $iso_arch . Continue?
当前机器是 $basearch ,但 ISO 似乎是 $iso_arch 。继续安装?"
read -r -p '[y/N]: '
if ! [[ " $REPLY " = [ Yy] ]] ; then
exit
2025-01-01 17:38:31 +08:00
fi
fi
fi
2024-10-13 22:47:52 +08:00
[ -n " $boot_wim " ] && test_url " $boot_wim " 'wim'
2024-11-26 21:20:10 +08:00
2026-06-19 10:53:30 +08:00
set_osvar iso " $iso "
set_osvar boot_wim " $boot_wim "
set_osvar image_name " $image_name "
2023-05-25 20:15:12 +08:00
}
2023-06-05 19:50:21 +08:00
# shellcheck disable=SC2154
2023-06-04 19:12:57 +08:00
setos_dd() {
2024-09-06 23:00:32 +08:00
# raw 包含 vhd
2024-10-24 21:28:50 +08:00
test_url $img 'raw raw.gzip raw.xz raw.zstd raw.tar.gzip raw.tar.xz raw.tar.zstd' img_type
2024-07-14 23:00:52 +08:00
if is_efi; then
2024-08-23 00:33:59 +08:00
install_pkg hexdump
2024-08-20 00:27:51 +08:00
# openwrt 镜像 efi part type 不是 esp
# 因此改成检测 fat?
# https://downloads.openwrt.org/releases/23.05.3/targets/x86/64/openwrt-23.05.3-x86-64-generic-ext4-combined-efi.img.gz
2024-07-14 23:00:52 +08:00
# od 在 coreutils 里面,好像要配合 tr 才能删除空格
# hexdump 在 util-linux / bsdmainutils 里面
# xxd 要单独安装,el 在 vim-common 里面
2024-08-13 23:20:06 +08:00
# xxd -l $((34 * 4096)) -ps -c 128
2024-07-14 23:00:52 +08:00
# 仅打印前34个扇区 * 4096字节(按最大的算)
2024-08-13 23:20:06 +08:00
# 每行128字节
2024-10-24 21:28:50 +08:00
hexdump -n $(( 34 * 4096 )) -e '128/1 "%02x" "\n"' -v " $tmp /img-test" >$tmp /img-test-hex
2024-07-17 23:53:47 +08:00
if grep -q '^28732ac11ff8d211ba4b00a0c93ec93b' $tmp /img-test-hex; then
2024-07-14 23:00:52 +08:00
echo 'DD: Image is EFI.'
else
echo 'DD: Image is not EFI.'
warn '
2025-02-10 21:30:28 +08:00
The current machine uses EFI boot, but the DD image seems not an EFI image.
2024-07-17 23:53:47 +08:00
Continue with DD?
2024-08-23 00:33:59 +08:00
当前机器使用 EFI 引导,但 DD 镜像可能不是 EFI 镜像。
2024-07-17 23:53:47 +08:00
继续 DD?'
read -r -p '[y/N]: '
2024-07-14 23:00:52 +08:00
if [[ " $REPLY " = [ Yy] ]] ; then
2026-06-19 10:53:30 +08:00
set_osvar confirmed_no_efi 1
2024-07-14 23:00:52 +08:00
else
exit
fi
fi
fi
2026-06-19 10:53:30 +08:00
set_osvar img " $img "
set_osvar img_type " $img_type "
set_osvar img_type_warp " $img_type_warp "
2023-06-04 19:12:57 +08:00
}
2025-02-21 00:30:34 +08:00
setos_fnos() {
# 系统盘大小
2026-09-06 11:53:44 +08:00
min = 10
default = 64
echo "请输入系统分区大小,最小 $min GB,但可能无法更新系统。建议 $default GB。"
echo "Please input System Partition Size. Minimal is $min GB but may not be able to do system updates. Recommended is $default GB."
2025-02-21 00:30:34 +08:00
while true; do
2026-01-07 21:34:17 +08:00
IFS = read -r -p "Size in GB [ $default ]: " input
2025-02-21 00:30:34 +08:00
input = ${ input :- $default }
if ! { is_digit " $input " && [ " $input " -ge " $min " ] ; } ; then
error "Invalid Size. Please Try again."
else
2026-06-19 10:53:30 +08:00
set_osvar fnos_part_size " ${ input } G"
2025-02-21 00:30:34 +08:00
break
fi
done
2026-03-10 01:06:10 +08:00
if [ -z " $iso " ] ; then
2026-07-22 19:26:35 +08:00
local download_page sign_page
2026-06-09 23:08:15 +08:00
if [ " $FLYGOOS " = 1 ] ; then
2026-07-22 19:26:35 +08:00
download_page = https://fygonas.com/download
# sign_page=https://fygonas.com/asset/download-sign
2026-06-09 23:08:15 +08:00
else
2026-07-22 19:26:35 +08:00
download_page = https://fnnas.com/download$( [ " $basearch " = aarch64 ] && echo -arm || true )
sign_page = https://fnnas.com/asset/download-sign
fi
2025-08-06 20:58:14 +08:00
2026-07-22 19:26:35 +08:00
# fnos_Mainland-PE_x86_1.2.0203_2149.iso
# fnos_Mainland-PE_arm_1.1.31_armsr_1366.iso
# fygoos_PE_x86_1.2.0203_2150.iso
# fygoos_PE_arm_1.2.0007_armsr_1837.iso
# 对于同一行有多个成功匹配,grep -m1 无效
iso = $( curl -L " $download_page " | grep -o 'https://[^"]*\.iso' |
grep " $( [ " $basearch " = aarch64 ] && echo _armsr_ || echo _x86_) " |
head -1 | grep .)
if [ -n " $sign_page " ] ; then
2026-06-09 23:08:15 +08:00
# curl 7.82.0+
2026-07-22 19:26:35 +08:00
# curl -L --json '{"url":"'$iso'"}' https://fnnas.com/asset/download-sign
2025-08-06 20:58:14 +08:00
2026-06-09 23:08:15 +08:00
iso = $( curl -L \
-d '{"url":"' $iso '"}' \
-H 'Content-Type: application/json' \
2026-07-22 19:26:35 +08:00
" $sign_page " |
2026-06-09 23:08:15 +08:00
grep -o 'https://[^"]*' )
fi
2026-03-10 01:06:10 +08:00
fi
2025-08-06 20:58:14 +08:00
2025-03-04 00:34:49 +08:00
test_url " $iso " iso
2026-06-19 10:53:30 +08:00
set_osvar iso " $iso "
2025-02-21 00:30:34 +08:00
}
2025-02-24 22:05:26 +08:00
setos_aosc() {
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
if is_in_china; then
mirror = https://mirror.nju.edu.cn/anthon/aosc-os
else
# 服务器在香港
mirror = https://releases.aosc.io
fi
2025-02-24 22:05:26 +08:00
2026-09-06 11:31:35 +08:00
dir = os-$basearch_alt /base
file = $( curl -L $mirror /$dir / | grep -oP 'aosc-os_base_.*?\.tar.xz' |
sort -uV | tail -1 | grep .)
img = $mirror /$dir /$file
fi
test_url " $img " 'tar.xz'
2026-06-19 10:53:30 +08:00
set_osvar img " $img "
2025-02-24 22:05:26 +08:00
}
2024-11-26 22:09:05 +08:00
setos_centos_almalinux_rocky_fedora() {
2025-05-29 20:48:30 +08:00
# el 10 需要 x86-64-v3,除了 almalinux
2025-01-01 17:38:33 +08:00
if [ " $basearch " = x86_64 ] &&
2025-05-29 20:48:30 +08:00
{ [ " $distro " = centos ] || [ " $distro " = rocky ] ; } &&
2025-01-01 17:38:33 +08:00
[ " $releasever " -ge 10 ] ; then
assert_cpu_supports_x86_64_v3
fi
2025-05-29 20:48:30 +08:00
elarch = $basearch
2025-09-21 15:27:17 +08:00
if [ " $basearch " = x86_64 ] &&
[ " $distro " = almalinux ] && [ " $releasever " -ge 10 ] &&
! is_cpu_supports_x86_64_v3; then
2025-05-29 20:48:30 +08:00
elarch = x86_64_v2
fi
2023-07-18 00:22:51 +08:00
if is_use_cloud_image; then
# ci
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
if is_in_china; then
case $distro in
centos) ci_mirror = "https://mirror.nju.edu.cn/centos-cloud/centos" ;;
almalinux) ci_mirror = "https://mirror.nju.edu.cn/almalinux/ $releasever /cloud/ $elarch /images" ;;
rocky) ci_mirror = "https://mirror.nju.edu.cn/rocky/ $releasever /images/ $elarch " ;;
fedora) ci_mirror = "https://mirror.nju.edu.cn/fedora/releases/ $releasever /Cloud/ $elarch /images" ;;
esac
else
case $distro in
centos) ci_mirror = "https://cloud.centos.org/centos" ;;
almalinux) ci_mirror = "https://repo.almalinux.org/almalinux/ $releasever /cloud/ $elarch /images" ;;
rocky) ci_mirror = "https://download.rockylinux.org/pub/rocky/ $releasever /images/ $elarch " ;;
fedora) ci_mirror = "https://d2lzkl7pfhq30w.cloudfront.net/pub/fedora/linux/releases/ $releasever /Cloud/ $elarch /images" ;;
esac
fi
2023-07-18 00:22:51 +08:00
case $distro in
2026-09-06 11:31:35 +08:00
centos)
case $releasever in
7)
# CentOS-7-aarch64-GenericCloud.qcow2c 是旧版本
ver = -2211
img = $ci_mirror /$releasever /images/CentOS-$releasever -$elarch -GenericCloud$ver .qcow2c
;;
*)
# 有 bios 和 efi 镜像
# https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-10-latest.x86_64.qcow2
# https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-x86_64-10-latest.x86_64.qcow2
[ " $elarch " = x86_64 ] &&
img = $ci_mirror /$releasever -stream/$elarch /images/CentOS-Stream-GenericCloud-x86_64-$releasever -latest.$elarch .qcow2 ||
img = $ci_mirror /$releasever -stream/$elarch /images/CentOS-Stream-GenericCloud-$releasever -latest.$elarch .qcow2
;;
esac
;;
almalinux) img = $ci_mirror /AlmaLinux-$releasever -GenericCloud-latest.$elarch .qcow2 ;;
rocky) img = $ci_mirror /Rocky-$releasever -GenericCloud-Base.latest.$elarch .qcow2 ;;
fedora)
# 不加 / 会跳转到 https://dl.fedoraproject.org,纯 ipv6 无法访问
# curl -L -6 https://d2lzkl7pfhq30w.cloudfront.net/pub/fedora/linux/releases/42/Cloud/x86_64/images
# curl -L -6 https://d2lzkl7pfhq30w.cloudfront.net/pub/fedora/linux/releases/42/Cloud/x86_64/images/
filename = $( curl -L $ci_mirror / | grep -oP "Fedora-Cloud-Base-Generic.*?.qcow2" |
sort -uV | tail -1 | grep .)
img = $ci_mirror /$filename
;;
2023-07-18 00:22:51 +08:00
esac
fi
2026-09-06 11:31:35 +08:00
set_osvar img " $img "
2023-07-18 00:22:51 +08:00
else
# 传统安装
2023-10-08 22:10:05 +08:00
case $distro in
2025-05-29 20:48:30 +08:00
centos) mirrorlist = "https://mirrors.centos.org/mirrorlist?repo=centos-baseos- $releasever -stream&arch= $elarch " ;;
2024-11-29 22:13:53 +08:00
almalinux) mirrorlist = "https://mirrors.almalinux.org/mirrorlist/ $releasever /baseos" ;;
2025-05-29 20:48:30 +08:00
rocky) mirrorlist = "https://mirrors.rockylinux.org/mirrorlist?arch= $elarch &repo=BaseOS- $releasever " ;;
fedora) mirrorlist = "https://mirrors.fedoraproject.org/mirrorlist?arch= $elarch &repo=fedora- $releasever " ;;
2023-10-08 22:10:05 +08:00
esac
2023-07-18 00:22:51 +08:00
2025-05-29 20:48:30 +08:00
# rocky/centos9 需要删除第一行注释, almalinux 需要替换链接里面的 $basearch
for cur_mirror in $( curl -L $mirrorlist | sed "/^#/d" | sed "s,\$basearch, $elarch ," ) ; do
2023-10-08 22:10:05 +08:00
host = $( get_host_by_url $cur_mirror )
if is_host_has_ipv4_and_ipv6 $host &&
test_url_grace ${ cur_mirror } images/pxeboot/vmlinuz; then
mirror = $cur_mirror
break
2023-07-18 00:22:51 +08:00
fi
2023-10-08 22:10:05 +08:00
done
2023-07-18 00:22:51 +08:00
2023-10-08 22:10:05 +08:00
if [ -z " $mirror " ] ; then
error_and_exit "All mirror failed."
2023-07-18 00:22:51 +08:00
fi
2026-06-19 10:53:30 +08:00
set_osvar mirrorlist " $mirrorlist "
2023-10-08 22:10:05 +08:00
2026-06-19 10:53:30 +08:00
set_osvar ks " $confhome /deprecated/redhat.cfg"
2026-06-19 10:53:30 +08:00
set_osvar vmlinuz " ${ mirror } images/pxeboot/vmlinuz"
set_osvar initrd " ${ mirror } images/pxeboot/initrd.img"
set_osvar squashfs " ${ mirror } images/install.img"
2024-07-07 17:07:22 +08:00
test_url ${ mirror } images/install.img 'squashfs'
2023-05-03 22:22:21 +08:00
fi
}
2024-05-03 23:34:42 +08:00
setos_oracle() {
2025-01-01 17:38:33 +08:00
# el 10 需要 x86-64-v3
if [ " $basearch " = x86_64 ] && [ " $releasever " -ge 10 ] ; then
assert_cpu_supports_x86_64_v3
fi
2024-05-03 23:34:42 +08:00
if is_use_cloud_image; then
# ci
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
install_pkg jq
mirror = https://yum.oracle.com
2024-05-03 23:34:42 +08:00
2026-09-06 11:31:35 +08:00
[ " $basearch " = aarch64 ] &&
template_prefix = ol${ releasever } _${ basearch } -cloud ||
template_prefix = ol${ releasever }
curl -Lo $tmp /oracle.json $mirror /templates/OracleLinux/$template_prefix -template.json
dir = $( jq -r .base_url $tmp /oracle.json)
file = $( jq -r .kvm.image $tmp /oracle.json)
img = $mirror$dir /$file
fi
set_osvar img " $img "
2024-05-03 23:34:42 +08:00
else
:
fi
}
2024-07-07 17:07:21 +08:00
setos_redhat() {
if is_use_cloud_image; then
2025-05-16 19:06:09 +08:00
# el 10 需要 x86-64-v3
if [ " $basearch " = x86_64 ] && [[ " $img " = *rhel-10* ]] ; then
assert_cpu_supports_x86_64_v3
fi
2026-06-19 10:53:30 +08:00
set_osvar img " $img "
2024-07-07 17:07:21 +08:00
else
:
fi
}
2024-07-09 23:33:22 +08:00
setos_opencloudos() {
# https://mirrors.opencloudos.tech 不支持 ipv6
2026-09-06 11:53:44 +08:00
# mirrors.cloud.tencent.com 为公网与内网统一域名
# https://cloud.tencent.com/document/product/213/8623
2025-01-13 23:19:38 +08:00
if [ " $releasever " -ge 23 ] ; then
2026-09-06 11:53:44 +08:00
mirror = https://mirrors.cloud.tencent.com/opencloudos-stream/releases
2025-01-13 23:19:38 +08:00
else
mirror = https://mirrors.cloud.tencent.com/opencloudos
fi
2024-07-09 23:33:22 +08:00
if is_use_cloud_image; then
# ci
2026-09-06 11:53:44 +08:00
# https://opencloudos.org/api/v1/iso-releases/published
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
if [ " $releasever " -eq 9 ] ; then
dir = $releasever /images/qcow2/$basearch
else
dir = $releasever /images/$basearch
fi
2025-09-03 19:36:08 +08:00
2026-09-06 11:31:35 +08:00
file = $( curl -L $mirror /$dir / | grep -oP 'OpenCloudOS.*?\.qcow2' |
sort -uV | tail -1 | grep .)
img = $mirror /$dir /$file
fi
set_osvar img " $img "
2024-07-09 23:33:22 +08:00
else
:
fi
}
setos_anolis() {
mirror = https://mirrors.openanolis.cn/anolis
if is_use_cloud_image; then
# ci
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
dir = $releasever /isos/GA/$basearch
[ " $releasever " -ge 23 ] &&
filename = 'AnolisOS.*?\.qcow2' ||
filename = 'AnolisOS.*?-ANCK\.qcow2'
file = $( curl -L $mirror /$dir / | grep -oP " $filename " |
sort -uV | tail -1 | grep .)
img = $mirror /$dir /$file
fi
set_osvar img " $img "
2024-07-09 23:33:22 +08:00
else
:
fi
}
setos_openeuler() {
if is_in_china; then
mirror = https://repo.openeuler.openatom.cn
else
mirror = https://repo.openeuler.org
fi
if is_use_cloud_image; then
# ci
2026-09-06 11:31:35 +08:00
if [ -z " $img " ] ; then
name = $( curl -L " $mirror /" | grep -oE "openEuler- $releasever (-LTS)?(-SP[0-9])?" |
sort -uV | tail -1 | grep .)
img = $mirror /$name /virtual_machine_img/$basearch /$name -$basearch .qcow2.xz
fi
set_osvar img " $img "
2024-07-09 23:33:22 +08:00
else
:
fi
}
2026-06-19 10:53:30 +08:00
set_osvar distro " $distro "
set_osvar releasever " $releasever "
2024-04-27 09:45:10 +08:00
2024-07-07 17:07:21 +08:00
case " $distro " in
2024-11-26 22:09:05 +08:00
centos | almalinux | rocky | fedora) setos_centos_almalinux_rocky_fedora ;;
2024-07-07 17:07:21 +08:00
*) setos_$distro ;;
esac
2023-12-02 22:46:16 +08:00
2024-06-10 22:17:49 +08:00
# debian/kali <=256M 必须使用云内核,否则不够内存
2024-06-12 22:40:22 +08:00
if is_distro_like_debian && ! is_in_windows && [ " $ram_size " -le 256 ] ; then
2024-06-10 22:17:49 +08:00
exit_if_cant_use_cloud_kernel
fi
2023-12-02 22:46:16 +08:00
# 集中测试云镜像格式
if is_use_cloud_image && [ " $step " = finalos ] ; then
# shellcheck disable=SC2154
2025-04-10 23:38:36 +08:00
test_url $finalos_img 'qemu qemu.gzip qemu.xz qemu.zstd raw.xz' finalos_img_type
2023-12-02 22:46:16 +08:00
fi
2023-05-03 22:22:21 +08:00
}
2023-07-22 22:17:05 +08:00
is_distro_like_redhat() {
2024-05-08 22:17:33 +08:00
if [ -n " $1 " ] ; then
_distro = $1
else
_distro = $distro
fi
2024-11-26 22:09:05 +08:00
[ " $_distro " = redhat ] || [ " $_distro " = centos ] || [ " $_distro " = almalinux ] || [ " $_distro " = rocky ] || [ " $_distro " = fedora ] || [ " $_distro " = oracle ]
2024-05-03 21:37:07 +08:00
}
is_distro_like_debian() {
2024-05-08 22:17:33 +08:00
if [ -n " $1 " ] ; then
_distro = $1
else
_distro = $distro
fi
[ " $_distro " = debian ] || [ " $_distro " = kali ]
2023-07-22 22:17:05 +08:00
}
2025-04-12 15:44:20 +08:00
get_latest_distro_releasever() {
get_function_content verify_os_name |
grep -wo " $1 [^'\"]*" | awk -F'|' '{print $NF}'
}
2023-05-13 00:14:46 +08:00
# 检查是否为正确的系统名
2023-10-22 16:43:34 +08:00
verify_os_name() {
if [ -z " $* " ] ; then
usage_and_exit
fi
2024-07-14 22:12:59 +08:00
# 不要删除 centos 7
2023-08-01 22:33:07 +08:00
for os in \
2024-12-06 22:50:07 +08:00
'centos 7|9|10' \
2025-01-13 23:19:38 +08:00
'anolis 7|8|23' \
'opencloudos 8|9|23' \
2025-05-29 20:48:30 +08:00
'almalinux 8|9|10' \
2025-06-13 21:41:24 +08:00
'rocky 8|9|10' \
2025-09-29 21:00:08 +08:00
'oracle 8|9|10' \
2025-12-04 19:43:54 +08:00
'fnos 1' \
2026-06-09 23:08:15 +08:00
'fygoos 1' \
2026-04-30 22:08:15 +08:00
'fedora 43|44' \
2026-06-12 22:15:57 +08:00
'nixos 26.05' \
2025-08-10 08:02:22 +08:00
'debian 9|10|11|12|13' \
2026-05-11 20:20:33 +08:00
'opensuse 16.0|tumbleweed' \
2026-06-11 00:12:39 +08:00
'alpine 3.21|3.22|3.23|3.24' \
2026-04-30 22:08:15 +08:00
'openeuler 20.03|22.03|24.03' \
2026-04-24 00:12:09 +08:00
'ubuntu 18.04|20.04|22.04|24.04|26.04' \
2026-09-06 11:31:38 +08:00
'kali last-snapshot|rolling' \
2025-05-16 19:06:09 +08:00
'redhat' \
2023-09-10 22:23:04 +08:00
'arch' \
2023-09-16 20:00:07 +08:00
'gentoo' \
2025-02-24 22:05:26 +08:00
'aosc' \
2023-09-10 22:23:04 +08:00
'windows' \
2023-11-09 22:27:07 +08:00
'dd' \
2026-03-10 00:09:05 +08:00
'netboot.xyz' \
'reset' ; do
2024-08-19 00:33:59 +08:00
read -r ds vers <<< " $os "
vers_ = ${ vers // \. / \\\. }
finalos = $( echo " $@ " | to_lower | sed -n -E "s,^( $ds )[ :-]?(| $vers_ ) $,\1 \2,p" )
2023-05-13 00:14:46 +08:00
if [ -n " $finalos " ] ; then
2024-08-19 00:33:59 +08:00
read -r distro releasever <<< " $finalos "
2026-06-09 23:08:15 +08:00
# fygoos to fnos
if [ " $distro " = fygoos ] ; then
distro = fnos
FLYGOOS = 1
fi
2023-10-22 16:46:51 +08:00
# 默认版本号
2024-08-19 00:33:59 +08:00
if [ -z " $releasever " ] && [ -n " $vers " ] ; then
releasever = $( awk -F '|' '{print $NF}' <<< "| $vers " )
2023-09-10 22:23:04 +08:00
fi
2023-05-13 00:14:46 +08:00
return
fi
done
2023-06-18 21:27:22 +08:00
error "Please specify a proper os"
2023-05-13 00:14:46 +08:00
usage_and_exit
}
2023-10-22 16:43:34 +08:00
verify_os_args() {
2026-05-29 07:25:16 +08:00
# 必备参数
2023-10-22 16:43:34 +08:00
case " $distro " in
2026-05-29 07:25:16 +08:00
dd) [ -n " $img " ] || error_and_exit "dd need --img." ;;
redhat) [ -n " $img " ] || error_and_exit "redhat need --img." ;;
2024-07-07 17:07:21 +08:00
windows) [ -n " $image_name " ] || error_and_exit "Install Windows need --image-name." ;;
2023-10-22 16:43:34 +08:00
esac
2025-03-06 20:45:30 +08:00
2026-05-29 07:25:16 +08:00
# 用户名/密码/证书相关
2025-03-06 20:45:30 +08:00
case " $distro " in
2026-05-29 07:25:16 +08:00
netboot.xyz)
[ -z " $username " ] || error_and_exit "not support set username for $distro ."
[ -z " $password " ] || error_and_exit "not support set password for $distro ."
[ -z " $ssh_keys " ] || error_and_exit "not support set ssh key for $distro ."
;;
windows)
[ -z " $ssh_keys " ] || error_and_exit "not support set ssh key for $distro ."
;;
2025-03-06 20:45:30 +08:00
esac
2026-05-29 07:25:16 +08:00
# 不能同时使用证书和密码
if [ -n " $password " ] && [ -n " $ssh_keys " ] ; then
error_and_exit "Cannot set both password and ssh key."
fi
2023-10-22 16:43:34 +08:00
}
2023-10-07 22:33:38 +08:00
get_cmd_path() {
# arch 云镜像不带 which
# command -v 包括脚本里面的方法
2023-10-22 19:07:12 +08:00
# ash 无效
2023-10-07 22:33:38 +08:00
type -f -p $1
}
is_have_cmd() {
get_cmd_path $1 >/dev/null 2>& 1
}
2023-05-13 00:14:46 +08:00
install_pkg() {
2023-07-22 22:25:54 +08:00
is_in_windows && return
2023-09-16 20:00:06 +08:00
find_pkg_mgr() {
2024-08-20 00:27:51 +08:00
[ -n " $pkg_mgr " ] && return
2025-01-01 17:38:29 +08:00
# 查找方法1: 通过 ID / ID_LIKE
2024-08-20 00:27:51 +08:00
# 因为可能装了多种包管理器
if [ -f /etc/os-release ] ; then
# shellcheck source=/dev/null
2025-10-31 07:50:45 +08:00
for id in $( { . /etc/os-release && echo $ID $ID_LIKE ; } ) ; do
2024-08-20 23:52:55 +08:00
# https://github.com/chef/os_release
case " $id " in
2024-08-20 00:27:51 +08:00
fedora | centos | rhel) is_have_cmd dnf && pkg_mgr = dnf || pkg_mgr = yum ;;
2024-09-06 23:00:35 +08:00
debian | ubuntu) pkg_mgr = apt-get ;;
2024-08-20 00:27:51 +08:00
opensuse | suse) pkg_mgr = zypper ;;
alpine) pkg_mgr = apk ;;
arch) pkg_mgr = pacman ;;
gentoo) pkg_mgr = emerge ;;
nixos) pkg_mgr = nix-env ;;
esac
[ -n " $pkg_mgr " ] && return
2023-10-07 22:33:38 +08:00
done
2023-07-15 23:25:42 +08:00
fi
2024-08-20 00:27:51 +08:00
# 查找方法 2
2024-11-29 22:13:53 +08:00
for mgr in dnf yum apt-get pacman zypper emerge apk nix-env; do
2024-08-20 00:27:51 +08:00
is_have_cmd $mgr && pkg_mgr = $mgr && return
done
return 1
2023-09-16 20:00:06 +08:00
}
2023-08-01 22:10:34 +08:00
2023-09-16 20:00:06 +08:00
cmd_to_pkg() {
unset USE
case $cmd in
2024-04-03 21:53:52 +08:00
ar)
case " $pkg_mgr " in
*) pkg = "binutils" ;;
esac
;;
xz)
case " $pkg_mgr " in
2024-09-06 23:00:35 +08:00
apt-get) pkg = "xz-utils" ;;
2024-04-03 21:53:52 +08:00
*) pkg = "xz" ;;
esac
;;
2024-02-01 00:59:34 +08:00
lsblk | findmnt)
case " $pkg_mgr " in
apk) pkg = " $cmd " ;;
*) pkg = "util-linux" ;;
esac
;;
lsmem)
case " $pkg_mgr " in
apk) pkg = "util-linux-misc" ;;
*) pkg = "util-linux" ;;
esac
;;
fdisk)
case " $pkg_mgr " in
2024-09-06 23:00:35 +08:00
apt-get) pkg = "fdisk" ;;
2024-02-01 00:59:34 +08:00
apk) pkg = "util-linux-misc" ;;
*) pkg = "util-linux" ;;
esac
;;
2024-07-14 23:00:52 +08:00
hexdump)
case " $pkg_mgr " in
2024-09-06 23:00:35 +08:00
apt-get) pkg = "bsdmainutils" ;;
2024-07-14 23:00:52 +08:00
*) pkg = "util-linux" ;;
esac
;;
2023-09-16 20:00:06 +08:00
unsquashfs)
case " $pkg_mgr " in
zypper) pkg = "squashfs" ;;
emerge) pkg = "squashfs-tools" && export USE = "lzma" ;;
*) pkg = "squashfs-tools" ;;
esac
;;
nslookup | dig)
case " $pkg_mgr " in
2024-09-06 23:00:35 +08:00
apt-get) pkg = "dnsutils" ;;
2023-09-16 20:00:06 +08:00
pacman) pkg = "bind" ;;
apk | emerge) pkg = "bind-tools" ;;
yum | dnf | zypper) pkg = "bind-utils" ;;
esac
;;
2024-10-13 22:58:12 +08:00
iconv)
case " $pkg_mgr " in
apk) pkg = "musl-utils" ;;
*) error_and_exit "Which GNU/Linux do not have iconv built-in?" ;;
esac
;;
2023-09-16 20:00:06 +08:00
*) pkg = $cmd ;;
2023-08-01 22:10:34 +08:00
esac
2023-09-16 20:00:06 +08:00
}
2025-01-01 17:38:29 +08:00
# 系统 package名称 repo名称
# centos/alma/rocky/fedora epel-release epel
# oracle linux oracle-epel-release ol9_developer_EPEL
# opencloudos epol-release EPOL
# alibaba cloud linux 3 epel-release/epel-aliyuncs-release(qcow2自带) epel
# anolis 23 anolis-epao-release EPAO
# anolis 8
# [root@localhost ~]# yum search *ep*-release | grep -v next
# ========================== Name Matched: *ep*-release ==========================
# anolis-epao-release.noarch : EPAO Packages for Anolis OS 8 repository configuration
# epel-aliyuncs-release.noarch : Extra Packages for Enterprise Linux repository configuration
# epel-release.noarch : Extra Packages for Enterprise Linux repository configuration (qcow2自带)
2024-07-09 23:37:51 +08:00
check_is_need_epel() {
is_need_epel() {
case " $pkg " in
dpkg) true ;;
jq) is_have_cmd yum && ! is_have_cmd dnf ;; # el7/ol7 的 jq 在 epel 仓库
*) false ;;
esac
}
2024-04-03 21:53:52 +08:00
2024-07-21 23:57:59 +08:00
get_epel_repo_name() {
# el7 不支持 yum repolist --all,要使用 yum repolist all
# el7 yum repolist 第一栏有 /x86_64 后缀,因此要去掉。而 el9 没有
2025-01-01 17:38:29 +08:00
$pkg_mgr repolist all | awk '{print $1}' | awk -F/ '{print $1}' | grep -Ei 'ep(el|ol|ao)$'
2024-07-21 23:57:59 +08:00
}
get_epel_pkg_name() {
2025-01-01 17:38:29 +08:00
# el7 不支持 yum list --available,要使用 yum list available
$pkg_mgr list available | grep -E '(.*-)?ep(el|ol|ao)-(.*-)?release' |
awk '{print $1}' | cut -d. -f1 | grep -v next | head -1
2024-07-09 23:37:51 +08:00
}
2024-04-03 21:53:52 +08:00
2024-07-09 23:37:51 +08:00
if is_need_epel; then
2024-07-21 23:57:59 +08:00
if ! epel = $( get_epel_repo_name) ; then
$pkg_mgr install -y " $( get_epel_pkg_name) "
epel = $( get_epel_repo_name)
2024-04-03 21:53:52 +08:00
fi
2024-07-09 23:37:51 +08:00
enable_epel = "--enablerepo= $epel "
2024-07-21 23:57:59 +08:00
else
enable_epel =
2024-04-03 21:53:52 +08:00
fi
}
2023-09-16 20:00:06 +08:00
install_pkg_real() {
2024-03-05 23:01:24 +08:00
text = " $pkg "
if [ " $pkg " != " $cmd " ] ; then
text += " ( $cmd )"
fi
echo "Installing package ' $text '..."
2024-04-03 21:53:52 +08:00
2023-09-16 20:00:06 +08:00
case $pkg_mgr in
2024-07-09 23:37:51 +08:00
dnf)
check_is_need_epel
dnf install $enable_epel -y --setopt= install_weak_deps = False $pkg
;;
yum)
check_is_need_epel
yum install $enable_epel -y $pkg
;;
2023-09-16 20:00:06 +08:00
emerge) emerge --oneshot $pkg ;;
pacman) pacman -Syu --noconfirm --needed $pkg ;;
zypper) zypper install -y $pkg ;;
apk)
add_community_repo_for_alpine
apk add $pkg
;;
2024-09-06 23:00:35 +08:00
apt-get)
[ -z " $apt_updated " ] && apt-get update && apt_updated = 1
DEBIAN_FRONTEND = noninteractive apt-get install -y $pkg
2023-09-16 20:00:06 +08:00
;;
2024-08-19 00:42:12 +08:00
nix-env)
# 不指定 channel 会很慢,而且很占内存
[ -z " $nix_updated " ] && nix-channel --update && nix_updated = 1
nix-env -iA nixos.$pkg
;;
2023-09-16 20:00:06 +08:00
esac
}
2023-10-07 22:33:38 +08:00
2024-02-01 00:59:34 +08:00
is_need_reinstall() {
2026-07-22 19:20:25 +08:00
local cmd = $1
2024-02-01 00:59:34 +08:00
2024-01-27 23:17:50 +08:00
# gentoo 默认编译的 unsquashfs 不支持 xz
2026-07-22 19:20:25 +08:00
if [ " $cmd " = unsquashfs ] && is_have_cmd emerge && ! " $cmd " |& grep -wq xz; then
2024-01-27 23:17:50 +08:00
echo "unsquashfs not supported xz. rebuilding."
return 0
fi
2024-02-01 00:59:34 +08:00
2026-07-22 19:20:25 +08:00
# busybox grep 不支持 -oP
# busybox lsblk 不支持 -r -n --inverse
2024-02-01 00:59:34 +08:00
# busybox fdisk 无法显示 mbr 分区表的 id
2026-07-22 19:20:25 +08:00
if { [ " $cmd " = grep ] || [ " $cmd " = lsblk ] || [ " $cmd " = fdisk ] ; } &&
is_have_cmd apk && " $cmd " --help |& grep -wq BusyBox; then
2024-02-01 00:59:34 +08:00
return 0
fi
2024-01-27 23:17:50 +08:00
return 1
}
2023-10-07 22:33:38 +08:00
for cmd in " $@ " ; do
2024-02-01 00:59:34 +08:00
if ! is_have_cmd $cmd || is_need_reinstall $cmd ; then
2023-10-07 22:33:38 +08:00
if ! find_pkg_mgr; then
error_and_exit "Can't find compatible package manager. Please manually install $cmd ."
fi
cmd_to_pkg
install_pkg_real
fi
2024-09-06 23:00:35 +08:00
done >& 2
2023-05-13 00:14:46 +08:00
}
2025-08-22 22:47:15 +08:00
is_valid_ram_size() {
is_digit " $1 " && [ " $1 " -gt 0 ]
}
2023-05-17 22:54:26 +08:00
check_ram() {
2024-03-28 00:16:05 +08:00
ram_standard = $(
case " $distro " in
netboot.xyz) echo 0 ;;
2024-05-03 21:37:07 +08:00
alpine | debian | kali | dd) echo 256 ;;
2025-02-24 22:05:26 +08:00
arch | gentoo | aosc | nixos | windows) echo 512 ;;
2024-11-26 22:09:05 +08:00
redhat | centos | almalinux | rocky | fedora | oracle | ubuntu | anolis | opencloudos | openeuler) echo 1024 ;;
2025-02-21 00:30:34 +08:00
opensuse | fnos) echo -1 ;; # 没有安装模式
2024-03-28 00:16:05 +08:00
esac
)
# 不用检查内存的情况
if [ " $ram_standard " -eq 0 ] ; then
return
fi
2024-04-25 22:43:11 +08:00
# 未测试
ram_cloud_image = 256
2024-03-28 00:16:05 +08:00
has_cloud_image = $(
case " $distro " in
2024-11-26 22:09:05 +08:00
redhat | centos | almalinux | rocky | oracle | fedora | debian | ubuntu | opensuse | anolis | openeuler) echo true ;;
2024-08-19 00:42:12 +08:00
netboot.xyz | alpine | dd | arch | gentoo | nixos | kali | windows) echo false ;;
2024-03-28 00:16:05 +08:00
esac
)
2023-06-18 21:27:22 +08:00
if is_in_windows; then
2025-08-22 22:47:15 +08:00
ram_size = $( wmic memorychip get capacity | awk -F= '{sum+=$2} END {if(sum>0) print sum/1024/1024}' )
2023-06-18 21:27:22 +08:00
else
2024-04-03 21:53:52 +08:00
# lsmem最准确但 centos7 arm 和 alpine 不能用,debian 9 util-linux 没有 lsmem
2023-06-18 21:27:22 +08:00
# arm 24g dmidecode 显示少了128m
2023-07-05 21:57:27 +08:00
# arm 24g lshw 显示23BiB
# ec2 t4g arm alpine 用 lsmem 和 dmidecode 都无效,要用 lshw,但结果和free -m一致,其他平台则没问题
2023-07-15 23:25:42 +08:00
install_pkg lsmem
2023-06-18 21:27:22 +08:00
ram_size = $( lsmem -b 2>/dev/null | grep 'Total online memory:' | awk '{ print $NF/1024/1024 }' )
2023-07-05 21:57:27 +08:00
2025-08-22 22:47:15 +08:00
if ! is_valid_ram_size " $ram_size " ; then
2023-06-18 21:27:22 +08:00
install_pkg dmidecode
2025-08-22 22:47:15 +08:00
ram_size = $( dmidecode -t 17 | grep "Size.*[GM]B" | awk '{if ($3=="GB") s+=$2*1024; else s+=$2} END {if(s>0) print s}' )
2023-06-18 21:27:22 +08:00
fi
2023-07-05 21:57:27 +08:00
2025-08-22 22:47:15 +08:00
if ! is_valid_ram_size " $ram_size " ; then
2023-07-05 21:57:27 +08:00
install_pkg lshw
# 不能忽略 -i, alpine 显示的是 System memory
ram_str = $( lshw -c memory -short | grep -i 'System Memory' | awk '{print $3}' )
ram_size = $( grep <<< $ram_str -o '[0-9]*' )
grep <<< $ram_str GiB && ram_size = $(( ram_size * 1024 ))
fi
2023-05-17 22:54:26 +08:00
fi
2024-10-19 00:42:30 +08:00
# 用于兜底,不太准确
2025-04-15 17:30:35 +08:00
# cygwin 要装 procps-ng 才有 free 命令
2025-08-22 22:47:15 +08:00
if ! is_valid_ram_size " $ram_size " ; then
2025-04-15 17:30:35 +08:00
ram_size_k = $( grep '^MemTotal:' /proc/meminfo | awk '{print $2}' )
ram_size = $(( ram_size_k / 1024 + 64 + 4 ))
2024-10-19 00:42:30 +08:00
fi
2025-08-22 22:47:15 +08:00
if ! is_valid_ram_size " $ram_size " ; then
2023-06-18 21:27:22 +08:00
error_and_exit "Could not detect RAM size."
fi
2023-07-18 00:22:51 +08:00
# ram 足够就用普通方法安装,否则如果内存大于512就用 cloud image
2023-08-04 00:07:45 +08:00
# TODO: 测试 256 384 内存
2024-03-28 00:16:05 +08:00
if ! is_use_cloud_image && [ $ram_size -lt $ram_standard ] ; then
if $has_cloud_image ; then
info "RAM < $ram_standard MB. Fallback to cloud image mode"
2023-07-18 00:22:51 +08:00
cloud_image = 1
else
2024-03-28 00:16:05 +08:00
error_and_exit "Could not install $distro : RAM < $ram_standard MB."
2023-07-18 00:22:51 +08:00
fi
2023-06-18 21:27:22 +08:00
fi
2024-03-28 00:16:05 +08:00
if is_use_cloud_image && [ $ram_size -lt $ram_cloud_image ] ; then
error_and_exit "Could not install $distro using cloud image: RAM < $ram_cloud_image MB."
fi
2023-06-18 21:27:22 +08:00
}
is_efi() {
if is_in_windows; then
2024-01-27 23:17:50 +08:00
# bcdedit | grep -qi '^path.*\.efi'
2026-03-15 23:32:51 +08:00
mountvol | grep -q -a 'EFI'
2023-06-18 21:27:22 +08:00
else
[ -d /sys/firmware/efi ]
fi
}
2024-06-05 22:23:18 +08:00
is_grub_dir_linked() {
# cloudcone 重装前/重装后(方法1)
[ " $( readlink -f /boot/grub/grub.cfg) " = /boot/grub2/grub.cfg ] ||
[ " $( readlink -f /boot/grub2/grub.cfg) " = /boot/grub/grub.cfg ] ||
# cloudcone 重装后(方法2)
{ [ -f /boot/grub2/grub.cfg ] && [ " $( cat /boot/grub2/grub.cfg) " = 'chainloader (hd0)+1' ] ; }
}
2023-12-22 23:45:06 +08:00
is_secure_boot_enabled() {
if is_efi; then
if is_in_windows; then
2024-03-15 00:01:34 +08:00
reg query 'HKLM\SYSTEM\CurrentControlSet\Control\SecureBoot\State' /v UEFISecureBootEnabled 2>/dev/null | grep 0x1
2023-12-22 23:45:06 +08:00
else
2024-07-17 23:53:47 +08:00
if dmesg | grep -i 'Secure boot enabled' ; then
return 0
fi
install_pkg mokutil
mokutil --sb-state 2>& 1 | grep -i 'SecureBoot enabled'
2023-12-22 23:45:06 +08:00
fi
2024-02-02 00:49:24 +08:00
else
return 1
2023-12-22 23:45:06 +08:00
fi
}
2026-03-10 00:09:05 +08:00
is_need_boot_vmlinuz() {
2023-11-09 22:27:07 +08:00
! { is_netboot_xyz && is_efi; }
}
2024-09-09 22:56:13 +08:00
# 只有 linux bios 是用本机的 grub/extlinux
is_use_local_grub_extlinux() {
2026-03-10 00:09:05 +08:00
is_need_boot_vmlinuz && ! is_in_windows && ! is_efi
2024-09-09 22:56:13 +08:00
}
2023-12-16 16:12:13 +08:00
is_use_local_grub() {
2024-09-09 22:56:13 +08:00
is_use_local_grub_extlinux && is_mbr_using_grub
}
is_use_local_extlinux() {
is_use_local_grub_extlinux && ! is_mbr_using_grub
}
2026-03-10 00:09:05 +08:00
# 软 raid 时 xda 可能不是引导盘,以后再修正
2024-09-09 22:56:13 +08:00
is_mbr_using_grub() {
find_main_disk
# 各发行版不一定自带 strings hexdump xxd od 命令
2026-03-15 23:32:51 +08:00
head -c 440 /dev/$xda | grep -a -iq 'GRUB'
2023-12-16 16:12:13 +08:00
}
2023-10-22 16:54:45 +08:00
to_upper() {
tr '[:lower:]' '[:upper:]'
}
to_lower() {
tr '[:upper:]' '[:lower:]'
}
del_cr() {
2025-03-26 23:25:07 +08:00
# wmic/reg 换行符是 \r\r\n
# wmic nicconfig where InterfaceIndex=$id get MACAddress,IPAddress,IPSubnet,DefaultIPGateway | hexdump -c
sed -E 's/\r+$//'
2023-10-22 16:54:45 +08:00
}
2024-03-05 23:01:24 +08:00
del_empty_lines() {
2024-03-01 22:06:33 +08:00
sed '/^[[:space:]]*$/d'
}
2025-10-31 07:50:40 +08:00
del_comment_lines() {
sed '/^[[:space:]]*#/d'
}
2025-03-02 21:57:06 +08:00
trim() {
# sed -E -e 's/^[[:space:]]+//' -e 's/[[:space:]]+$//'
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
2026-05-05 22:55:00 +08:00
assert_username_valid() {
# https://learn.microsoft.com/windows-hardware/customize/desktop/unattend/microsoft-windows-shell-setup-useraccounts-localaccounts-localaccount-name
# 不能为 none [ ] / \ : | < > + = ; , ? * % @
2026-05-29 07:25:16 +08:00
# 账号为空
2026-05-05 22:55:00 +08:00
if [ -z " $username " ] ; then
2026-05-29 07:25:16 +08:00
error_and_exit "Username: Can not be empty."
2026-05-05 22:55:00 +08:00
fi
2026-05-29 07:25:16 +08:00
# 账号为 none
2026-05-05 22:55:00 +08:00
if [ " $( to_lower <<< " $username " ) " = none ] ; then
2026-05-29 07:25:16 +08:00
error_and_exit "Username: Can not be 'none'."
2026-05-05 22:55:00 +08:00
fi
2026-05-29 07:25:16 +08:00
# 账号包含非法字符
2026-05-05 22:55:00 +08:00
if grep -q '[][/\:|<>+=;,?*%@]' <<< " $username " ; then
2026-05-29 07:25:16 +08:00
error_and_exit "Username: Do not use any of the following characters: / \ [ ] : | < > + = ; , ? * % @"
2026-05-05 22:55:00 +08:00
fi
2026-05-29 07:25:16 +08:00
}
# trans.sh 有同名方法
is_administrator_username() {
username_in_lower = $( to_lower <<< " $1 " )
2026-05-05 22:55:00 +08:00
# 如果输入以下用户名则忽略,并使用系统内置的 Administrator 账号
# 防止系统有两个不同语言的 Administrator 账号而造成困扰
for builtin_username in \
administrator \
administrador \
administrateur \
administratör \
администратор \
järjestelmänvalvoja \
rendszergazda; do
2026-05-29 07:25:16 +08:00
if [ " $username_in_lower " = " $builtin_username " ] ; then
2026-05-05 22:55:00 +08:00
return 0
fi
done
2026-05-29 07:25:16 +08:00
return 1
2026-05-05 22:55:00 +08:00
}
prompt_username() {
info "prompt username"
2026-05-29 07:25:16 +08:00
if [ " $distro " = windows ] ; then
default_username = administrator
else
default_username = root
fi
warn false "Set username, leave blank to use $default_username "
warn false "设置用户名,不填写则使用 $default_username "
2026-05-05 22:55:00 +08:00
IFS = read -r -p "Username: " username
username = " $( printf "%s" " $username " | trim) "
2026-05-29 07:25:16 +08:00
if [ -z " $username " ] ; then
username = $default_username
fi
2026-05-05 22:55:00 +08:00
assert_username_valid
}
2024-10-13 22:58:12 +08:00
prompt_password() {
2025-03-02 21:57:06 +08:00
info "prompt password"
2026-05-29 07:25:16 +08:00
warn false "Set password, leave blank to use a random password."
warn false "设置密码,不填写则使用随机密码"
2024-10-13 22:58:12 +08:00
while true; do
2025-10-31 07:50:41 +08:00
IFS = read -r -p "Password: " password
if [ -n " $password " ] ; then
IFS = read -r -p "Retype password: " password_confirm
if [ " $password " = " $password_confirm " ] ; then
break
else
error "Passwords don't match. Try again."
fi
2024-10-13 22:58:12 +08:00
else
2025-11-04 21:54:21 +08:00
# 特殊字符列表
# https://learn.microsoft.com/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/hh994562(v=ws.11)
# 有的机器运行 centos 7 ,用 /dev/random 产生 16 位密码,开启了 rngd 也要 5 秒,关闭了 rngd 则长期阻塞
chars = \' 'A-Za-z0-9~!@#$%^&*_=+`|(){}[]:;"<>,.?/-'
password = $( tr -dc " $chars " </dev/urandom | head -c16)
2024-10-13 22:58:12 +08:00
break
fi
done
}
save_password() {
dir = $1
# mkpasswd 有三个
# expect 里的 mkpasswd 是用来生成随机密码的
# whois 里的 mkpasswd 才是我们想要的,可能不支持 yescrypt, alpine 的 mkpasswd 是独立的包
# busybox 里的 mkpasswd 也是我们想要的,但多数不支持 yescrypt
# alpine 这两个包有冲突
# apk add expect mkpasswd
2024-10-18 23:58:04 +08:00
# 不要用 echo "$password" 保存密码,原因:
# password="-n"
# echo "$password" # 空白
2024-10-13 22:58:12 +08:00
# 明文密码
2024-10-18 23:58:04 +08:00
# 假如用户运行 alpine live 直接打包硬盘镜像,如果保存了明文密码,则会暴露明文密码,因为 netboot initrd 在里面
2024-10-13 22:58:12 +08:00
# 通过 --password 传入密码,history 有记录,也会暴露明文密码
2024-10-18 23:58:04 +08:00
# /reinstall.log 也会暴露明文密码(已处理)
2024-10-13 22:58:12 +08:00
if false; then
2024-10-18 23:58:04 +08:00
printf '%s' " $password " >>" $dir /password-plaintext"
2024-10-13 22:58:12 +08:00
fi
# sha512
# 以下系统均支持 sha512 密码,但是生成密码需要不同的工具
# 兼容性 openssl mkpasswd busybox python
# centos 7 × 只有expect的 需要编译 √
# centos 8 √ 只有expect的
# debian 9 × √
# ubuntu 16 × √
# alpine √ 可能系统装了expect √
# cygwin √
# others √
# alpine
if is_have_cmd busybox && busybox mkpasswd --help 2>& 1 | grep -wq sha512; then
crypted = $( printf '%s' " $password " | busybox mkpasswd -m sha512)
# others
elif install_pkg openssl && openssl passwd --help 2>& 1 | grep -wq '\-6' ; then
crypted = $( printf '%s' " $password " | openssl passwd -6 -stdin)
# debian 9 / ubuntu 16
elif is_have_cmd apt-get && install_pkg whois && mkpasswd -m help | grep -wq sha-512; then
crypted = $( printf '%s' " $password " | mkpasswd -m sha-512 --stdin)
2024-10-18 10:35:05 +08:00
# centos 7
# crypt.mksalt 是 python3 的
# 红帽把它 backport 到了 centos7 的 python2 上
# 在其它发行版的 python2 上运行会出错
elif is_have_cmd yum && is_have_cmd python2; then
crypted = $( python2 -c "import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA512)))" " $password " )
2024-10-13 22:58:12 +08:00
else
error_and_exit "Could not generate sha512 password."
fi
echo " $crypted " >" $dir /password-linux-sha512"
# yescrypt
# 旧系统不支持,先不管
if false; then
if mkpasswd -m help | grep -wq yescrypt; then
crypted = $( printf '%s' " $password " | mkpasswd -m yescrypt --stdin)
echo " $crypted " >" $dir /password-linux-yescrypt"
fi
fi
# windows
2026-09-06 11:53:44 +08:00
if [ " $distro " = windows ] ; then
2024-10-13 22:58:12 +08:00
install_pkg iconv
# 要分两行写,因为 echo "$(xxx)" 返回值始终为 0,出错也不会中断脚本
# grep . 为了保证脚本没有出错
base64 = $( printf '%s' " ${ password } Password" | iconv -f UTF-8 -t UTF-16LE | base64 -w 0 | grep .)
echo " $base64 " >" $dir /password-windows-user-base64"
base64 = $( printf '%s' " ${ password } AdministratorPassword" | iconv -f UTF-8 -t UTF-16LE | base64 -w 0 | grep .)
echo " $base64 " >" $dir /password-windows-administrator-base64"
fi
}
2024-02-01 00:59:34 +08:00
# 记录主硬盘
find_main_disk() {
2024-04-03 21:53:52 +08:00
if [ -n " $main_disk " ] ; then
return
fi
2024-02-01 00:59:34 +08:00
if is_in_windows; then
# TODO:
# 已测试 vista
# 测试 软raid
# 测试 动态磁盘
# diskpart 命令结果
# 磁盘 ID: E5FDE61C
# 磁盘 ID: {92CF6564-9B2E-4348-A3BD-D84E3507EBD7}
2024-06-06 20:50:02 +08:00
main_disk = $( printf "%s\n%s" "select volume $c " "uniqueid disk" | diskpart |
2024-11-23 23:50:11 +08:00
tail -1 | awk '{print $NF}' | sed 's,[{}],,g' )
2024-02-01 00:59:34 +08:00
else
2026-03-10 00:09:05 +08:00
if [ -z " $xda " ] ; then
# centos7下测试 lsblk --inverse $mapper | grep -w disk grub2-probe -t disk /
# 跨硬盘btrfs 只显示第一个硬盘 显示两个硬盘
# 跨硬盘lvm 显示两个硬盘 显示/dev/mapper/centos-root
# 跨硬盘软raid 显示两个硬盘 显示/dev/md127
2024-02-01 00:59:34 +08:00
2026-03-10 00:09:05 +08:00
# 还有 findmnt
2024-05-22 21:21:15 +08:00
2026-03-10 00:09:05 +08:00
# 理论上 /boot/efi /efi /boot 可以不在主硬盘上
# 因此只查找 / 分区
2024-02-01 00:59:34 +08:00
2026-03-10 00:09:05 +08:00
install_pkg lsblk
# lvm 显示的是 /dev/mapper/xxx-yyy,再用第二条命令得到sda
mapper = $( mount | awk '$3=="/" {print $1}' | grep .)
xdas = $( lsblk -rn --inverse $mapper | grep -w disk | awk '{print $1}' | sort -u | grep .)
2024-02-01 00:59:34 +08:00
2026-03-10 00:09:05 +08:00
# 注意 wc -l 的坑
# wc -l <<<"" 输出 1
# 检测主硬盘是否横跨多个磁盘
if [ " $( wc -l <<< " $xdas " ) " -eq 1 ] ; then
xda = $xdas
else
# vultr 官网新建数据盘时有写,部分地区的实例,不能从数据盘启动
# 实测 nvram 可写入数据盘的条目但不生效,手动安装 debian 到此硬盘,nvram 也不生效
# 从 bios 手动选择数据盘的 efi 文件才能启动
# 但是 nvram BootNext 数据盘的条目却可以生效
# vultr 从软 raid 0 debian 下用本脚本安装 debian ,进入 grub efi 时,无法识别系统分区 md1 的文件系统,也少了 hd1
# 但是从 bios 手动选择 reinstall 的 grub efi 时却可以识别
# 后期应该将 vmlinux/initrd 放到 efi/boot 分区
# Gemini 说:
# 使用 efibootmgr 的 BootNext 时,固件走的是“快速路径”:
# 它只初始化存放 EFI 引导文件(grubx64.efi)的那块硬盘(hd0),然后直接把控制权交给 GRUB。
# 因为 hd1 还没“醒”,GRUB 自然无法拼凑出完整的 md1 阵列,导致无法识别文件系统。
# 使用 nativedisk 可以强制 grub 识别所有硬盘?
info false "Multiple disks found for root partition:"
echo '-----'
printf "%s\n" " $xdas "
echo '-----'
read -r -p "Select a disk to install: " xda
if ! grep -Fqx " $xda " <<< " $xdas " ; then
error_and_exit "Invalid Input."
fi
fi
2024-02-01 00:59:34 +08:00
fi
2026-03-10 00:09:05 +08:00
info "Main disk: $xda "
2024-02-01 00:59:34 +08:00
# 可以用 dd 找出 guid?
2024-03-01 22:01:22 +08:00
# centos7 blkid lsblk 不显示 PTUUID
2024-02-01 00:59:34 +08:00
# centos7 sfdisk 不显示 Disk identifier
2024-03-01 22:01:22 +08:00
# alpine blkid 不显示 gpt 分区表的 PTUUID
2024-02-01 00:59:34 +08:00
# 因此用 fdisk
# Disk identifier: 0x36778223 # gnu fdisk + mbr
# Disk identifier: D6B17C1A-FA1E-40A1-BDCB-0278A3ED9CFC # gnu fdisk + gpt
# Disk identifier (GUID): d6b17c1a-fa1e-40a1-bdcb-0278a3ed9cfc # busybox fdisk + gpt
# 不显示 Disk identifier # busybox fdisk + mbr
# 获取 xda 的 id
install_pkg fdisk
main_disk = $( fdisk -l /dev/$xda | grep 'Disk identifier' | awk '{print $NF}' | sed 's/0x//' )
fi
# 检查 id 格式是否正确
if ! grep -Eix '[0-9a-f]{8}' <<< " $main_disk " &&
! grep -Eix '[0-9a-f-]{36}' <<< " $main_disk " ; then
error_and_exit "Disk ID is invalid: $main_disk "
fi
}
2024-06-23 01:25:23 +08:00
is_found_ipv4_netconf() {
[ -n " $ipv4_mac " ] && [ -n " $ipv4_addr " ] && [ -n " $ipv4_gateway " ]
}
is_found_ipv6_netconf() {
[ -n " $ipv6_mac " ] && [ -n " $ipv6_addr " ] && [ -n " $ipv6_gateway " ]
}
2024-06-12 22:40:22 +08:00
# TODO: 单网卡多IP
2023-08-21 23:50:20 +08:00
collect_netconf() {
if is_in_windows; then
2023-10-22 16:54:45 +08:00
convert_net_str_to_array() {
config = $1
key = $2
var = $3
2023-11-09 22:30:21 +08:00
IFS = ',' read -r -a " ${ var ? } " <<< " $( grep " $key =" <<< " $config " | cut -d= -f2 | sed 's/[{}\"]//g' ) "
2023-09-21 00:12:49 +08:00
}
2023-10-22 16:54:45 +08:00
# 部分机器精简了 powershell
# 所以不要用 powershell 获取网络信息
2024-11-23 23:50:11 +08:00
# ids=$(wmic nic where "PhysicalAdapter=true and MACAddress is not null and (PNPDeviceID like '%VEN_%&DEV_%' or PNPDeviceID like '%{F8615163-DF3E-46C5-913F-F2D2F965ED0E}%')" get InterfaceIndex | sed '1d')
2023-12-23 20:32:32 +08:00
# 否 手动 0 0.0.0.0/0 19 192.168.1.1
# 否 手动 0 0.0.0.0/0 59 nekoray-tun
2024-06-12 22:40:22 +08:00
2024-06-23 01:25:23 +08:00
# wmic nic:
# 真实网卡
# AdapterType=以太网 802.3
# AdapterTypeId=0
# MACAddress=68:EC:C5:11:11:11
# PhysicalAdapter=TRUE
# PNPDeviceID=PCI\VEN_8086&DEV_095A&SUBSYS_94108086&REV_61\4&295A4BD&1&00E0
# VPN tun 网卡,部分移动云电脑也有
# AdapterType=
# AdapterTypeId=
# MACAddress=
# PhysicalAdapter=TRUE
# PNPDeviceID=SWD\WINTUN\{6A460D48-FB76-6C3F-A47D-EF97D3DC6B0E}
# VMware 网卡
# AdapterType=以太网 802.3
# AdapterTypeId=0
# MACAddress=00:50:56:C0:00:08
# PhysicalAdapter=TRUE
# PNPDeviceID=ROOT\VMWARE\0001
2024-06-12 22:40:22 +08:00
for v in 4 6; do
if [ " $v " = 4 ] ; then
# 或者 route print
2024-11-23 23:50:11 +08:00
routes = $( netsh int ipv4 show route | awk '$4 == "0.0.0.0/0"' )
2024-06-12 22:40:22 +08:00
else
2024-11-23 23:50:11 +08:00
routes = $( netsh int ipv6 show route | awk '$4 == "::/0"' )
2024-06-12 22:40:22 +08:00
fi
2024-06-23 01:25:23 +08:00
if [ -z " $routes " ] ; then
2024-06-12 22:40:22 +08:00
continue
fi
2024-06-23 01:25:23 +08:00
while read -r route; do
if false; then
read -r _ _ _ _ id gateway <<< " $route "
else
id = $( awk '{print $5}' <<< " $route " )
gateway = $( awk '{print $6}' <<< " $route " )
fi
2024-06-12 22:40:22 +08:00
2024-11-23 23:50:11 +08:00
config = $( wmic nicconfig where InterfaceIndex = $id get MACAddress,IPAddress,IPSubnet,DefaultIPGateway)
2024-06-23 01:25:23 +08:00
# 排除 IP/子网/网关/MAC 为空的
if grep -q '=$' <<< " $config " ; then
continue
fi
2023-10-22 16:54:45 +08:00
2024-06-23 01:25:23 +08:00
mac_addr = $( grep "MACAddress=" <<< " $config " | cut -d= -f2 | to_lower)
convert_net_str_to_array " $config " IPAddress ips
convert_net_str_to_array " $config " IPSubnet subnets
convert_net_str_to_array " $config " DefaultIPGateway gateways
2023-10-22 16:54:45 +08:00
2024-06-23 01:25:23 +08:00
# IPv4
# shellcheck disable=SC2154
if [ " $v " = 4 ] ; then
for (( i = 0; i < ${# ips [@] } ; i++)) ; do
ip = ${ ips [i] }
subnet = ${ subnets [i] }
if [[ " $ip " = *.* ]] ; then
2025-02-24 22:05:27 +08:00
# ipcalc 依赖 perl,会使 cygwin 增加 ~50M
# cidr=$(ipcalc -b "$ip/$subnet" | grep Netmask: | awk '{print $NF}')
cidr = $( mask2cidr " $subnet " )
2024-06-23 01:25:23 +08:00
ipv4_addr = " $ip / $cidr "
ipv4_gateway = " $gateway "
ipv4_mac = " $mac_addr "
# 只取第一个 IP
2024-06-12 22:40:22 +08:00
break
fi
2024-06-23 01:25:23 +08:00
done
fi
2023-10-22 16:54:45 +08:00
2024-06-23 01:25:23 +08:00
# IPv6
if [ " $v " = 6 ] ; then
ipv6_type_list = $( netsh interface ipv6 show address $id normal)
for (( i = 0; i < ${# ips [@] } ; i++)) ; do
ip = ${ ips [i] }
cidr = ${ subnets [i] }
if [[ " $ip " = *:* ]] ; then
ipv6_type = $( grep " $ip " <<< " $ipv6_type_list " | awk '{print $1}' )
# Public 是 slaac
# 还有类型 Temporary,不过有 Temporary 肯定还有 Public,因此不用
if [ " $ipv6_type " = Public ] ||
[ " $ipv6_type " = Dhcp ] ||
[ " $ipv6_type " = Manual ] ; then
ipv6_addr = " $ip / $cidr "
ipv6_gateway = " $gateway "
ipv6_mac = " $mac_addr "
# 只取第一个 IP
break
fi
fi
done
fi
# 网关
# shellcheck disable=SC2154
if false; then
for gateway in " ${ gateways [@] } " ; do
if [ -n " $ipv4_addr " ] && [[ " $gateway " = *.* ]] ; then
ipv4_gateway = " $gateway "
elif [ -n " $ipv6_addr " ] && [[ " $gateway " = *:* ]] ; then
ipv6_gateway = " $gateway "
fi
done
fi
# 如果通过本条 route 的网卡找到了 IP 则退出 routes 循环
if is_found_ipv${ v } _netconf; then
break
fi
done < <( echo " $routes " )
2023-10-22 16:54:45 +08:00
done
2023-08-21 23:50:20 +08:00
else
2023-10-22 16:56:44 +08:00
# linux
# 通过默认网关得到默认网卡
2024-05-29 21:30:21 +08:00
# 多个默认路由下
# ip -6 route show default dev ens3 完全不显示
# ip -6 route show default
# default proto static metric 1024 pref medium
# nexthop via 2a01:1111:262:4940::2 dev ens3 weight 1 onlink
# nexthop via fe80::5054:ff:fed4:5286 dev ens3 weight 1
# ip -6 route show default
# default via 2602:1111:0:80::1 dev eth0 metric 1024 onlink pref medium
2025-01-10 00:43:27 +08:00
# arch + vultr
# ip -6 route show default
# default nhid 4011550343 via fe80::fc00:5ff:fe3d:2714 dev enp1s0 proto ra metric 1024 expires 1504sec pref medium
2023-10-22 16:56:44 +08:00
for v in 4 6; do
2025-01-10 00:43:27 +08:00
if via_gateway_dev_ethx = $( ip -$v route show default | grep -Ewo 'via [^ ]+ dev [^ ]+' | head -1 | grep .) ; then
read -r _ gateway _ ethx <<< " $via_gateway_dev_ethx "
2026-06-19 10:53:30 +08:00
set_var ipv${ v } _ethx " $ethx " # can_use_cloud_kernel 要用
set_var ipv${ v } _mac " $( ip link show dev $ethx | grep link/ether | head -1 | awk '{print $2}' ) "
set_var ipv${ v } _gateway " $gateway "
2026-03-08 22:17:59 +08:00
# 获取所有全局地址
all_addrs = $( ip -$v -o addr show scope global dev $ethx | grep -v temporary | awk '{print $4}' )
primary_addr = $( echo " $all_addrs " | head -1)
# IPv6: 用 ip route get 让内核返回正确的源 IP,指定 dev 避免 tun/warp 干扰
if [ " $v " = 6 ] && [ -n " $primary_addr " ] ; then
route_src = $( ip -6 route get 2001:4860:4860::8888 dev " $ethx " 2>/dev/null | grep -oP 'src \K[^ ]+' )
if [ -n " $route_src " ] ; then
for addr in $all_addrs ; do
if [ " ${ addr %/* } " = " $route_src " ] ; then
primary_addr = $addr
break
fi
done
fi
fi
2026-06-19 10:53:30 +08:00
set_var ipv${ v } _addr " $primary_addr "
2026-03-08 22:17:59 +08:00
# extra_addrs: 除主地址外的所有地址
2026-06-19 10:53:30 +08:00
set_var ipv${ v } _extra_addrs " $( echo " $all_addrs " | grep -Fxve " $primary_addr " | tr '\n' ',' | sed 's/,$//' ) "
2023-10-22 16:56:44 +08:00
fi
done
2023-08-21 23:50:20 +08:00
fi
2023-09-21 00:12:49 +08:00
2024-06-23 01:25:23 +08:00
if ! is_found_ipv4_netconf && ! is_found_ipv6_netconf; then
error_and_exit "Can not get IP info."
fi
2023-10-22 16:56:44 +08:00
info "Network Info"
2024-06-12 22:40:22 +08:00
echo "IPv4 MAC: $ipv4_mac "
2023-10-22 16:56:44 +08:00
echo "IPv4 Address: $ipv4_addr "
echo "IPv4 Gateway: $ipv4_gateway "
2024-06-12 22:40:22 +08:00
echo "---"
echo "IPv6 MAC: $ipv6_mac "
2023-10-22 16:56:44 +08:00
echo "IPv6 Address: $ipv6_addr "
echo "IPv6 Gateway: $ipv6_gateway "
2024-04-03 21:53:52 +08:00
echo
2023-08-21 23:50:20 +08:00
}
2026-03-10 00:09:05 +08:00
get_efi_dir_in_windows() {
2023-11-09 22:27:07 +08:00
# 挂载
if result = $( find /cygdrive/?/EFI/Microsoft/Boot/bootmgfw.efi 2>/dev/null) ; then
# 已经挂载
x = $( echo $result | cut -d/ -f3)
else
# 找到空盘符并挂载
for x in { a..z} ; do
[ ! -e /cygdrive/$x ] && break
done
2026-03-10 00:09:05 +08:00
if ! mountvol $x : /s >& 2; then
error_and_exit "Can't mount efi partition in windows."
fi
2023-11-09 22:27:07 +08:00
fi
2026-03-10 00:09:05 +08:00
echo "/cygdrive/ $x "
}
add_efi_entry_in_windows() {
info "Add efi entry in windows"
local source = $1
2023-11-09 22:27:07 +08:00
# 文件夹命名为reinstall而不是grub,因为可能机器已经安装了grub,bcdedit名字同理
2026-03-10 00:09:05 +08:00
dist_dir = " $( get_efi_dir_in_windows) /EFI/reinstall"
efi_drive = $( echo " $dist_dir " | cut -d/ -f3)
2023-11-09 22:27:07 +08:00
basename = $( basename $source )
2026-03-10 00:09:05 +08:00
download_or_copy_file " $source " " $dist_dir / $basename "
2023-11-09 22:27:07 +08:00
2023-12-22 23:42:12 +08:00
# 如果 {fwbootmgr} displayorder 为空
# 执行 bcdedit /copy '{bootmgr}' 会报错
# 例如 azure windows 2016 模板
# 要先设置默认的 {fwbootmgr} displayorder
# https://github.com/hakuna-m/wubiuefi/issues/286
bcdedit /set '{fwbootmgr}' displayorder '{bootmgr}' /addfirst
2023-11-09 22:27:07 +08:00
# 添加启动项
id = $( bcdedit /copy '{bootmgr}' /d " $( get_entry_name) " | grep -o '{.*}' )
2026-03-10 00:09:05 +08:00
bcdedit /set $id device partition = $efi_drive :
2023-11-09 22:27:07 +08:00
bcdedit /set $id path \\ EFI\\ reinstall\\ $basename
bcdedit /set '{fwbootmgr}' bootsequence $id
}
get_maybe_efi_dirs_in_linux() {
2026-03-10 00:09:05 +08:00
# 不从 fstab 查找,因为极端情况下可能只用 systemd mount
# arch云镜像efi分区挂载在/efi,且使用 autofs, mount 命令会有两个 /efi 条目
install_pkg findmnt >& 2
# 寻找 efi 分区,并输出根目录
# root_dirs=$(mount | awk '$5=="vfat" || $5=="autofs" {print $3}' | grep -Ex '/efi|/boot/efi|/boot' | sort -u)
root_dirs = $( findmnt -t fat,vfat -n -o TARGET | grep -Ex '/efi|/boot/efi|/boot' | sort -u)
efi_dirs = $(
for dir in $root_dirs ; do
# 只显示有 efi 文件的
# -quit 表示找到第一个 *.efi 就立即退出 find
if [ -d " $dir " ] ; then
find " $dir " -type f -iname "*.efi" -exec printf '%s\n' " $dir " \; -quit
fi
done
)
if [ -z " $efi_dirs " ] ; then
error_and_exit "Can't find efi partition."
fi
echo " $efi_dirs "
2023-11-09 22:27:07 +08:00
}
2023-12-16 15:56:20 +08:00
get_disk_by_part() {
dev_part = $1
2024-02-01 00:59:34 +08:00
install_pkg lsblk >& 2
2023-12-16 15:56:20 +08:00
lsblk -rn --inverse " $dev_part " | grep -w disk | awk '{print $1}'
}
get_part_num_by_part() {
dev_part = $1
2024-01-31 22:29:49 +08:00
grep -oE '[0-9]*$' <<< " $dev_part "
2023-12-16 15:56:20 +08:00
}
2024-08-12 19:57:14 +08:00
grep_efi_entry() {
# efibootmgr
# BootCurrent: 0002
# Timeout: 1 seconds
# BootOrder: 0000,0002,0003,0001
# Boot0000* sles-secureboot
# Boot0001* CD/DVD Rom
# Boot0002* Hard Disk
# Boot0003* sles-secureboot
# MirroredPercentageAbove4G: 0.00
# MirrorMemoryBelow4GB: false
# 根据文档,* 表示 active,也就是说有可能没有*(代表inactive)
# https://manpages.debian.org/testing/efibootmgr/efibootmgr.8.en.html
grep -E '^Boot[0-9a-fA-F]{4}'
}
2025-03-23 10:15:48 +08:00
# trans.sh 有同名方法
2023-12-22 23:51:23 +08:00
grep_efi_index() {
2024-08-12 19:57:14 +08:00
awk '{print $1}' | sed -e 's/Boot//' -e 's/\*//'
2023-12-22 23:51:23 +08:00
}
2026-03-10 00:09:05 +08:00
download_or_copy_file() {
local source = $1
local dist = $2
mkdir -p " $( dirname $dist ) "
if [[ " $source " = http* ]] ; then
curl -Lo " $dist " " $source "
else
cp -f " $source " " $dist "
fi
}
2023-11-09 22:27:07 +08:00
add_efi_entry_in_linux() {
2026-03-10 00:09:05 +08:00
local source = $1
info "Add efi entry in linux"
2023-11-09 22:27:07 +08:00
install_pkg efibootmgr
2026-03-10 00:09:05 +08:00
# 只取第一个
# 因为不用关心是否为 efi 分区,只要是 fat/vfat 格式的分区即可添加到引导
# 这里用了管道导致 get_maybe_efi_dirs_in_linux 里面的 error_and_exit 不生效
efi_part = $( get_maybe_efi_dirs_in_linux | head -1 | grep .)
dist_dir = $efi_part /EFI/reinstall
basename = $( basename $source )
download_or_copy_file " $source " " $dist_dir / $basename "
2023-12-16 15:56:20 +08:00
2026-03-10 00:09:05 +08:00
# 原系统可能不是用 grub 引导,因此不一定有 grub-probe
if false; then
grub_probe = " $( command -v grub-probe grub2-probe | head -1) "
dev_part = " $( " $grub_probe " -t device " $dist_dir " ) "
else
install_pkg findmnt
# arch findmnt 会得到
# systemd-1
# /dev/sda2
dev_part = $( findmnt -T " $dist_dir " -no SOURCE | grep '^/dev/' )
fi
2023-11-09 22:27:07 +08:00
2026-05-03 23:08:03 +08:00
set -- efibootmgr --create-only \
--disk "/dev/ $( get_disk_by_part $dev_part ) " \
--part " $( get_part_num_by_part $dev_part ) " \
--label " $( get_entry_name) " \
--loader "\\EFI\\reinstall\\ $basename "
if ! res = $( " $@ " ) ; then
echo "Command: $* "
2026-03-10 00:09:05 +08:00
echo " $res "
error_and_exit "Could not add efi entry."
fi
2026-05-03 23:08:03 +08:00
id = $( echo " $res " | grep_efi_entry | tail -1 | grep_efi_index | grep .)
efibootmgr --bootnext " $id "
2023-11-09 22:27:07 +08:00
}
2024-11-23 23:50:13 +08:00
get_grub_efi_filename() {
case " $basearch " in
x86_64) echo grubx64.efi ;;
aarch64) echo grubaa64.efi ;;
esac
}
2023-12-16 16:12:13 +08:00
install_grub_linux_efi() {
info 'download grub efi'
2024-02-02 00:49:24 +08:00
# fedora 39 的 efi 无法识别 opensuse tumbleweed 的 xfs
2026-03-17 11:33:48 +08:00
efi_distro = fedora
2025-10-31 07:50:45 +08:00
2024-11-23 23:50:13 +08:00
grub_efi = $( get_grub_efi_filename)
2023-12-23 21:03:56 +08:00
2024-01-27 23:17:50 +08:00
# 不要用 download.opensuse.org 和 download.fedoraproject.org
# 因为 ipv6 访问有时跳转到 ipv4 地址,造成 ipv6 only 机器无法下载
2024-05-22 22:32:43 +08:00
# 日韩机器有时得到国内镜像源,但镜像源屏蔽了国外 IP 导致连不上
# https://mirrors.bfsu.edu.cn/opensuse/ports/aarch64/tumbleweed/repo/oss/EFI/BOOT/grub.efi
2024-08-12 22:28:22 +08:00
# fcix 经常 404
# https://mirror.fcix.net/opensuse/tumbleweed/repo/oss/EFI/BOOT/bootx64.efi
# https://mirror.fcix.net/opensuse/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2
2025-04-12 15:44:20 +08:00
# dl.fedoraproject.org 不支持 ipv6
2023-12-23 21:03:56 +08:00
if [ " $efi_distro " = fedora ] ; then
2025-10-31 07:50:45 +08:00
# fedora 43 efi 在 vultr 无法引导 debain 9/10 netboot
2025-04-12 15:44:20 +08:00
fedora_ver = $( get_latest_distro_releasever fedora)
2023-12-22 23:46:22 +08:00
if is_in_china; then
2024-08-17 23:33:13 +08:00
mirror = https://mirror.nju.edu.cn/fedora
2023-12-22 23:46:22 +08:00
else
2025-04-12 15:44:20 +08:00
mirror = https://d2lzkl7pfhq30w.cloudfront.net/pub/fedora/linux
2023-12-22 23:46:22 +08:00
fi
2024-04-03 21:51:18 +08:00
curl -Lo $tmp /$grub_efi $mirror /releases/$fedora_ver /Everything/$basearch /os/EFI/BOOT/$grub_efi
2023-12-16 16:12:13 +08:00
else
2023-12-22 23:46:22 +08:00
if is_in_china; then
2024-12-06 22:50:05 +08:00
mirror = https://mirror.nju.edu.cn/opensuse
2023-12-22 23:46:22 +08:00
else
2025-03-10 22:23:37 +08:00
mirror = https://downloadcontentcdn.opensuse.org
2023-12-22 23:46:22 +08:00
fi
2024-02-02 00:49:24 +08:00
[ " $basearch " = x86_64 ] && ports = '' || ports = /ports/$basearch
2024-04-03 21:51:18 +08:00
curl -Lo $tmp /$grub_efi $mirror$ports /tumbleweed/repo/oss/EFI/BOOT/grub.efi
2023-12-16 16:12:13 +08:00
fi
2024-04-03 21:51:18 +08:00
add_efi_entry_in_linux $tmp /$grub_efi
2023-12-16 16:12:13 +08:00
}
2024-11-23 23:50:13 +08:00
download_and_extract_apk() {
local alpine_ver = $1
local package = $2
local extract_dir = $3
install_pkg tar xz
is_in_china && mirror = http://mirror.nju.edu.cn/alpine || mirror = https://dl-cdn.alpinelinux.org/alpine
package_apk = $( curl -L $mirror /v$alpine_ver /main/$basearch / | grep -oP " $package -[^-]*-[^-]*\.apk" | sort -u)
if ! [ " $( wc -l <<< " $package_apk " ) " -eq 1 ] ; then
error_and_exit "find no/multi apks."
fi
mkdir -p " $extract_dir "
# 屏蔽警告
tar 2>& 1 | grep -q BusyBox && tar_args = || tar_args = --warning= no-unknown-keyword
curl -L " $mirror /v $alpine_ver /main/ $basearch / $package_apk " | tar xz $tar_args -C " $extract_dir "
}
2023-11-09 22:27:07 +08:00
install_grub_win() {
2023-06-18 21:27:22 +08:00
# 下载 grub
info download grub
2026-03-10 00:09:05 +08:00
2026-06-13 21:36:12 +08:00
# https://wuyou.net/forum.php?mod=viewthread&tid=449379&extra=page%3D1&page=2
# 2.14
# efi 正常
# bios 报错 ld.gold bug https://lists.gnu.org/archive/html/grub-devel/2026-01/msg00041.html
# 替换成 alpine/arch 的模块后,出现 ntfs 读取 out of range 错误
# 2.12
# efi 报错 __stack_chk_guard https://lists.gnu.org/archive/html/bug-grub/2024-01/msg00002.html
# 替换成 alpine/arch 的模块后,正常
# bios 正常
# 2.06
# 一切正常
# 要使用的 grub 版本
if is_efi; then
local grub_ver = 2.14
else
local grub_ver = 2.12
fi
# grub 对应的 alpine 版本
case " $grub_ver " in
2.14) local alpine_ver = 3.24 ;;
2026-09-06 11:53:44 +08:00
2.12) local alpine_ver = 3.23 ;;
2.06) local alpine_ver = 3.19 ;;
2026-06-13 21:36:12 +08:00
esac
# grub 架构名和对应的 alpine 包名
if is_efi; then
local alpine_grub_pkg = grub-efi
case " $basearch " in
x86_64) local grub_arch = x86_64-efi ;;
aarch64) local grub_arch = arm64-efi ;;
esac
else
local alpine_grub_pkg = grub-bios
local grub_arch = i386-pc
fi
# 是否需要从 alpine 获取/替换 grub 模块
# arm64-efi 要从 alpine 下载 grub 模块
local need_download_grub_module_from_alpine = false
if [ " $grub_arch " = arm64-efi ] ; then
need_download_grub_module_from_alpine = true
fi
2026-03-10 00:09:05 +08:00
2024-05-22 22:32:43 +08:00
# ftpmirror.gnu.org 是 geoip 重定向,不是 cdn
# 有可能重定义到一个拉黑了部分 IP 的服务器
2026-03-10 00:09:05 +08:00
# 换成 ftp.gnu.org?
2024-08-17 23:33:13 +08:00
is_in_china && grub_url = https://mirror.nju.edu.cn/gnu/grub/grub-$grub_ver -for-windows.zip ||
2024-10-01 20:52:50 +08:00
grub_url = https://mirrors.kernel.org/gnu/grub/grub-$grub_ver -for-windows.zip
2024-04-03 21:51:18 +08:00
curl -Lo $tmp /grub.zip $grub_url
# unzip -qo $tmp/grub.zip
7z x $tmp /grub.zip -o$tmp -r -y -xr!i386-efi -xr!locale -xr!themes -bso0
grub_dir = $tmp /grub-$grub_ver -for-windows
2023-11-09 22:27:07 +08:00
grub = $grub_dir /grub
2023-06-18 21:27:22 +08:00
2026-06-13 21:36:12 +08:00
# 下载/替换 grub 模块
if $need_download_grub_module_from_alpine ; then
info 'download grub modules from alpine'
download_and_extract_apk $alpine_ver $alpine_grub_pkg $tmp /grub-from-alpine
cp -r $tmp /grub-from-alpine/usr/lib/grub/$grub_arch / $grub_dir
fi
2024-01-27 23:08:34 +08:00
# 设置 grub 包含的模块
2026-09-06 11:53:44 +08:00
# 原系统是 windows,因此不需要 ext2 lvm xfs btrfs 模块
# vmlinuz/initramfs 不需要 grub 解压,因此不需要 lzopio xzio gzio zstd 模块
grub_modules = "normal minicmd serial ls echo test cat reboot halt linux chain search all_video configfile"
grub_modules += " scsi part_msdos part_gpt fat ntfs ntfscomp"
2023-06-18 21:27:22 +08:00
if ! is_efi; then
2024-01-27 23:08:34 +08:00
grub_modules += " biosdisk linux16"
2023-06-18 21:27:22 +08:00
fi
# 设置 grub prefix 为c盘根目录
# 运行 grub-probe 会改变cmd窗口字体
2026-06-13 21:36:12 +08:00
local prefix
2024-01-27 23:17:50 +08:00
prefix = $( $grub -probe -t drive $c : | sed 's|.*PhysicalDrive|(hd|' | del_cr) /
2023-06-18 21:27:22 +08:00
echo $prefix
# 安装 grub
if is_efi; then
# efi
info install grub for efi
2024-11-23 23:50:13 +08:00
grub_efi = $( get_grub_efi_filename)
2026-06-13 21:36:12 +08:00
$grub -mkimage -p $prefix -O $grub_arch -o " $( cygpath -w " $grub_dir / $grub_efi " ) " $grub_modules
2024-11-23 23:50:13 +08:00
add_efi_entry_in_windows " $grub_dir / $grub_efi "
2023-06-18 21:27:22 +08:00
else
# bios
info install grub for bios
2024-01-27 23:19:14 +08:00
# bootmgr 加载 g2ldr 有大小限制
# 超过大小会报错 0xc000007b
2023-10-07 22:36:40 +08:00
# 解决方法1 g2ldr.mbr + g2ldr
2023-06-18 21:27:22 +08:00
# 解决方法2 生成少于64K的 g2ldr + 动态模块
2024-01-27 23:19:14 +08:00
if false; then
# g2ldr.mbr
# 部分国内机无法访问 ftp.cn.debian.org
2024-08-17 23:33:13 +08:00
is_in_china && host = mirror.nju.edu.cn || host = deb.debian.org
2026-03-10 00:09:05 +08:00
curl -LO http://$host /debian/tools/win32-loader/oldstable/win32-loader.exe
2024-04-03 21:51:18 +08:00
7z x win32-loader.exe 'g2ldr.mbr' -o$tmp /win32-loader -r -y -bso0
find $tmp /win32-loader -name 'g2ldr.mbr' -exec cp {} /cygdrive/$c / \;
2023-06-18 21:27:22 +08:00
2024-01-27 23:19:14 +08:00
# g2ldr
# 配置文件 c:\grub.cfg
2026-06-13 21:36:12 +08:00
$grub -mkimage -p " $prefix " -O $grub_arch -o " $( cygpath -w $grub_dir /core.img) " $grub_modules
cat $grub_dir /$grub_arch /lnxboot.img $grub_dir /core.img >/cygdrive/$c /g2ldr
2024-01-27 23:19:14 +08:00
else
# grub-install 无法设置 prefix
# 配置文件 c:\grub\grub.cfg
$grub -install $c \
2026-06-13 21:36:12 +08:00
--target= $grub_arch \
2024-01-27 23:19:14 +08:00
--boot-directory= $c : \
--install-modules= " $grub_modules " \
--themes= \
--fonts= \
--no-bootsector
2023-06-18 21:27:22 +08:00
2026-06-13 21:36:12 +08:00
cat $grub_dir /$grub_arch /lnxboot.img /cygdrive/$c /grub/$grub_arch /core.img >/cygdrive/$c /g2ldr
2024-01-27 23:19:14 +08:00
fi
2023-06-18 21:27:22 +08:00
# 添加引导
# 脚本可能不是首次运行,所以先删除原来的
id = '{1c41f649-1637-52f1-aea8-f96bfebeecc8}'
2026-03-15 23:32:51 +08:00
bcdedit /enum all | grep -a $id && bcdedit /delete $id
2023-11-09 22:27:07 +08:00
bcdedit /create $id /d " $( get_entry_name) " /application bootsector
2023-06-18 21:27:22 +08:00
bcdedit /set $id device partition = $c :
2024-01-27 23:19:14 +08:00
bcdedit /set $id path \\ g2ldr
2023-06-18 21:27:22 +08:00
bcdedit /displayorder $id /addlast
bcdedit /bootsequence $id /addfirst
2023-05-17 22:54:26 +08:00
fi
}
2024-09-09 22:56:13 +08:00
find_grub_extlinux_cfg() {
dir = $1
filename = $2
keyword = $3
# 当 ln -s /boot/grub /boot/grub2 时
# find /boot/ 会自动忽略 /boot/grub2 里面的文件
cfgs = $(
# 只要 $dir 存在
# 无论是否找到结果,返回值都是 0
find $dir \
-type f -name $filename \
-exec grep -E -l " $keyword " {} \;
)
count = " $( wc -l <<< " $cfgs " ) "
if [ " $count " -eq 1 ] ; then
echo " $cfgs "
else
error_and_exit "Find $count $filename ."
fi
}
2024-10-12 23:07:01 +08:00
# 空格、&、用户输入的网址要加引号,否则 grub 无法正确识别
is_need_quote() {
[[ " $1 " = *' ' * ]] || [[ " $1 " = *'&' * ]] || [[ " $1 " = http* ]]
}
2023-05-03 22:22:21 +08:00
# 转换 finalos_a=1 为 finalos.a=1 ,排除 finalos_mirrorlist
build_finalos_cmdline() {
2023-06-18 21:27:22 +08:00
if vars = $( compgen -v finalos_) ; then
for key in $vars ; do
value = ${ !key }
key = ${ key #finalos_ }
if [ -n " $value " ] && [ $key != "mirrorlist" ] ; then
2024-10-12 23:07:01 +08:00
is_need_quote " $value " &&
finalos_cmdline += " finalos_ $key =' $value '" ||
finalos_cmdline += " finalos_ $key = $value "
2023-06-18 21:27:22 +08:00
fi
done
fi
2023-05-03 22:22:21 +08:00
}
build_extra_cmdline() {
2024-07-17 22:31:42 +08:00
# 使用 extra_xxx=yyy 而不是 extra.xxx=yyy
# 因为 debian installer /lib/debian-installer-startup.d/S02module-params
# 会将 extra.xxx=yyy 写入新系统的 /etc/modprobe.d/local.conf
# https://answers.launchpad.net/ubuntu/+question/249456
# https://salsa.debian.org/installer-team/rootskel/-/blob/master/src/lib/debian-installer-startup.d/S02module-params?ref_type=heads
2026-09-10 01:06:52 +08:00
for key in confhome hold force_boot_mode force_cn force_old_windows_setup cloud_image no_cloud_kernel no_auto_drivers main_disk \
2025-02-06 18:47:17 +08:00
elts deb_mirror \
2026-09-06 11:31:38 +08:00
username ssh_port rdp_port web_port web_path allow_ping; do
2023-05-13 00:14:46 +08:00
value = ${ !key }
if [ -n " $value " ] ; then
2024-10-12 23:07:01 +08:00
is_need_quote " $value " &&
extra_cmdline += " extra_ $key =' $value '" ||
extra_cmdline += " extra_ $key = $value "
2023-05-03 22:22:21 +08:00
fi
2023-05-13 00:14:46 +08:00
done
# 指定最终安装系统的 mirrorlist,链接有&,在grub中是特殊字符,所以要加引号
if [ -n " $finalos_mirrorlist " ] ; then
2024-07-17 22:31:42 +08:00
extra_cmdline += " extra_mirrorlist=' $finalos_mirrorlist '"
2023-05-13 00:14:46 +08:00
elif [ -n " $nextos_mirrorlist " ] ; then
2024-07-17 22:31:42 +08:00
extra_cmdline += " extra_mirrorlist=' $nextos_mirrorlist '"
2023-05-03 22:22:21 +08:00
fi
2024-06-05 22:23:18 +08:00
# cloudcone 特殊处理
if is_grub_dir_linked; then
2024-07-17 22:31:42 +08:00
finalos_cmdline += " extra_link_grub_dir=1"
2024-06-05 22:23:18 +08:00
fi
2023-05-03 22:22:21 +08:00
}
2023-09-03 19:35:01 +08:00
echo_tmp_ttys() {
2024-08-20 00:27:51 +08:00
if false; then
curl -L $confhome /ttys.sh | sh -s "console="
else
case " $basearch " in
x86_64) echo "console=ttyS0,115200n8 console=tty0" ;;
aarch64) echo "console=ttyS0,115200n8 console=ttyAMA0,115200n8 console=tty0" ;;
esac
fi
2023-09-03 19:35:01 +08:00
}
2023-11-09 22:27:07 +08:00
get_entry_name() {
2024-01-27 23:17:50 +08:00
printf 'reinstall ('
printf '%s' " $distro "
[ -n " $releasever " ] && printf ' %s' " $releasever "
[ " $distro " = alpine ] && [ " $hold " = 1 ] && printf ' Live OS'
printf ')'
2023-11-09 22:27:07 +08:00
}
2023-06-18 21:27:22 +08:00
# shellcheck disable=SC2154
2023-12-02 22:46:16 +08:00
build_nextos_cmdline() {
if [ $nextos_distro = alpine ] ; then
nextos_cmdline = "alpine_repo= $nextos_repo modloop= $nextos_modloop "
2024-05-08 22:17:33 +08:00
elif is_distro_like_debian $nextos_distro ; then
2025-10-31 07:50:40 +08:00
# 设置分辨率为800*600,防止分辨率过高 ssh screen attach 后无法全部显示
# iso 默认有 vga=788
# 如果要设置位数: video=800x600-16
2024-05-03 21:37:07 +08:00
nextos_cmdline = "lowmem/low=1 auto=true priority=critical"
2025-10-31 07:50:40 +08:00
# nextos_cmdline+=" vga=788 video=800x600"
2024-05-03 21:37:07 +08:00
nextos_cmdline += " url= $nextos_ks "
2025-02-06 18:47:17 +08:00
nextos_cmdline += " mirror/http/hostname= ${ nextos_udeb_mirror %/* } "
nextos_cmdline += " mirror/http/directory=/ ${ nextos_udeb_mirror ##*/ } "
2024-05-03 21:37:07 +08:00
nextos_cmdline += " base-installer/kernel/image= $nextos_kernel "
2024-10-20 19:38:17 +08:00
# elts 的 debian 不能用 security 源,否则安装过程会提示无法访问
if [ " $nextos_distro " = debian ] && is_debian_elts; then
2024-07-14 22:11:53 +08:00
nextos_cmdline += " apt-setup/services-select="
fi
2024-05-03 21:37:07 +08:00
# kali 安装好后网卡是 eth0 这种格式,但安装时不是
if [ " $nextos_distro " = kali ] ; then
nextos_cmdline += " net.ifnames=0"
2024-05-22 21:39:00 +08:00
nextos_cmdline += " simple-cdd/profiles=kali"
2024-05-03 21:37:07 +08:00
fi
2024-05-08 22:17:33 +08:00
elif is_distro_like_redhat $nextos_distro ; then
2023-12-02 22:46:16 +08:00
# redhat
nextos_cmdline = "root=live: $nextos_squashfs inst.ks= $nextos_ks "
2023-06-18 21:27:22 +08:00
fi
2023-12-02 22:46:16 +08:00
2024-05-08 22:17:33 +08:00
if is_distro_like_debian $nextos_distro ; then
2024-03-17 00:08:48 +08:00
if [ " $basearch " = "x86_64" ] ; then
2024-05-22 21:37:30 +08:00
# debian installer 好像第一个 tty 是主 tty
2024-03-18 23:56:10 +08:00
# 设置ttyS0,tty0,安装界面还是显示在ttyS0
2024-03-17 00:08:48 +08:00
:
else
2024-03-18 23:56:10 +08:00
# debian arm 在没有ttyAMA0的机器上(aws t4g),最少要设置一个tty才能启动
# 只设置tty0也行,但安装过程ttyS0没有显示
2024-08-20 00:27:51 +08:00
nextos_cmdline += " $( echo_tmp_ttys) "
2024-03-17 00:08:48 +08:00
fi
else
2024-08-20 00:27:51 +08:00
nextos_cmdline += " $( echo_tmp_ttys) "
2024-03-17 00:08:48 +08:00
fi
2023-12-02 22:46:16 +08:00
# nextos_cmdline+=" mem=256M"
2024-05-03 21:37:07 +08:00
# nextos_cmdline+=" lowmem=+1"
2023-12-02 22:46:16 +08:00
}
build_cmdline() {
# nextos
build_nextos_cmdline
# finalos
# trans 需要 finalos_distro 识别是安装 alpine 还是其他系统
if [ " $distro " = alpine ] ; then
finalos_distro = alpine
fi
if [ -n " $finalos_distro " ] ; then
build_finalos_cmdline
fi
# extra
build_extra_cmdline
cmdline = " $nextos_cmdline $finalos_cmdline $extra_cmdline "
2023-06-18 21:27:22 +08:00
}
2023-05-13 00:14:46 +08:00
2023-07-16 15:32:45 +08:00
# 脚本可能多次运行,先清理之前的残留
mkdir_clear() {
2026-04-28 07:33:09 +08:00
local dir = $1
2023-07-16 15:32:45 +08:00
if [ -z " $dir " ] || [ " $dir " = / ] ; then
return
fi
2025-04-15 23:40:58 +08:00
# 再次运行时,有可能 mount 了 btrfs root,因此先要 umount_all
# 但目前不需要 mount ,因此用不到
2025-10-31 07:50:45 +08:00
# umount_all "$dir"
rm -rf " $dir "
mkdir -p " $dir "
2023-07-16 15:32:45 +08:00
}
2024-05-22 21:37:30 +08:00
mod_initrd_debian_kali() {
2024-03-16 23:06:38 +08:00
# hack 1
# 允许设置 ipv4 onlink 网关
sed -Ei 's,&&( onlink=),||\1,' etc/udhcpc/default.script
2023-05-13 00:14:46 +08:00
2024-03-16 23:06:38 +08:00
# hack 2
2025-10-31 07:50:40 +08:00
# 强制使用 screen
# shellcheck disable=SC1003,SC2016
{
echo 'if false && : \' | insert_into_file lib/debian-installer.d/S70menu before 'if [ -x "$bterm" ]' -F
echo 'if true || : \' | insert_into_file lib/debian-installer.d/S70menu before 'if [ -x "$screen_bin" -a' -F
}
# hack 3
2024-03-16 23:06:38 +08:00
# 修改 /var/lib/dpkg/info/netcfg.postinst 运行我们的脚本
netcfg() {
#!/bin/sh
2024-08-20 00:27:51 +08:00
# shellcheck source=/dev/null
2024-03-16 23:06:38 +08:00
. /usr/share/debconf/confmodule
db_progress START 0 5 debian-installer/netcfg/title
2023-05-13 00:14:46 +08:00
2024-06-12 22:40:22 +08:00
: get_ip_conf_cmd
2023-07-05 22:00:29 +08:00
2024-06-12 22:40:22 +08:00
# 运行 trans.sh,保存配置
2024-03-16 23:06:38 +08:00
db_progress INFO base-installer/progress/netcfg
2025-10-31 07:50:40 +08:00
# 添加 || exit ,可以在 debian installer 不兼容 /trans.sh 语法时强制报错
# exit 不带参数,返回值为 || 前面命令的返回值
sh /trans.sh || exit
2024-03-16 23:06:38 +08:00
db_progress STEP 1
2025-10-31 07:50:40 +08:00
db_progress STOP
2024-03-16 23:06:38 +08:00
}
2024-06-12 22:40:22 +08:00
postinst = var/lib/dpkg/info/netcfg.postinst
get_function_content netcfg >$postinst
get_ip_conf_cmd | insert_into_file $postinst after ": get_ip_conf_cmd"
# cat $postinst
2024-03-16 23:06:38 +08:00
2025-10-31 07:50:40 +08:00
# hack 4
# 修改 udeb 依赖
# 直接覆盖 net-retriever,方便调试
# curl -Lo /usr/lib/debian-installer/retriever/net-retriever $confhome/net-retriever
2024-07-14 22:11:53 +08:00
change_priority() {
while IFS = read -r line; do
2024-11-23 23:50:13 +08:00
if [[ " $line " = Package:* ]] ; then
package = $( echo " $line " | cut -d' ' -f2-)
2024-05-03 21:37:07 +08:00
2024-11-23 23:50:13 +08:00
elif [[ " $line " = Priority:* ]] ; then
2024-04-03 21:53:52 +08:00
# shellcheck disable=SC2154
2024-11-23 23:50:13 +08:00
if [ " $line " = "Priority: standard" ] ; then
for p in $disabled_list ; do
if [ " $package " = " $p " ] ; then
line = "Priority: optional"
break
fi
done
2024-05-22 21:37:30 +08:00
elif [[ " $package " = ata-modules* ]] ; then
# 改成强制安装
# 因为是 pata-modules sata-modules scsi-modules 的依赖
# 但我们没安装它们,也就不会自动安装 ata-modules
line = "Priority: standard"
2024-04-03 21:53:52 +08:00
fi
2024-11-23 23:50:13 +08:00
fi
2024-05-03 21:37:07 +08:00
echo " $line "
2024-04-03 21:53:52 +08:00
done
}
# shellcheck disable=SC2012
kver = $( ls -d lib/modules/* | awk -F/ '{print $NF}' )
net_retriever = usr/lib/debian-installer/retriever/net-retriever
2024-07-14 22:11:53 +08:00
# shellcheck disable=SC2016
sed -i 's,>> "$1",| change_priority >> "$1",' $net_retriever
2024-04-03 21:53:52 +08:00
insert_into_file $net_retriever after '#!/bin/sh' <<EOF
disabled_list="
depthcharge-tools-installer
kickseed-common
nobootloader
partman-btrfs
partman-cros
partman-iscsi
partman-jfs
partman-md
partman-xfs
rescue-check
wpasupplicant-udeb
2024-07-14 22:11:53 +08:00
lilo-installer
2025-08-10 08:02:22 +08:00
systemd-boot-installer
2024-04-03 21:53:52 +08:00
nic-modules-$kver-di
nic-pcmcia-modules-$kver-di
nic-usb-modules-$kver-di
nic-wireless-modules-$kver-di
nic-shared-modules-$kver-di
pcmcia-modules-$kver-di
pcmcia-storage-modules-$kver-di
cdrom-core-modules-$kver-di
firewire-core-modules-$kver-di
usb-storage-modules-$kver-di
isofs-modules-$kver-di
jfs-modules-$kver-di
xfs-modules-$kver-di
loop-modules-$kver-di
pata-modules-$kver-di
sata-modules-$kver-di
scsi-modules-$kver-di
"
2024-07-14 22:11:53 +08:00
$(get_function change_priority)
2024-04-03 21:53:52 +08:00
EOF
2024-04-29 22:26:12 +08:00
# https://github.com/linuxhw/LsPCI?tab=readme-ov-file#storageata-pci
2024-04-03 21:53:52 +08:00
# https://debian.pkgs.org/12/debian-main-amd64/linux-image-6.1.0-18-cloud-amd64_6.1.76-1_amd64.deb.html
2024-05-22 21:37:30 +08:00
# https://deb.debian.org/debian/pool/main/l/linux-signed-amd64/
# https://deb.debian.org/debian/dists/bookworm/main/debian-installer/binary-all/Packages.xz
# https://deb.debian.org/debian/dists/bookworm/main/debian-installer/binary-amd64/Packages.xz
# 以下是 debian-installer 有的驱动,这些驱动云内核不一定都有,(+)表示云内核有
# scsi-core-modules 默认安装(不用修改),是 ata-modules 的依赖
# 包含 sd_mod.ko(+) scsi_mod.ko(+) scsi_transport_fc.ko(+) scsi_transport_sas.ko(+) scsi_transport_spi.ko(+)
# ata-modules 默认可选(改成必装),是下方模块的依赖。只有 ata_generic.ko(+) 和 libata.ko(+) 两个驱动
# pata-modules 默认安装(改成可选),里面的驱动都是 pata_ 开头,但只有 pata_legacy.ko(+) 在云内核中
# sata-modules 默认安装(改成可选),里面的驱动大部分是 sata_ 开头的,其他重要的还有 ahci.ko libahci.ko ata_piix.ko(+)
2026-09-06 11:53:44 +08:00
# 云内核没有 sata 模块,也没有内嵌,有一个 CONFIG_SATA_HOST=y, libata-$(CONFIG_SATA_HOST) += libata-sata.o
2024-05-22 21:37:30 +08:00
# scsi-modules 默认安装(改成可选),包含 nvme.ko(+) 和各种虚拟化驱动(+)
2024-04-03 21:53:52 +08:00
2025-10-31 07:50:40 +08:00
download_and_extract_deb() {
local type = $1
local package = $2
local extract_dir = $3
2024-04-03 21:53:52 +08:00
2025-10-31 07:50:40 +08:00
# shellcheck disable=SC2154
case " $type " in
deb)
local mirror = $nextos_deb_mirror
local url = http://$mirror /dists/$nextos_codename /main/binary-$basearch_alt /Packages.gz
;;
udeb)
local mirror = $nextos_udeb_mirror
local url = http://$mirror /dists/$nextos_codename /main/debian-installer/binary-$basearch_alt /Packages.gz
;;
esac
# 获取 deb/udeb 列表
deb_list = $tmp /${ type } _list
if ! [ -f $deb_list ] ; then
curl -L " $url " | zcat | grep 'Filename:' | awk '{print $2}' >$deb_list
2024-04-03 21:53:52 +08:00
fi
2025-10-31 07:50:40 +08:00
# 下载 deb/udeb
deb_path = $( grep -F "/ ${ package } _" " $deb_list " )
curl -Lo $tmp /tmp.deb http://$mirror /" $deb_path "
2024-04-03 21:53:52 +08:00
if false; then
# 使用 dpkg
# cygwin 没有 dpkg
install_pkg dpkg
2025-10-31 07:50:40 +08:00
dpkg -x $tmp /tmp.deb $extract_dir
2024-04-03 21:53:52 +08:00
else
# 使用 ar tar xz
# cygwin 需安装 binutils
# centos7 ar 不支持 --output
install_pkg ar tar xz
2025-10-31 07:50:40 +08:00
( cd $tmp && ar x $tmp /tmp.deb)
2024-04-03 21:53:52 +08:00
tar xf $tmp /data.tar.xz -C $extract_dir
fi
}
2026-04-28 07:33:07 +08:00
cp_debian_kali_driver() {
# debian 13 的 linux-image.deb 有 /usr/lib 没有 /lib
# debian 13 的 scsi-modules.udeb 没有 /usr/lib 有 /lib
local src_drivers_dir = $1 /lib/modules/$kver /kernel/drivers
if ! [ -d " $src_drivers_dir " ] ; then
local src_drivers_dir = $1 /usr/lib/modules/$kver /kernel/drivers
fi
local extra_drivers = $2
# 各个版本的 debian/kali installer initrd 都有 /lib
local dst_drivers_dir = $initrd_dir /lib/modules/$kver /kernel/drivers
(
cd $src_drivers_dir
for driver in $extra_drivers ; do
# debian 模块没有压缩
# kali 模块有压缩
# 因此要有 *
if ! find $dst_drivers_dir -name " $driver .ko*" | grep -q .; then
echo "adding driver: $driver "
file = $( find . -name " $driver .ko*" | grep .)
cp -fv --parents " $file " " $dst_drivers_dir "
fi
done
)
}
2024-04-03 21:53:52 +08:00
# 不用在 windows 判断是哪种硬盘控制器,因为 256M 运行 windows 只可能是 xp,而脚本本来就不支持 xp
2024-04-29 22:26:12 +08:00
# 在 debian installer 中判断能否用云内核
2024-05-22 21:37:30 +08:00
create_can_use_cloud_kernel_sh can_use_cloud_kernel.sh
2024-04-29 22:26:12 +08:00
2025-01-10 00:43:26 +08:00
# 下载 fix-eth-name 脚本
curl -LO " $confhome /fix-eth-name.sh"
curl -LO " $confhome /fix-eth-name.service"
2025-04-28 23:18:27 +08:00
# 有段时间 kali initrd 删除了原版 wget
2024-06-03 20:58:02 +08:00
# 但 initrd 的 busybox wget 又不支持 https
# 因此改成在这里下载
curl -LO " $confhome /get-xda.sh"
curl -LO " $confhome /ttys.sh"
2025-04-28 23:18:27 +08:00
if [ -n " $frpc_config " ] ; then
curl -LO " $confhome /get-frpc-url.sh"
curl -LO " $confhome /frpc.service"
fi
2024-06-03 20:58:02 +08:00
2024-05-22 21:37:30 +08:00
# 可以节省一点内存?
echo 'export DEBCONF_DROP_TRANSLATIONS=1' |
insert_into_file lib/debian-installer/menu before 'exec debconf'
2024-04-29 22:26:12 +08:00
2024-05-22 21:39:00 +08:00
# 还原 kali netinst.iso 的 simple-cdd 机制
# 主要用于调用 kali.postinst 设置 zsh 为默认 shell
# 但 mini.iso 又没有这种机制
2026-09-11 00:11:38 +08:00
# https://gitlab.com/kalilinux/build-scripts/kali-installer/-/raw/main/simple-cdd/profiles/kali.postinst?ref_type=heads
2024-05-22 21:39:00 +08:00
# https://salsa.debian.org/debian/simple-cdd/-/blob/master/debian/14simple-cdd?ref_type=heads
# https://http.kali.org/pool/main/s/simple-cdd/simple-cdd-profiles_0.6.9_all.udeb
if [ " $distro " = kali ] ; then
# 但我们没有使用 iso,因此没有 kali.postinst,需要另外下载
mkdir -p cdrom/simple-cdd
2026-09-11 00:11:38 +08:00
curl -Lo cdrom/simple-cdd/kali.postinst https://gitlab.com/kalilinux/build-scripts/kali-installer/-/raw/main/simple-cdd/profiles/kali.postinst?ref_type= heads
2024-05-22 21:39:00 +08:00
chmod a+x cdrom/simple-cdd/kali.postinst
2026-09-11 00:11:38 +08:00
# kali simple-cdd 阶段将 apt 源改成 deb822 格式
# 但是写死了 http://http.kali.org/kali/ 和 kali-rolling
# 因此在这里改回去
# https://gitlab.com/kalilinux/build-scripts/kali-installer/-/raw/main/simple-cdd/profiles/kali.postinst?ref_type=heads
sed -E -i \
-e "s|^URIs: http://http.kali.org/kali/ $|URIs: http:// $nextos_deb_mirror /|" \
-e "s|^Suites: kali-rolling $|Suites: $nextos_codename |" \
cdrom/simple-cdd/kali.postinst
2024-05-22 21:39:00 +08:00
fi
2026-09-06 11:31:38 +08:00
# 安装 kali-last-snapshot 时
# 要将以下几处的 kali-rolling 替换为 kali-last-snapshot
# 注意系统安装后 /etc/apt/sources.list.d/kali.sources 依然是 kali-rolling
# kali-linux-202x.x-installer-netinst-amd64.iso 安装后也是 kali-rolling
# 而微软商店的 kali 的 kali.sources 是 kali-last-snapshot
if [ " $distro " = kali ] && [ " $releasever " = last-snapshot ] ; then
sed -i "s/kali-rolling/kali-last-snapshot/" \
preseed.cfg \
etc/default-release \
etc/udebs-source
fi
2024-10-20 19:38:17 +08:00
if [ " $distro " = debian ] && is_debian_elts; then
2024-07-14 22:11:53 +08:00
curl -Lo usr/share/keyrings/debian-archive-keyring.gpg https://deb.freexian.com/extended-lts/archive-key.gpg
fi
2025-10-31 07:50:40 +08:00
# 提前下载 sshd
# 以便在配置下载源之前就可以启动 sshd
mkdir_clear $tmp /sshd
download_and_extract_deb udeb openssh-server-udeb $tmp /sshd
cp -r $tmp /sshd/* .
2024-04-03 21:53:52 +08:00
# 提前下载 fdisk
# 因为 fdisk-udeb 包含 fdisk 和 sfdisk,提前下载可减少占用
mkdir_clear $tmp /fdisk
2025-10-31 07:50:40 +08:00
download_and_extract_deb udeb fdisk-udeb $tmp /fdisk
2024-04-03 21:53:52 +08:00
cp -f $tmp /fdisk/usr/sbin/fdisk usr/sbin/
2025-10-31 07:50:40 +08:00
# 下载 websocketd
# debian 11+ 才有 websocketd
if [ " $distro " = kali ] ||
{ [ " $distro " = debian ] && [ " $releasever " -ge 11 ] ; } ; then
mkdir_clear $tmp /websocketd
download_and_extract_deb deb websocketd $tmp /websocketd
cp -f $tmp /websocketd/usr/bin/websocketd usr/bin/
fi
2026-04-28 07:33:07 +08:00
# 提前下载 pci-hyperv
# udeb 没有这个模块 curl https://deb.debian.org/debian/dists/stable/main/Contents-udeb-amd64.gz | zcat | grep pci-hyperv
# 缺少这个模块 azure 会找不到 nvme 硬盘
# kali 的 pci-hyperv/pci-hyperv-intf 已嵌入到内核,不需要下载
# 用到 pci-hyperv 才需要下载,因为
# 1. azure 普通网卡、scsi 硬盘不需要这个模块
# 2. 没有这个模块会缺少加速网卡,但还有 hyperv 合成网卡,可以正常上网
if { is_in_windows && wmic PATH Win32_PnPEntity where "DeviceID like 'VMBUS\\\\{44C4F61D-4444-4400-9D52-802E27EDE19F}\\\\%'" | grep -q . ||
[ -d /sys/module/pci_hyperv ] ; } &&
# 可能在 host 或 controller 文件夹
! ls lib/modules/$kver /kernel/drivers/pci/*/pci-hyperv.ko* >/dev/null 2>& 1 &&
! grep -Fq /pci-hyperv.ko lib/modules/$kver /modules.builtin; then
mkdir_clear $tmp /linux-image-$kver
download_and_extract_deb deb linux-image-$kver $tmp /linux-image-$kver
cp_debian_kali_driver $tmp /linux-image-$kver pci-hyperv
fi
2024-06-12 22:40:22 +08:00
# >256M 或者当前系统是 windows
if [ $ram_size -gt 256 ] || is_in_windows; then
2024-04-03 21:53:52 +08:00
sed -i '/^pata-modules/d' $net_retriever
sed -i '/^sata-modules/d' $net_retriever
sed -i '/^scsi-modules/d' $net_retriever
else
# <=256M 极限优化
find_main_disk
extra_drivers =
2024-07-11 21:03:47 +08:00
for driver in $( get_disk_drivers $xda ) ; do
2024-04-03 21:53:52 +08:00
echo "using driver: $driver "
case $driver in
2026-04-24 23:12:53 +08:00
nvme)
extra_drivers += " nvme nvme-core"
# debian 13+ / kali 有 nvme-auth 模块
# 添加后才能识别 nvme 硬盘
if grep -q nvme-auth lib/modules/$kver /modules.order; then
extra_drivers += " nvme-auth"
fi
;;
# xen 的横杠特别不同
2024-07-14 22:11:53 +08:00
xen_blkfront) extra_drivers += " xen-blkfront" ;;
xen_scsifront) extra_drivers += " xen-scsifront" ;;
virtio_blk | virtio_scsi | hv_storvsc | vmw_pvscsi) extra_drivers += " $driver " ;;
2024-05-22 21:37:30 +08:00
pata_legacy) sed -i '/^pata-modules/d' $net_retriever ;; # 属于 pata-modules
ata_piix) sed -i '/^sata-modules/d' $net_retriever ;; # 属于 sata-modules
ata_generic) ;; # 属于 ata-modules,不用处理,因为我们设置强制安装了 ata-modules
2024-04-03 21:53:52 +08:00
esac
done
# extra drivers
2024-05-22 21:37:30 +08:00
# xen 还需要以下两个?
# kernel/drivers/xen/xen-scsiback.ko
# kernel/drivers/block/xen-blkback/xen-blkback.ko
2026-04-28 07:33:07 +08:00
# udeb 没有这个模块 curl https://deb.debian.org/debian/dists/stable/main/Contents-udeb-amd64.gz | zcat | grep xen
2024-04-03 21:53:52 +08:00
if [ -n " $extra_drivers " ] ; then
mkdir_clear $tmp /scsi
2025-10-31 07:50:40 +08:00
download_and_extract_deb udeb scsi-modules-$kver -di $tmp /scsi
2026-04-28 07:33:07 +08:00
cp_debian_kali_driver $tmp /scsi " $extra_drivers "
2024-04-03 21:53:52 +08:00
fi
fi
2024-05-22 21:37:30 +08:00
# amd64)
2026-09-06 11:53:44 +08:00
# level1=737 # MT=754108, qemu: -m 780
# level2=424 # MT=433340, qemu: -m 460
# min=316 # MT=322748, qemu: -m 350
2024-05-22 21:37:30 +08:00
2024-03-31 01:07:33 +08:00
# 将 use_level 2 9 修改为 use_level 1
2024-04-03 21:53:52 +08:00
# x86 use_level 2 会出现 No root file system is defined.
# arm 即使 use_level 1 也会出现 No root file system is defined.
2024-03-31 01:07:33 +08:00
sed -i 's/use_level=[29]/use_level=1/' lib/debian-installer-startup.d/S15lowmem
2024-03-16 23:06:38 +08:00
# hack 3
# 修改 trans.sh
# 1. 直接调用 create_ifupdown_config
2025-02-06 18:47:21 +08:00
# shellcheck disable=SC2154
2024-10-18 23:58:03 +08:00
insert_into_file $initrd_dir /trans.sh after '^: main' <<EOF
2024-05-03 21:37:07 +08:00
distro=$nextos_distro
2025-02-06 18:47:21 +08:00
releasever=$nextos_releasever
2024-03-16 23:06:38 +08:00
create_ifupdown_config /etc/network/interfaces
exit
EOF
# 2. 删除 debian busybox 无法识别的语法
# 3. 删除 apk 语句
# 4. debian 11/12 initrd 无法识别 > >
2026-05-29 07:25:16 +08:00
# 5. debian 11/12 initrd 无法识别 < < ,注意可能分两行写
2024-03-16 23:06:38 +08:00
# 6. debian 11 initrd 无法识别 set -E
# 7. debian 11 initrd 无法识别 trap ERR
2024-10-13 22:58:12 +08:00
# 8. debian 9 initrd 无法识别 ${string//find/replace}
2025-03-03 00:13:28 +08:00
# 9. debian 12 initrd 无法识别 . <(
2024-03-16 23:06:38 +08:00
# 删除或注释,可能会导致空方法而报错,因此改为替换成'\n: #'
replace = '\n: #'
2024-10-13 22:58:12 +08:00
sed -Ei \
-e "s/> >/ $replace /" \
-e "s/< </ $replace /" \
2025-03-03 00:13:28 +08:00
-e "s/\. <\(/ $replace /" \
2026-05-29 07:25:16 +08:00
-e "s/< \\\\/ $replace /" \
-e "s/ <\(/ $replace /" \
2024-10-13 22:58:12 +08:00
-e "s/^[[:space:]]*apk[[:space:]]/ $replace /" \
-e "s/^[[:space:]]*trap[[:space:]]/ $replace /" \
-e "s/\\ $\{.*\/\/.*\/.*\}/ $replace /" \
-e "/^[[:space:]]*set[[:space:]]/s/E//" \
$initrd_dir /trans.sh
2026-05-29 07:25:16 +08:00
2026-05-29 15:35:48 +08:00
# ubuntu 22.04 不支持这种语法,bash -n 会报错
# 因此不验证 trans.sh 的语法
# a=$(
# case 1 in
# 1)
# case 1 in
# 1) echo ;;
# 2) echo ;;
# esac
# ;;
# 2)
# case 1 in
# 1) echo ;;
# 2) echo ;;
# esac
# ;;
# esac
# )
2026-05-29 07:25:16 +08:00
# 测试魔改后的 trans.sh 有没有语法问题
2026-05-29 15:35:48 +08:00
# bash -n $initrd_dir/trans.sh
2024-03-16 23:06:38 +08:00
}
2024-06-10 22:17:49 +08:00
get_disk_drivers() {
2024-06-12 22:40:22 +08:00
get_drivers "/sys/block/ $1 "
2024-06-10 22:17:49 +08:00
}
get_net_drivers() {
2024-06-12 22:40:22 +08:00
get_drivers "/sys/class/net/ $1 "
2024-06-10 22:17:49 +08:00
}
# 不用在 windows 判断是哪种硬盘/网络驱动,因为 256M 运行 windows 只可能是 xp,而脚本本来就不支持 xp
# 而且安装过程也有二次判断
2026-04-28 07:33:07 +08:00
# trans.sh 有同名方法
2024-06-10 22:17:49 +08:00
get_drivers() {
2024-05-22 21:37:30 +08:00
# 有以下结果组合出现
# sd_mod
# virtio_blk
# virtio_scsi
# virtio_pci
# pcieport
# xen_blkfront
# ahci
# nvme
2026-04-28 07:33:07 +08:00
# pci_hyperv
2024-05-29 21:30:24 +08:00
# mptspi
# mptsas
# vmw_pvscsi
2024-05-22 21:37:30 +08:00
(
2024-06-10 22:17:49 +08:00
cd " $( readlink -f $1 ) "
2024-05-22 21:37:30 +08:00
while ! [ " $( pwd ) " = / ] ; do
if [ -d driver ] ; then
if [ -d driver/module ] ; then
# 显示全名,例如 xen_blkfront sd_mod
# 但 ahci 没有这个文件,所以 else 不能省略
basename " $( readlink -f driver/module) "
else
# 不显示全名,例如 vbd sd
basename " $( readlink -f driver) "
fi
fi
cd ..
done
)
}
2024-06-12 22:40:22 +08:00
exit_if_cant_use_cloud_kernel() {
find_main_disk
collect_netconf
# shellcheck disable=SC2154
if ! can_use_cloud_kernel " $xda " $ipv4_ethx $ipv6_ethx ; then
error_and_exit "Can't use cloud kernel. And not enough RAM to run normal kernel."
fi
}
2024-05-22 21:37:30 +08:00
can_use_cloud_kernel() {
2024-06-12 22:40:22 +08:00
# initrd 下也要使用,不要用 <<<
2024-05-22 21:37:30 +08:00
# 有些虚拟机用了 ahci,但云内核没有 ahci 驱动
2024-06-12 22:40:22 +08:00
cloud_eth_modules = 'ena|gve|mana|virtio_net|xen_netfront|hv_netvsc|vmxnet3|mlx4_en|mlx4_core|mlx5_core|ixgbevf'
cloud_blk_modules = 'ata_generic|ata_piix|pata_legacy|nvme|virtio_blk|virtio_scsi|xen_blkfront|xen_scsifront|hv_storvsc|vmw_pvscsi'
# disk
drivers = " $( get_disk_drivers $1 ) "
shift
for driver in $drivers ; do
echo "using disk driver: $driver "
done
echo " $drivers " | grep -Ewq " $cloud_blk_modules " || return 1
# net
# v4 v6 eth 相同,只检查一次
if [ " $1 " = " $2 " ] ; then
shift
fi
while [ $# -gt 0 ] ; do
drivers = " $( get_net_drivers $1 ) "
shift
for driver in $drivers ; do
echo "using net driver: $driver "
done
echo " $drivers " | grep -Ewq " $cloud_eth_modules " || return 1
done
2024-05-22 21:37:30 +08:00
}
2024-06-10 22:17:49 +08:00
2024-05-22 21:37:30 +08:00
create_can_use_cloud_kernel_sh() {
cat <<EOF >$1
2024-06-10 22:17:49 +08:00
$(get_function get_drivers)
$(get_function get_net_drivers)
$(get_function get_disk_drivers)
$(get_function can_use_cloud_kernel)
2024-06-12 22:40:22 +08:00
can_use_cloud_kernel "\$@"
2024-05-22 21:37:30 +08:00
EOF
}
2024-06-12 22:40:22 +08:00
get_ip_conf_cmd() {
collect_netconf >& 2
is_in_china && is_in_china = true || is_in_china = false
2024-11-23 23:50:09 +08:00
sh = /initrd-network.sh
2024-06-23 01:25:23 +08:00
if is_found_ipv4_netconf && is_found_ipv6_netconf && [ " $ipv4_mac " = " $ipv6_mac " ] ; then
2026-03-08 22:17:59 +08:00
echo "' $sh ' ' $ipv4_mac ' ' $ipv4_addr ' ' $ipv4_gateway ' ' $ipv6_addr ' ' $ipv6_gateway ' ' $is_in_china ' ' $ipv6_extra_addrs '"
2024-06-12 22:40:22 +08:00
else
2024-06-23 01:25:23 +08:00
if is_found_ipv4_netconf; then
2026-03-08 22:17:59 +08:00
echo "' $sh ' ' $ipv4_mac ' ' $ipv4_addr ' ' $ipv4_gateway ' '' '' ' $is_in_china ' ''"
2024-06-12 22:40:22 +08:00
fi
2024-06-23 01:25:23 +08:00
if is_found_ipv6_netconf; then
2026-03-08 22:17:59 +08:00
echo "' $sh ' ' $ipv6_mac ' '' '' ' $ipv6_addr ' ' $ipv6_gateway ' ' $is_in_china ' ' $ipv6_extra_addrs '"
2024-06-12 22:40:22 +08:00
fi
fi
}
2026-09-06 11:31:38 +08:00
is_need_web_viewer() {
! { [ " $distro " = netboot.xyz ] || is_alpine_live; }
}
2024-03-16 23:06:38 +08:00
mod_initrd_alpine() {
2024-05-22 21:53:32 +08:00
# hack 1 v3.19 和之前的 virt 内核需添加 ipv6 模块
2024-07-14 22:11:53 +08:00
if virt_dir = $( ls -d $initrd_dir /lib/modules/*-virt 2>/dev/null) ; then
2023-07-16 15:32:45 +08:00
ipv6_dir = $virt_dir /kernel/net/ipv6
2024-07-23 23:00:18 +08:00
if ! [ -f $ipv6_dir /ipv6.ko ] && ! grep -q ipv6 $initrd_dir /lib/modules/*/modules.builtin; then
2024-03-31 00:47:13 +08:00
mkdir -p $ipv6_dir
2024-04-03 21:51:18 +08:00
modloop_file = $tmp /modloop_file
modloop_dir = $tmp /modloop_dir
2024-03-31 00:47:13 +08:00
curl -Lo $modloop_file $nextos_modloop
if is_in_windows; then
# cygwin 没有 unsquashfs
7z e $modloop_file ipv6.ko -r -y -o$ipv6_dir
else
install_pkg unsquashfs
mkdir_clear $modloop_dir
unsquashfs -f -d $modloop_dir $modloop_file 'modules/*/kernel/net/ipv6/ipv6.ko'
find $modloop_dir -name ipv6.ko -exec cp {} $ipv6_dir / \;
fi
2023-07-16 15:32:45 +08:00
fi
fi
2023-08-21 23:50:20 +08:00
2024-11-23 23:50:13 +08:00
# hack 下载 dhcpcd
# shellcheck disable=SC2154
download_and_extract_apk " $nextos_releasever " dhcpcd " $initrd_dir "
sed -i -e '/^slaac private/s/^/#/' -e '/^#slaac hwaddr/s/^#//' $initrd_dir /etc/dhcpcd.conf
2024-06-12 22:40:22 +08:00
# hack 2 /usr/share/udhcpc/default.script
2023-08-23 14:17:45 +08:00
# 脚本被调用的顺序
# udhcpc: deconfig
# udhcpc: bound
# udhcpc6: deconfig
# udhcpc6: bound
2026-03-10 00:09:05 +08:00
# shellcheck disable=SC2329
2023-10-22 19:07:12 +08:00
udhcpc() {
if [ " $1 " = deconfig ] ; then
2023-08-23 14:17:45 +08:00
return
fi
2023-10-22 19:07:12 +08:00
if [ " $1 " = bound ] && [ -n " $ipv6 " ] ; then
2024-03-16 23:06:38 +08:00
# shellcheck disable=SC2154
2023-10-22 19:07:12 +08:00
ip -6 addr add " $ipv6 " dev " $interface "
ip link set dev " $interface " up
2023-08-23 14:17:45 +08:00
return
fi
2023-10-22 19:07:12 +08:00
}
get_function_content udhcpc |
insert_into_file usr/share/udhcpc/default.script after 'deconfig\|renew\|bound'
2023-08-23 14:17:45 +08:00
2023-10-27 11:46:06 +08:00
# 允许设置 ipv4 onlink 网关
sed -Ei 's,(0\.0\.0\.0\/0),"\1 onlink",' usr/share/udhcpc/default.script
2024-06-12 22:40:22 +08:00
# hack 3 网络配置
# alpine 根据 MAC_ADDRESS 判断是否有网络
# https://github.com/alpinelinux/mkinitfs/blob/c4c0115f9aa5aa8884c923dc795b2638711bdf5c/initramfs-init.in#L914
insert_into_file init after 'configure_ip\(\)' <<EOF
depmod
[ -d /sys/module/ipv6 ] || modprobe ipv6
$(get_ip_conf_cmd)
MAC_ADDRESS=1
return
2023-08-21 23:50:20 +08:00
EOF
2024-06-12 22:40:22 +08:00
# grep -E -A5 'configure_ip\(\)' init
# hack 4 运行 trans.start
2023-07-05 22:00:29 +08:00
# 1. alpine arm initramfs 时间问题 要添加 --no-check-certificate
2023-08-21 23:50:20 +08:00
# 2. aws t4g arm 如果没设置console=ttyx,在initramfs里面wget https会出现bad header错误,chroot后正常
2023-07-05 22:00:29 +08:00
# Connecting to raw.githubusercontent.com (185.199.108.133:443)
# 60C0BB2FFAFF0000:error:0A00009C:SSL routines:ssl3_get_record:http request:ssl/record/ssl3_record.c:345:
# ssl_client: SSL_connect
# wget: bad header line: �
2025-04-02 19:26:07 +08:00
insert_into_file init before '^exec switch_root' <<EOF
2024-10-13 22:58:12 +08:00
# trans
2023-08-21 23:50:20 +08:00
# echo "wget --no-check-certificate -O- $confhome/trans.sh | /bin/ash" >\$sysroot/etc/local.d/trans.start
2023-07-05 22:00:29 +08:00
# wget --no-check-certificate -O \$sysroot/etc/local.d/trans.start $confhome/trans.sh
2024-03-16 23:06:38 +08:00
cp /trans.sh \$sysroot/etc/local.d/trans.start
2023-05-13 00:14:46 +08:00
chmod a+x \$sysroot/etc/local.d/trans.start
ln -s /etc/init.d/local \$sysroot/etc/runlevels/default/
2024-10-13 22:58:12 +08:00
2025-02-15 00:25:38 +08:00
# 配置 + 自定义驱动
for dir in /configs /custom_drivers; do
if [ -d \$dir ]; then
cp -r \$dir \$sysroot/
rm -rf \$dir
fi
done
2023-05-13 00:14:46 +08:00
EOF
2024-05-22 21:37:30 +08:00
# 判断云镜像 debain 能否用云内核
if is_distro_like_debian; then
create_can_use_cloud_kernel_sh can_use_cloud_kernel.sh
insert_into_file init before '^exec (/bin/busybox )?switch_root' <<EOF
cp /can_use_cloud_kernel.sh \$sysroot/
chmod a+x \$sysroot/can_use_cloud_kernel.sh
EOF
fi
2024-03-16 23:06:38 +08:00
}
mod_initrd() {
info "mod $nextos_distro initrd"
install_pkg gzip cpio
# 解压
# 先删除临时文件,避免之前运行中断有残留文件
2024-07-14 22:11:53 +08:00
initrd_dir = $tmp /initrd
mkdir_clear $initrd_dir
cd $initrd_dir
2024-03-17 01:13:44 +08:00
# cygwin 下处理 debian initrd 时
# 解压/重新打包/删除 initrd 的 /dev/console /dev/null 都会报错
# cpio: dev/console: Cannot utime: Invalid argument
# cpio: ./dev/console: Cannot stat: Bad address
# 用 windows 文件管理器可删除
# 但同样运行 zcat /reinstall-initrd | cpio -idm
# 打开 C:\cygwin\Cygwin.bat ,运行报错
# 打开桌面的 Cygwin 图标,运行就没问题
# shellcheck disable=SC2046
# nonmatching 是精确匹配路径
zcat /reinstall-initrd | cpio -idm \
$( is_in_windows && echo --nonmatching 'dev/console' --nonmatching 'dev/null' )
2024-03-16 23:06:38 +08:00
2024-07-14 22:11:53 +08:00
curl -Lo $initrd_dir /trans.sh $confhome /trans.sh
2024-10-13 22:58:12 +08:00
if ! grep -iq " $SCRIPT_VERSION " $initrd_dir /trans.sh; then
2024-09-06 23:00:28 +08:00
error_and_exit "
This script is outdated, please download reinstall.sh again.
脚本有更新,请重新下载 reinstall.sh"
fi
2024-10-13 22:58:12 +08:00
2024-11-23 23:50:09 +08:00
curl -Lo $initrd_dir /initrd-network.sh $confhome /initrd-network.sh
chmod a+x $initrd_dir /trans.sh $initrd_dir /initrd-network.sh
2024-03-16 23:06:38 +08:00
2024-10-13 22:58:12 +08:00
# 保存配置
mkdir -p $initrd_dir /configs
2025-03-06 20:45:30 +08:00
if [ -n " $ssh_keys " ] ; then
cat <<< " $ssh_keys " >$initrd_dir /configs/ssh_keys
else
save_password $initrd_dir /configs
fi
2025-04-28 23:18:27 +08:00
if [ -n " $frpc_config " ] ; then
2026-03-08 23:50:50 +08:00
cat " $frpc_config " >$initrd_dir /configs/frpc.conf
2025-04-28 23:18:27 +08:00
fi
2024-10-13 22:58:12 +08:00
2026-02-12 22:14:04 +08:00
# 收集 cloud-data 打包进 initrd
if [ -n " $cloud_data " ] ; then
mkdir -p $initrd_dir /configs/cloud-data
if [ -d " $cloud_data " ] ; then
# 本地目录:直接复制
cp " $cloud_data " /* $initrd_dir /configs/cloud-data/
else
# URL:在 host 下载
for f in user-data meta-data network-config; do
curl -fsSL " $cloud_data / $f " -o " $initrd_dir /configs/cloud-data/ $f " 2>/dev/null || true
done
fi
# 校验:至少要有 user-data
[ -f $initrd_dir /configs/cloud-data/user-data ] || error_and_exit "--cloud-data must contain user-data"
2026-02-26 20:01:07 +08:00
cloud_data_files = $( ls $initrd_dir /configs/cloud-data/ | tr '\n' ' ' )
2026-02-12 22:14:04 +08:00
fi
2024-05-08 22:17:33 +08:00
if is_distro_like_debian $nextos_distro ; then
2024-05-22 21:37:30 +08:00
mod_initrd_debian_kali
2024-05-03 21:37:07 +08:00
else
mod_initrd_$nextos_distro
fi
2023-08-21 23:50:20 +08:00
2025-02-15 00:25:38 +08:00
# 添加自定义 windows 驱动
2025-03-02 21:57:06 +08:00
if [ " $distro " = windows ] && [ -n " $custom_infs " ] ; then
# shellcheck disable=SC1090
. <( curl -L $confhome /windows-driver-utils.sh)
echo " $custom_infs " | while read -r inf; do
parse_inf_and_cp_driever " $inf " " $initrd_dir /custom_drivers" " $basearch_alt " true
done
2025-02-15 00:25:38 +08:00
fi
2024-05-22 21:37:30 +08:00
# alpine live 不精简 initrd
# 因为不知道用户想干什么,可能会用到精简的文件
2024-03-31 01:07:33 +08:00
if is_virt && ! is_alpine_live; then
2024-05-22 21:37:30 +08:00
remove_useless_initrd_files
2024-03-31 01:07:33 +08:00
fi
2025-10-31 07:50:45 +08:00
if [ " $hold " = 0 ] ; then
info 'hold 0'
2026-04-28 07:33:09 +08:00
echo "Edit $tmp if needed."
2025-10-31 07:50:45 +08:00
read -r -p 'Press Enter to continue...'
fi
2023-05-13 00:14:46 +08:00
# 重建
# 注意要用 cpio -H newc 不要用 cpio -c ,不同版本的 -c 作用不一样,很坑
# -c Use the old portable (ASCII) archive format
# -c Identical to "-H newc", use the new (SVR4)
# portable format.If you wish the old portable
# (ASCII) archive format, use "-H odc" instead.
2026-03-15 23:43:52 +08:00
find . | cpio --quiet -o -H newc -R 0:0 | gzip -1 >/reinstall-initrd
2023-12-02 22:46:16 +08:00
cd - >/dev/null
2023-06-18 21:27:22 +08:00
}
2023-05-13 00:14:46 +08:00
2024-05-22 21:37:30 +08:00
remove_useless_initrd_files() {
2025-02-15 00:25:38 +08:00
info "slim initrd"
2024-05-22 21:37:30 +08:00
# 显示精简前的大小
du -sh .
# 删除 initrd 里面没用的文件/驱动
rm -rf bin/brltty
rm -rf etc/brltty
rm -rf sbin/wpa_supplicant
rm -rf usr/lib/libasound.so.*
rm -rf usr/share/alsa
(
cd lib/modules/*/kernel/drivers/net/ethernet/
for item in *; do
case " $item " in
2025-02-10 21:30:28 +08:00
# 甲骨文 arm 用自定义镜像支持设为 mlx5 vf 网卡,且不是 azure 那样显示两个网卡
2025-10-31 07:50:45 +08:00
# https://debian.pkgs.org/13/debian-main-amd64/linux-image-6.12.43+deb13-cloud-amd64_6.12.43-1_amd64.deb.html
amazon | google | mellanox | realtek | pensando) ;;
2025-02-10 21:30:28 +08:00
intel)
(
cd " $item "
for sub_item in *; do
case " $sub_item " in
# 有 e100.ko e1000文件夹 e1000e文件夹
2025-10-31 07:50:45 +08:00
e100* | lib* | *vf | idpf) ;;
2025-02-10 21:30:28 +08:00
*) rm -rf $sub_item ;;
esac
done
)
;;
2024-05-22 21:37:30 +08:00
*) rm -rf $item ;;
esac
done
)
(
cd lib/modules/*/kernel
for item in \
net/mac80211 \
net/wireless \
net/bluetooth \
drivers/hid \
drivers/mtd \
drivers/usb \
drivers/ssb \
drivers/mfd \
drivers/bcma \
drivers/pcmcia \
drivers/parport \
drivers/platform \
drivers/staging \
drivers/net/usb \
drivers/net/bonding \
drivers/net/wireless \
drivers/input/rmi4 \
drivers/input/keyboard \
drivers/input/touchscreen \
drivers/bus/mhi \
drivers/char/pcmcia \
drivers/misc/cardreader; do
rm -rf $item
done
)
# 显示精简后的大小
du -sh .
}
2025-03-06 20:45:30 +08:00
get_unix_path() {
if is_in_windows; then
# 输入的路径是 / 开头也没问题
cygpath -u " $1 "
else
printf '%s' " $1 "
fi
}
2026-03-10 00:09:05 +08:00
init_basearch() {
# 设置 basearch
if is_in_windows; then
# x86-based PC
# x64-based PC
# ARM-based PC
# ARM64-based PC
# 三种方法都不需要管理员运行
if false; then
# 如果机器没有 wmic 则需要下载 wmic.ps1,但此时未判断国内外,还是用国外源
basearch = $( wmic ComputerSystem get SystemType | grep '=' | cut -d= -f2 | cut -d- -f1)
elif true; then
basearch = $( reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE |
grep . | tail -1 | awk '{print $NF}' )
else
# 也可以用
basearch = $( cmd /c "if defined PROCESSOR_ARCHITEW6432 (echo %PROCESSOR_ARCHITEW6432%) else (echo %PROCESSOR_ARCHITECTURE%)" )
fi
else
# archlinux 云镜像没有 arch 命令
# https://en.wikipedia.org/wiki/Uname
basearch = $( uname -m)
fi
# 统一架构名称,并强制 64 位
case " $( echo $basearch | to_lower) " in
i?86 | x64 | x86* | amd64)
basearch = x86_64
basearch_alt = amd64
;;
arm* | aarch64)
basearch = aarch64
basearch_alt = arm64
;;
*) error_and_exit "Unsupported arch: $basearch " ;;
esac
}
init_confhome() {
# 设置 confhome
# 未测试
if false && [[ " $confhome " = http*://raw.githubusercontent.com/* ]] ; then
repo = $( echo $confhome | cut -d/ -f4,5)
branch = $( echo $confhome | cut -d/ -f6)
# 避免脚本更新时,文件不同步造成错误
if [ -z " $commit " ] ; then
commit = $( curl -L https://api.github.com/repos/$repo /git/refs/heads/$branch |
grep '"sha"' | grep -Eo '[0-9a-f]{40}' )
fi
# shellcheck disable=SC2001
confhome = $( echo " $confhome " | sed "s/main $/ $commit /" )
fi
# 设置国内代理
# 要在使用 wmic 前设置,否则国内机器会从国外源下载 wmic.ps1
# gitee 不支持ipv6
# jsdelivr 有12小时缓存
# https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L31
if is_in_china; then
if [ -n " $confhome_cn " ] ; then
confhome = $confhome_cn
elif [ -n " $github_proxy " ] && [[ " $confhome " = http*://raw.githubusercontent.com/* ]] ; then
confhome = ${ confhome /http: \/\/ /https: \/\/ }
confhome = ${ confhome /https: \/\/ raw .githubusercontent.com/ $github_proxy }
fi
fi
}
remove_exist_reinstall_efi_dir() {
info "remove exist reinstall efi dir"
local dir = '' dirs = ''
if is_in_windows; then
dirs = $( get_efi_dir_in_windows)
else
dirs = $( get_maybe_efi_dirs_in_linux)
fi
# 后期可能会将 reinstall-vmlinuz 和 reinstall-initrd 放到 efi 分区下
# 因此也删除它们
for dir in $dirs ; do
rm -f " $dir /reinstall-vmlinuz"
rm -f " $dir /reinstall-initrd"
done
find $dirs -type f \
\( -ipath '*/EFI/reinstall/grubx64.efi' \
-o -ipath '*/EFI/reinstall/grubaa64.efi' \
-o -ipath '*/EFI/reinstall/netboot.xyz.efi' \
-o -ipath '*/EFI/reinstall/netboot.xyz-arm64.efi' \) |
while IFS = read -r efi_file; do
reinstall_dir = $( dirname " $efi_file " )
echo "removing $reinstall_dir "
rm -rf " $reinstall_dir "
done
}
# linux windows
# bios /boot/grub*/custom.cfg /cygdrive/c/grub/grub.cfg
# efi efi分区的/EFI/reinstall/grub.cfg /cygdrive/c/grub.cfg
# efi文件夹 efi分区的/EFI/reinstall/ /cygdrive/a/EFI/reinstall/
init_bootloader_facts() {
if is_in_windows; then
# windows
if is_efi; then
_grub_cfg = /cygdrive/$c /grub.cfg
else
_grub_cfg = /cygdrive/$c /grub/grub.cfg
fi
target_cfg = $_grub_cfg
else
# linux
if is_efi; then
efi_dir = $( get_maybe_efi_dirs_in_linux | head -1)
_grub_cfg = $efi_dir /EFI/reinstall/grub.cfg
target_cfg = $_grub_cfg
else
if is_mbr_using_grub; then
if is_have_cmd update-grub; then
# alpine debian ubuntu
_grub_cfg = $( grep -o '[^ ]*grub.cfg' " $( get_cmd_path update-grub) " | head -1)
else
# 找出主配置文件(含有menuentry|blscfg)
# 有没有可能在 efi 目录?
_grub_cfg = $( find_grub_extlinux_cfg '/boot/grub*' grub.cfg 'menuentry|blscfg' )
fi
target_cfg = $( dirname $_grub_cfg ) /custom.cfg
if is_have_cmd grub2-mkconfig; then
grub = grub2
elif is_have_cmd grub-mkconfig; then
grub = grub
else
error_and_exit "grub not found"
fi
else
# extlinux
_extlinux_cfg = $( find_grub_extlinux_cfg /boot extlinux.conf LINUX)
target_cfg = $_extlinux_cfg
fi
fi
fi
}
# 重新生成 grub.cfg
# 因为有些机子例如hython debian的grub.cfg少了40_custom 41_custom 部分
recreate_grub_or_extlinux_cfg() {
# 没用到原机的 grub 和 extlinux
# 因此不需要重新生成 grub.cfg 或 extlinux.conf
if is_efi || is_in_windows; then
return
fi
if is_mbr_using_grub; then
info "recreate grub.cfg"
# nixos 手动执行 grub-mkconfig -o /boot/grub/grub.cfg 会丢失系统启动条目
# 正确的方法是修改 configuration.nix 的 boot.loader.grub.extraEntries
# 但是修改 configuration.nix 不是很好,因此改成修改 grub.cfg
if [ -x /nix/var/nix/profiles/system/bin/switch-to-configuration ] ; then
# 生成 grub.cfg
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
# 手动启用 41_custom
nixos_grub_home = " $( dirname " $( readlink -f " $( get_cmd_path grub-mkconfig) " ) " ) /.."
2026-05-20 21:15:43 +08:00
$nixos_grub_home /etc/grub.d/41_custom >>" $( dirname " $target_cfg " ) /grub.cfg"
2026-03-10 00:09:05 +08:00
elif is_have_cmd update-grub; then
update-grub
else
$grub -mkconfig -o $target_cfg
fi
elif is_have_cmd update-extlinux; then
# alpine 才有 update-extlinux
info "recreate extlinux.conf"
update-extlinux
else
error_and_exit "unsupported bootloader."
fi
}
# 删除之前的 reinstall 启动项
remove_exist_reinstall() {
info "remove exist reinstall"
rm -f /reinstall-vmlinuz /reinstall-initrd
rm -f /boot/reinstall-vmlinuz /boot/reinstall-initrd
if is_in_windows; then
rm -f /cygdrive/$c /reinstall-vmlinuz /cygdrive/$c /reinstall-initrd
fi
# 使用外部 grub 时,删除外部 grub.cfg
if ! is_use_local_grub_extlinux; then
rm -f " $target_cfg "
fi
if is_in_windows; then
if is_efi; then
# efi
remove_exist_reinstall_efi_dir
bcdedit /set '{fwbootmgr}' bootsequence '{bootmgr}'
2026-03-15 23:32:51 +08:00
bcdedit /enum bootmgr | grep -a -B3 'reinstall' | awk '{print $2}' | grep '{.*}' |
2026-03-10 00:09:05 +08:00
xargs -I {} cmd /c bcdedit /delete {}
else
# bios
id = '{1c41f649-1637-52f1-aea8-f96bfebeecc8}'
2026-03-15 23:32:51 +08:00
if bcdedit /enum all | grep -a " $id " ; then
2026-03-10 00:09:05 +08:00
bcdedit /delete " $id "
fi
fi
else
if is_efi; then
# efi
# 题外话
# 1. 如果用本机的 grub,则 custom.cfg 可能在 efi 分区,也可能在 /boot 分区
# 2. 有可能没有 /boot 文件夹
# 如果 nixos 的 efi 挂载到 /efi,则不会生成 /boot 文件夹
# 3. find 不存在的路径会报错
remove_exist_reinstall_efi_dir
install_pkg efibootmgr
efibootmgr | grep -q 'BootNext:' && efibootmgr --quiet --delete-bootnext
efibootmgr | grep_efi_entry | grep 'reinstall' | grep_efi_index |
xargs -I {} efibootmgr --quiet --bootnum {} --delete-bootnum
else
# bios
# 删除 reinstall 条目
if [ -f " $target_cfg " ] ; then
sed -i "/^ $BOOT_ENTEY_START_MARK /,/^ $BOOT_ENTEY_END_MARK /d" " $target_cfg "
fi
# 清除 next entry
if is_use_local_grub; then
$grub -editenv - unset next_entry
elif is_use_local_extlinux; then
extlinux --clear-once " $( dirname " $target_cfg " ) "
fi
# 重新创建 grub.cfg / extlinux.conf
recreate_grub_or_extlinux_cfg
fi
fi
}
reset_and_exit() {
from_ctrl_c = ${ 1 :- false }
# info
if $from_ctrl_c ; then
info "Caught Ctrl+C, reseting..."
fi
# 清除
remove_exist_reinstall
rm -rf " $tmp "
echo "reset done."
# 退出
if $from_ctrl_c ; then
exit 1
else
exit 0
fi
}
2023-11-09 22:27:07 +08:00
# 脚本入口
2025-10-31 07:50:45 +08:00
2026-03-10 00:09:05 +08:00
# windows 环境下的额外初始化
2024-03-15 00:01:34 +08:00
if is_in_windows; then
# win系统盘
c = $( echo $SYSTEMDRIVE | cut -c1)
# 64位系统 + 32位cmd/cygwin,需要添加 PATH,否则找不到64位系统程序,例如bcdedit
sysnative = $( cygpath -u $WINDIR \\ Sysnative)
if [ -d $sysnative ] ; then
PATH = $PATH :$sysnative
fi
# 更改 windows 命令输出语言为英文
2024-03-22 20:05:53 +08:00
# chcp 会清屏
mode.com con cp select = 437 >/dev/null
2024-11-23 23:50:11 +08:00
# 为 windows 程序输出删除 cr
for exe in $WINDOWS_EXES ; do
2025-03-10 22:23:37 +08:00
# 如果我们覆写了 wmic(),则先将 wmic() 重命名为 _wmic()
if get_function $exe >/dev/null 2>& 1; then
eval "_ $( get_function $exe ) "
fi
# 使用以下方法重新生成 wmic()
# 调用链:wmic() -> run_with_del_cr(wmic) -> _wmic() -> command wmic
2024-11-23 23:50:11 +08:00
eval " $exe (){ $( get_function_content run_with_del_cr_template | sed "s/\$exe/ $exe /g" ) }"
done
2024-03-15 00:01:34 +08:00
fi
2023-11-09 22:27:07 +08:00
# 检查 root
2024-01-27 23:17:50 +08:00
if is_in_windows; then
2024-03-15 00:01:34 +08:00
# 64位系统 + 32位cmd/cygwin,运行 openfiles 报错:目标系统必须运行 32 位的操作系统
if ! fltmc >/dev/null 2>& 1; then
2024-01-27 23:17:50 +08:00
error_and_exit "Please run as administrator."
fi
else
2023-11-09 22:27:07 +08:00
if [ " $EUID " -ne 0 ] ; then
2024-01-27 23:17:50 +08:00
error_and_exit "Please run as root."
2023-05-17 22:54:26 +08:00
fi
2023-05-13 00:14:46 +08:00
fi
2026-03-10 00:09:05 +08:00
# 不支持 Live OS 下运行
if mount | grep -q 'tmpfs on / type tmpfs' ; then
error_and_exit "Can't run this script in Live OS."
fi
# 不支持容器虚拟化
if is_in_container; then
error_and_exit "Not Supported OS in Container.\nPlease use https://github.com/LloydAsp/OsMutation"
fi
# 不支持安全启动
if is_secure_boot_enabled; then
error_and_exit "Please disable secure boot first."
fi
# 整理参数
2024-10-12 23:07:01 +08:00
long_opts =
2026-09-10 01:06:52 +08:00
for o in ci installer debug minimal no-cloud-kernel no-auto-drivers allow-ping force-cn help \
2025-03-02 21:57:06 +08:00
add-driver: \
2024-10-13 22:58:12 +08:00
hold: sleep: \
2024-10-12 23:07:01 +08:00
iso: \
image-name: \
boot-wim: \
img: \
2026-02-12 22:14:04 +08:00
cloud-data: \
2024-10-12 23:07:01 +08:00
lang: \
2026-05-05 22:55:00 +08:00
user: username: \
2024-10-13 22:58:12 +08:00
passwd: password: \
2024-10-12 23:07:01 +08:00
ssh-port: \
2025-03-06 20:45:30 +08:00
ssh-key: public-key: \
2024-10-12 23:07:01 +08:00
rdp-port: \
2024-10-14 22:41:01 +08:00
web-port: http-port: \
2024-10-12 23:07:01 +08:00
allow-ping: \
commit: \
2026-03-08 23:50:50 +08:00
frpc-conf: frpc-config: \
2026-03-10 00:09:05 +08:00
target-disk: \
2025-10-11 00:01:18 +08:00
force-boot-mode: \
2024-10-12 23:07:01 +08:00
force-old-windows-setup:; do
[ -n " $long_opts " ] && long_opts += ,
long_opts += $o
done
2026-03-10 00:09:05 +08:00
# 使用 getopt 解析参数
if ! ORIGINAL_OPTS = $( getopt -n $0 -o "h,x" --long " $long_opts " -- " $@ " ) ; then
exit 1
2023-11-09 22:27:07 +08:00
fi
2026-03-10 00:09:05 +08:00
# 第一遍扫描,验证要安装的系统和版本
eval set -- " $ORIGINAL_OPTS "
while true; do
case " $1 " in
-x | --debug)
set -x
shift
;;
--)
shift
verify_os_name " $@ "
break
;;
*)
shift
;;
esac
done
# 初始化重要变量
# wmic 随时会用到
# wmic 需要下载 wmic.ps1,需要从 confhome 得到,要先将 confhome 改成国内
# 而 wmic.ps1 又放在 $tmp 目录,因此要先创建临时目录
2026-03-08 23:50:50 +08:00
# 处理 --frpc-config 时会下载文件,因此在处理参数前就创建临时目录
2025-10-31 07:50:43 +08:00
mkdir_clear " $tmp "
2026-03-10 00:09:05 +08:00
init_basearch
init_confhome
init_bootloader_facts
2025-10-31 07:50:43 +08:00
2026-03-10 00:09:05 +08:00
if [ " $distro " = reset ] ; then
reset_and_exit
fi
# 安装必备组件
install_pkg curl grep
# 第二遍扫描,处理参数
eval set -- " $ORIGINAL_OPTS "
2023-11-09 22:27:07 +08:00
# shellcheck disable=SC2034
while true; do
case " $1 " in
2026-03-10 00:09:05 +08:00
-x | --debug)
# 第一遍扫描已处理
shift
;;
2025-03-06 20:45:31 +08:00
-h | --help)
usage_and_exit
;;
2024-05-22 22:32:43 +08:00
--commit)
commit = $2
shift 2
;;
2024-04-05 22:45:59 +08:00
--ci)
2023-11-09 22:27:07 +08:00
cloud_image = 1
2024-09-03 19:01:32 +08:00
unset installer
shift
;;
--installer)
installer = 1
unset cloud_image
2023-11-09 22:27:07 +08:00
shift
;;
2024-08-11 23:05:53 +08:00
--minimal)
minimal = 1
shift
;;
2026-09-07 19:21:11 +08:00
--no-cloud-kernel)
no_cloud_kernel = 1
shift
;;
2026-09-10 01:06:52 +08:00
--no-auto-drivers)
no_auto_drivers = 1
shift
;;
2024-10-12 23:07:01 +08:00
--allow-ping)
allow_ping = 1
shift
;;
2025-02-06 18:47:17 +08:00
--force-cn)
# 仅为了方便测试
force_cn = 1
shift
;;
2023-11-09 22:27:07 +08:00
--hold | --sleep)
2025-10-31 07:50:45 +08:00
if ! { [ " $2 " = 0 ] || [ " $2 " = 1 ] || [ " $2 " = 2 ] ; } ; then
2024-10-12 23:07:01 +08:00
error_and_exit "Invalid $1 value: $2 "
2024-04-05 22:45:59 +08:00
fi
2024-10-12 23:07:01 +08:00
hold = $2
2025-04-28 23:18:27 +08:00
shift 2
;;
2026-03-08 23:50:50 +08:00
--frpc-conf | --frpc-config)
2025-04-28 23:18:27 +08:00
[ -n " $2 " ] || error_and_exit "Need value for $1 "
2025-10-31 07:50:43 +08:00
case " $( to_lower <<< " $2 " ) " in
http://* | https://*)
frpc_config_url = $2
2026-03-08 23:50:50 +08:00
frpc_config = $tmp /frpc.conf
# 用 file 识别文件类型?
2025-10-31 07:50:43 +08:00
if ! curl -L " $frpc_config_url " -o " $frpc_config " ; then
error_and_exit "Can't get frpc config from $frpc_config_url "
fi
;;
*)
# windows 路径转换
if ! { frpc_config = $( get_unix_path " $2 " ) && [ -f " $frpc_config " ] ; } ; then
error_and_exit "File not exists: $2 "
fi
;;
esac
2025-04-28 23:18:27 +08:00
# 转为绝对路径
frpc_config = $( readlink -f " $frpc_config " )
2023-11-09 22:27:07 +08:00
shift 2
;;
2025-10-11 00:01:18 +08:00
--force-boot-mode)
2024-10-12 23:07:01 +08:00
if ! { [ " $2 " = bios ] || [ " $2 " = efi ] ; } ; then
error_and_exit "Invalid $1 value: $2 "
2024-07-09 23:37:51 +08:00
fi
2025-10-11 00:01:18 +08:00
force_boot_mode = $2
2024-10-12 23:07:01 +08:00
shift 2
;;
2026-05-05 22:55:00 +08:00
--user | --username)
2026-05-29 07:25:16 +08:00
[ -n " $2 " ] || error_and_exit "Need value for $1 "
2026-05-05 22:55:00 +08:00
username = " $( printf "%s" " $2 " | trim) "
assert_username_valid
shift 2
;;
2024-10-13 22:58:12 +08:00
--passwd | --password)
[ -n " $2 " ] || error_and_exit "Need value for $1 "
password = $2
2025-03-06 20:45:30 +08:00
shift 2
;;
--ssh-key | --public-key)
ssh_key_error_and_exit() {
error " $1 "
cat <<EOF
Available options:
--ssh-key "ssh-rsa ..."
--ssh-key "ssh-ed25519 ..."
--ssh-key "ecdsa-sha2-nistp256/384/521 ..."
--ssh-key github:your_username
--ssh-key gitlab:your_username
2025-03-07 22:22:11 +08:00
--ssh-key http://path/to/public_key
--ssh-key https://path/to/public_key
2025-03-06 20:45:30 +08:00
--ssh-key /path/to/public_key
2025-03-07 22:22:11 +08:00
--ssh-key C:\path\to\public_key
2025-03-06 20:45:30 +08:00
EOF
exit 1
}
# https://manpages.debian.org/testing/openssh-server/authorized_keys.5.en.html#AUTHORIZED_KEYS_FILE_FORMAT
is_valid_ssh_key() {
2025-11-10 19:37:18 +08:00
grep -qE '^(ecdsa-sha2-nistp(256|384|521)|ssh-(ed25519|rsa)) ' <<< " $1 "
2025-03-06 20:45:30 +08:00
}
[ -n " $2 " ] || ssh_key_error_and_exit "Need value for $1 "
case " $( to_lower <<< " $2 " ) " in
2026-09-06 11:53:44 +08:00
gh:* | github:* | gl:* | gitlab:* | http://* | https://*)
2025-03-06 20:45:30 +08:00
if [[ " $( to_lower <<< " $2 " ) " = http* ]] ; then
key_url = $2
else
IFS = : read -r site user <<< " $2 "
2026-09-06 11:53:44 +08:00
case " $( to_lower <<< " $site " ) " in
gh | github) site = github ;;
gl | gitlab) site = gitlab ;;
*) ;;
esac
2025-03-06 20:45:30 +08:00
[ -n " $user " ] || ssh_key_error_and_exit "Need a username for $site "
key_url = "https:// $site .com/ $user .keys"
fi
if ! ssh_key = $( curl -L " $key_url " ) ; then
error_and_exit "Can't get ssh key from $key_url "
fi
;;
*)
# 检测值是否为 ssh key
if is_valid_ssh_key " $2 " ; then
ssh_key = $2
else
# 视为路径
# windows 路径转换
if ! { ssh_key_file = $( get_unix_path " $2 " ) && [ -f " $ssh_key_file " ] ; } ; then
ssh_key_error_and_exit "SSH Key/File/Url \" $2 \" is invalid."
fi
ssh_key = $( <" $ssh_key_file " )
fi
;;
esac
# 检查 key 格式
if ! is_valid_ssh_key " $ssh_key " ; then
ssh_key_error_and_exit "SSH Key/File/Url \" $2 \" is invalid."
fi
# 保存 key
# 不用处理注释,可以支持写入 authorized_keys
# 安装 nixos 时再处理注释/空行,转成数组,再添加到 nix 配置文件中
if [ -n " $ssh_keys " ] ; then
ssh_keys += $'\n'
fi
ssh_keys += $ssh_key
2024-10-13 22:58:12 +08:00
shift 2
;;
2024-10-12 23:07:01 +08:00
--ssh-port)
is_port_valid $2 || error_and_exit "Invalid $1 value: $2 "
ssh_port = $2
shift 2
;;
--rdp-port)
is_port_valid $2 || error_and_exit "Invalid $1 value: $2 "
rdp_port = $2
shift 2
;;
2024-10-14 22:41:01 +08:00
--web-port | --http-port)
2024-10-12 23:07:01 +08:00
is_port_valid $2 || error_and_exit "Invalid $1 value: $2 "
web_port = $2
shift 2
;;
2025-03-02 21:57:06 +08:00
--add-driver)
2025-03-06 20:45:30 +08:00
[ -n " $2 " ] || error_and_exit "Need value for $1 "
# windows 路径转换
inf_or_dir = $( get_unix_path " $2 " )
2025-02-15 00:25:38 +08:00
2025-03-02 21:57:06 +08:00
# alpine busybox 不支持 readlink -m
# readlink -m /asfsafasfsaf/fasf
# 因此需要先判断路径是否存在
if ! [ -d " $inf_or_dir " ] &&
! { [ -f " $inf_or_dir " ] && [[ " $inf_or_dir " = ~ \. [ iI][ nN][ fF] $ ]] ; } ; then
2026-03-10 00:09:05 +08:00
error_and_exit "Not a inf or dir: $2 "
2025-02-15 00:25:38 +08:00
fi
2025-03-02 21:57:06 +08:00
# 转为绝对路径
inf_or_dir = $( readlink -f " $inf_or_dir " )
info "finding inf in $inf_or_dir "
# find /tmp -type f -iname '*.inf' 只要 /tmp 存在就会返回 0
if infs = $( find " $inf_or_dir " -type f -iname '*.inf' | grep .) ; then
while IFS = read -r inf; do
# 防止重复添加
if ! grep -Fqx " $inf " <<< " $custom_infs " ; then
echo "inf found: $inf "
# 一行一个 inf
if [ -n " $custom_infs " ] ; then
custom_infs += $'\n'
fi
custom_infs += $inf
fi
done <<< " $infs "
else
error_and_exit "Can't find inf files in $2 "
fi
2025-02-15 00:25:38 +08:00
shift 2
;;
2024-10-12 23:07:01 +08:00
--force-old-windows-setup)
force_old_windows_setup = $2
2024-07-09 23:37:51 +08:00
shift 2
;;
2026-03-10 00:09:05 +08:00
--target-disk)
xda = ${ 2 ##*/dev/ }
if ! [ -b "/dev/ $xda " ] ; then
error_and_exit "Can't not find Disk $2 ."
fi
shift 2
;;
2023-11-09 22:27:07 +08:00
--img)
img = $2
shift 2
;;
2026-02-12 22:14:04 +08:00
--cloud-data)
cloud_data = $2
shift 2
;;
2023-11-09 22:27:07 +08:00
--iso)
iso = $2
shift 2
;;
2024-10-13 22:47:52 +08:00
--boot-wim)
boot_wim = $2
shift 2
;;
2023-11-09 22:27:07 +08:00
--image-name)
2024-04-05 22:45:59 +08:00
image_name = $( echo " $2 " | to_lower)
shift 2
;;
--lang)
2026-08-06 22:13:07 +08:00
if ! is_valid_lang_chars " $2 " ; then
error_and_exit "Invalid $1 value: $2 "
fi
2024-04-05 22:45:59 +08:00
lang = $( echo " $2 " | to_lower)
2023-11-09 22:27:07 +08:00
shift 2
;;
--)
shift
break
;;
*)
echo "Unexpected option: $1 ."
usage_and_exit
;;
esac
done
# 检查必须的参数
verify_os_args
2026-05-05 22:55:00 +08:00
# 用户名
2026-05-29 07:25:16 +08:00
if ! is_netboot_xyz && [ -z " $username " ] ; then
2026-05-05 22:55:00 +08:00
prompt_username
fi
2024-10-13 22:58:12 +08:00
# 密码
2025-03-06 20:45:30 +08:00
if ! is_netboot_xyz && [ -z " $ssh_keys " ] && [ -z " $password " ] ; then
2025-03-02 21:57:06 +08:00
if is_use_dd; then
2025-10-31 07:50:41 +08:00
show_dd_password_tips
2024-10-13 22:58:12 +08:00
fi
prompt_password
fi
2024-03-28 00:16:05 +08:00
# 强制忽略/强制添加 --ci 参数
2024-05-22 21:37:30 +08:00
# debian 不强制忽略 ci 留作测试
2024-03-28 00:16:05 +08:00
case " $distro " in
2025-02-24 22:05:26 +08:00
dd | windows | netboot.xyz | kali | alpine | arch | gentoo | aosc | nixos | fnos)
2024-03-28 00:16:05 +08:00
if is_use_cloud_image; then
echo "ignored --ci"
2024-09-03 19:01:32 +08:00
unset cloud_image
2024-03-28 00:16:05 +08:00
fi
;;
2024-09-03 19:01:32 +08:00
oracle | opensuse | anolis | opencloudos | openeuler)
2024-03-28 00:16:05 +08:00
cloud_image = 1
;;
2024-11-26 22:09:05 +08:00
redhat | centos | almalinux | rocky | fedora | ubuntu)
2024-09-03 19:01:32 +08:00
if is_force_use_installer; then
unset cloud_image
else
cloud_image = 1
fi
;;
2024-03-28 00:16:05 +08:00
esac
2023-11-09 22:27:07 +08:00
2025-05-05 21:27:07 +08:00
# 检查内存
# 会用到 wmic,因此要在设置国内 confhome 后使用
check_ram
2023-12-02 22:46:16 +08:00
# 以下目标系统不需要两步安装
# alpine
2023-11-09 22:27:07 +08:00
# debian
# el7 x86_64 >=1g
# el7 aarch64 >=1.5g
# el8/9/fedora 任何架构 >=2g
if is_netboot_xyz ||
{ ! is_use_cloud_image && {
2024-05-03 21:37:07 +08:00
[ " $distro " = "alpine" ] || is_distro_like_debian ||
{ is_distro_like_redhat && [ $releasever -eq 7 ] && [ $ram_size -ge 1024 ] && [ $basearch = "x86_64" ] ; } ||
{ is_distro_like_redhat && [ $releasever -eq 7 ] && [ $ram_size -ge 1536 ] && [ $basearch = "aarch64" ] ; } ||
{ is_distro_like_redhat && [ $releasever -ge 8 ] && [ $ram_size -ge 2048 ] ; }
2023-11-09 22:27:07 +08:00
} ; } ; then
setos nextos $distro $releasever
else
2024-12-06 22:50:07 +08:00
# alpine 作为中间系统时,使用最新版
2025-04-12 15:44:20 +08:00
alpine_ver_for_trans = $( get_latest_distro_releasever alpine)
2023-11-09 22:27:07 +08:00
setos finalos $distro $releasever
2023-12-02 22:46:16 +08:00
setos nextos alpine $alpine_ver_for_trans
2023-11-09 22:27:07 +08:00
fi
2023-12-16 16:12:13 +08:00
# 有的机器开启了 kexec,例如腾讯云轻量 debian,要禁用
2024-12-06 22:50:05 +08:00
if [ -f /etc/default/kexec ] ; then
2023-12-16 16:12:13 +08:00
sed -i 's/LOAD_KEXEC=true/LOAD_KEXEC=false/' /etc/default/kexec
2023-11-09 22:27:07 +08:00
fi
2026-03-10 00:09:05 +08:00
# 一切就绪,正式下载内核和添加引导项
# 先删除之前的启动项,再设置 trap
# 暂时不用 trap,因为 bat 下无效
remove_exist_reinstall
# trap 'reset_and_exit true' SIGINT
2023-11-09 22:27:07 +08:00
# 下载 netboot.xyz / 内核
# shellcheck disable=SC2154
if is_netboot_xyz; then
if is_efi; then
if is_in_windows; then
2026-03-10 00:09:05 +08:00
add_efi_entry_in_windows $nextos_efi
2023-11-09 22:27:07 +08:00
else
2026-03-10 00:09:05 +08:00
add_efi_entry_in_linux $nextos_efi
2023-11-09 22:27:07 +08:00
fi
else
curl -Lo /reinstall-vmlinuz $nextos_vmlinuz
fi
else
# 下载 nextos 内核
info download vmlnuz and initrd
curl -Lo /reinstall-vmlinuz $nextos_vmlinuz
curl -Lo /reinstall-initrd $nextos_initrd
2024-05-03 21:37:07 +08:00
if is_use_firmware; then
curl -Lo /reinstall-firmware $nextos_firmware
fi
2023-12-16 16:12:13 +08:00
fi
2023-11-09 22:27:07 +08:00
2024-05-03 21:37:07 +08:00
# 修改 alpine debian kali initrd
2024-05-08 22:17:33 +08:00
if [ " $nextos_distro " = alpine ] || is_distro_like_debian " $nextos_distro " ; then
2024-03-16 23:06:38 +08:00
mod_initrd
2023-12-16 16:12:13 +08:00
fi
2026-09-06 11:31:38 +08:00
# web 路径
if is_need_web_viewer; then
web_path = "/ $( tr -dc "A-Za-z0-9" </dev/urandom | head -c8) "
fi
2023-12-16 16:12:13 +08:00
# 将内核/netboot.xyz.lkrn 放到正确的位置
2026-03-10 00:09:05 +08:00
if false && is_need_boot_vmlinuz; then
2023-12-16 16:12:13 +08:00
if is_in_windows; then
2024-01-27 23:17:50 +08:00
cp -f /reinstall-vmlinuz /cygdrive/$c /
is_have_initrd && cp -f /reinstall-initrd /cygdrive/$c /
2023-12-16 16:12:13 +08:00
else
if is_os_in_btrfs && is_os_in_subvol; then
cp_to_btrfs_root /reinstall-vmlinuz
is_have_initrd && cp_to_btrfs_root /reinstall-initrd
fi
2023-12-02 22:46:16 +08:00
fi
2023-11-09 22:27:07 +08:00
fi
2026-03-10 00:09:05 +08:00
# 需要使用 vmlinuz/initrd 引导的情况
if is_need_boot_vmlinuz; then
2023-12-16 16:12:13 +08:00
# win 使用外部 grub
if is_in_windows; then
install_grub_win
else
2024-12-06 22:50:05 +08:00
# linux efi 使用外部 grub,因为
# 1. 原系统 grub 可能没有去除 aarch64 内核 magic number 校验
# 2. 原系统可能不是用 grub
2023-12-16 16:12:13 +08:00
if is_efi; then
install_grub_linux_efi
fi
fi
2023-11-09 22:27:07 +08:00
2024-03-01 22:06:33 +08:00
# 找到 /reinstall-vmlinuz /reinstall-initrd 的绝对路径
if is_in_windows; then
# dir=/cygwin/
dir = $( cygpath -m / | cut -d: -f2-) /
else
2024-09-09 22:56:13 +08:00
# extlinux + 单独的 boot 分区
# 把内核文件放在 extlinux.conf 所在的目录
if is_use_local_extlinux && is_boot_in_separate_partition; then
dir =
2024-03-01 22:06:33 +08:00
else
2024-09-09 22:56:13 +08:00
# 获取当前系统根目录在 btrfs 中的绝对路径
if is_os_in_btrfs; then
# btrfs subvolume show /
# 输出可能是 / 或 root 或 @/.snapshots/1/snapshot
dir = $( btrfs subvolume show / | head -1)
if ! [ " $dir " = / ] ; then
dir = "/ $dir /"
fi
else
dir = /
fi
2024-03-01 22:06:33 +08:00
fi
fi
vmlinuz = ${ dir } reinstall-vmlinuz
initrd = ${ dir } reinstall-initrd
2024-05-03 21:37:07 +08:00
firmware = ${ dir } reinstall-firmware
2024-03-01 22:06:33 +08:00
2024-09-09 22:56:13 +08:00
# 设置 linux initrd 命令
if is_use_local_extlinux; then
linux_cmd = LINUX
initrd_cmd = INITRD
2023-11-09 22:27:07 +08:00
else
2024-09-09 22:56:13 +08:00
if is_netboot_xyz; then
linux_cmd = linux16
initrd_cmd = initrd16
else
2026-03-10 00:09:05 +08:00
linux_cmd = linux
initrd_cmd = initrd
2024-05-03 21:37:07 +08:00
fi
2023-11-09 22:27:07 +08:00
fi
2023-12-02 22:46:16 +08:00
2024-09-09 22:56:13 +08:00
# 设置 cmdlind initrds
if ! is_netboot_xyz; then
find_main_disk
build_cmdline
2024-06-05 22:23:18 +08:00
2024-09-09 22:56:13 +08:00
initrds = " $initrd "
if is_use_firmware; then
initrds += " $firmware "
2024-06-05 22:23:18 +08:00
fi
2024-09-09 22:56:13 +08:00
fi
2024-06-05 22:23:18 +08:00
2024-09-09 22:56:13 +08:00
if is_use_local_extlinux; then
info extlinux
2026-03-10 00:09:05 +08:00
echo " $target_cfg "
extlinux_dir = " $( dirname " $target_cfg " ) "
2024-06-05 22:23:18 +08:00
2024-09-09 22:56:13 +08:00
# 不起作用
# 好像跟 extlinux --once 有冲突
2026-03-10 00:09:05 +08:00
sed -i "/^MENU HIDDEN/d" " $target_cfg "
sed -i "/^TIMEOUT /d" " $target_cfg "
2024-06-05 22:23:18 +08:00
2026-03-10 00:09:05 +08:00
del_empty_lines <<EOF | tee -a "$target_cfg"
$BOOT_ENTEY_START_MARK
2024-09-09 22:56:13 +08:00
TIMEOUT 5
LABEL reinstall
MENU LABEL $(get_entry_name)
$linux_cmd $vmlinuz
$([ -n "$initrds" ] && echo "$initrd_cmd $initrds")
$([ -n "$cmdline" ] && echo "APPEND $cmdline")
2026-03-10 00:09:05 +08:00
$BOOT_ENTEY_END_MARK
2024-09-09 22:56:13 +08:00
EOF
# 设置重启引导项
extlinux --once= reinstall $extlinux_dir
# 复制文件到 extlinux 工作目录
if is_boot_in_separate_partition; then
info "copying files to $extlinux_dir "
is_have_initrd && cp -f /reinstall-initrd $extlinux_dir
is_use_firmware && cp -f /reinstall-firmware $extlinux_dir
# 放最后,防止前两条返回非 0 而报错
cp -f /reinstall-vmlinuz $extlinux_dir
fi
else
# cloudcone 从光驱的 grub 启动,再加载硬盘的 grub.cfg
# menuentry "Grub 2" --id grub2 {
# set root=(hd0,msdos1)
# configfile /boot/grub2/grub.cfg
# }
# 加载后 $prefix 依然是光驱的 (hd96)/boot/grub
# 导致找不到 $prefix 目录的 grubenv,因此读取不到 next_entry
# 以下方法为 cloudcone 重新加载 grubenv
# 需查找 2*2 个文件夹
# 分区:系统 / boot
# 文件夹:grub / grub2
# shellcheck disable=SC2121,SC2154
# cloudcone debian 能用但 ubuntu 模板用不了
# ubuntu 模板甚至没显示 reinstall menuentry
load_grubenv_if_not_loaded() {
if ! [ -s $prefix /grubenv ] ; then
for dir in /boot/grub /boot/grub2 /grub /grub2; do
set grubenv = "( $root ) $dir /grubenv"
if [ -s $grubenv ] ; then
load_env --file $grubenv
if [ " ${ next_entry } " ] ; then
set default = " ${ next_entry } "
set next_entry =
save_env --file $grubenv next_entry
else
set default = "0"
fi
return
fi
done
fi
}
# 生成 grub 配置
# 实测 centos 7 lvm 要手动加载 lvm 模块
info grub
echo $target_cfg
2026-03-10 00:09:05 +08:00
echo '### BEGIN reinstall.sh ###' >$target_cfg
get_function_content load_grubenv_if_not_loaded >>$target_cfg
2024-09-09 22:56:13 +08:00
# 原系统为 openeuler 云镜像,需要添加 --unrestricted,否则要输入密码
2025-10-31 07:50:40 +08:00
del_empty_lines <<EOF | del_comment_lines | tee -a $target_cfg
2024-06-05 22:23:18 +08:00
set timeout_style=menu
2023-06-18 21:27:22 +08:00
set timeout=5
2024-07-09 23:37:51 +08:00
menuentry "$(get_entry_name)" --unrestricted {
2024-03-01 22:06:33 +08:00
$(! is_in_windows && echo 'insmod lvm')
$(is_os_in_btrfs && echo 'set btrfs_relative_path=n')
2025-10-31 07:50:40 +08:00
# fedora efi 没有 load_video
2023-11-09 22:27:07 +08:00
insmod all_video
2025-10-31 07:50:40 +08:00
# set gfxmode=800x600
# set gfxpayload=keep
# terminal_output gfxterm 在 vultr 上会花屏
# terminal_output console
2024-03-01 22:06:33 +08:00
search --no-floppy --file --set=root $vmlinuz
2024-09-09 22:56:13 +08:00
$linux_cmd $vmlinuz $cmdline
$([ -n "$initrds" ] && echo "$initrd_cmd $initrds")
2023-05-03 22:22:21 +08:00
}
2022-09-25 21:57:47 +08:00
EOF
2026-03-10 00:09:05 +08:00
echo '### END reinstall.sh ###' >>$target_cfg
2022-09-25 21:57:47 +08:00
2024-09-09 22:56:13 +08:00
# 设置重启引导项
if is_use_local_grub; then
$grub -reboot " $( get_entry_name) "
fi
2023-11-09 22:27:07 +08:00
fi
2023-06-18 21:27:22 +08:00
fi
2023-12-04 22:19:59 +08:00
info 'info'
echo " $distro $releasever "
2024-06-06 20:48:44 +08:00
2026-05-29 07:25:16 +08:00
ssh_port = ${ ssh_port :- 22 }
rdp_port = ${ rdp_port :- 3389 }
web_port = ${ web_port :- 80 }
2025-03-06 20:45:30 +08:00
2026-05-29 07:25:16 +08:00
if [ " $distro " = netboot.xyz ] ; then
:
elif [ " $distro " = alpine ] && [ " $hold " = 1 ] ; then
info "Alpine Live OS"
2024-06-06 20:48:44 +08:00
echo "Username: $username "
2025-03-06 20:45:30 +08:00
if [ -n " $ssh_keys " ] ; then
echo "Public Key: $ssh_keys "
else
echo "Password: $password "
fi
2026-05-29 07:25:16 +08:00
echo "SSH Port: $ssh_port "
2024-06-06 20:48:44 +08:00
2026-05-29 07:25:16 +08:00
elif [ " $distro " = fnos ] ; then
info "While Install (View Logs)"
echo "Username: $username "
if [ -n " $ssh_keys " ] ; then
echo "Public Key: $ssh_keys "
else
echo "Password: $password "
fi
echo "SSH Port: $ssh_port "
2026-09-06 11:31:38 +08:00
echo "WEB: $( get_http_log_url) "
2026-05-29 07:25:16 +08:00
info "After Install"
echo "安装后不会开启 SSH 服务。"
echo "你需要尽快到 http://IP:5666 配置账号密码。"
echo
echo "SSH Service is disabled after installation."
echo "You need to config the username and password on http://IP:5666 as soon as possible."
elif [ " $distro " = windows ] ; then
info "While Install (View Logs)"
echo "Username: $username "
echo "Password: $password "
echo "SSH Port: $ssh_port "
2026-09-06 11:31:38 +08:00
echo "WEB: $( get_http_log_url) "
2026-05-29 07:25:16 +08:00
info "After Install"
if is_administrator_username " $username " ; then
echo "Username: $username (Depends on Windows iso's language)"
else
echo "Username: $username "
fi
echo "Password: $password "
echo "RDP Port: $rdp_port "
elif [ " $distro " = dd ] ; then
info "While Install (View Logs)"
echo "Username: $username "
if [ -n " $ssh_keys " ] ; then
echo "Public Key: $ssh_keys "
else
echo "Password: $password "
fi
echo "SSH Port: $ssh_port "
2026-09-06 11:31:38 +08:00
echo "WEB: $( get_http_log_url) "
2026-05-29 07:25:16 +08:00
info "After Install"
2026-02-26 20:01:07 +08:00
if [ -n " $cloud_data " ] ; then
echo "Cloud Data: $cloud_data "
echo "Cloud Data Files: $cloud_data_files "
2026-05-29 07:25:16 +08:00
else
echo "Username: [Depends on image]"
echo "Public Key: [Depends on image]"
echo "Password: [Depends on image]"
echo "SSH Port: [Depends on image]"
2026-02-26 20:01:07 +08:00
fi
2026-05-29 07:25:16 +08:00
2023-08-04 00:07:45 +08:00
else
2026-05-29 07:25:16 +08:00
# 普通 linux
info "While Install (View Logs)"
echo "Username: $username "
if [ -n " $ssh_keys " ] ; then
echo "Public Key: $ssh_keys "
else
echo "Password: $password "
fi
echo "SSH Port: $ssh_port "
2026-09-06 11:31:38 +08:00
echo "WEB: $( get_http_log_url) "
2026-05-29 07:25:16 +08:00
info "After Install"
echo "Username: $username "
if [ -n " $ssh_keys " ] ; then
echo "Public Key: $ssh_keys "
else
echo "Password: $password "
fi
echo "SSH Port: $ssh_port "
2023-12-04 22:19:59 +08:00
fi
2024-03-31 00:47:13 +08:00
if is_in_windows; then
2026-05-29 07:25:16 +08:00
echo
2024-06-12 22:40:22 +08:00
echo 'You can run this command to reboot:'
2024-03-31 00:47:13 +08:00
echo 'shutdown /r /t 0'
fi
2026-03-10 00:09:05 +08:00
echo
2026-05-29 07:25:16 +08:00
if [ " $distro " = netboot.xyz ] ; then
echo '重启后进入 netboot.xyz。'
echo "或者现在运行 \" $reinstall_____ reset\" 以清除该引导项。"
echo
echo 'Reboot to start netboot.xyz.'
echo "Or run \" $reinstall_____ reset\" now to clear this boot entry."
echo
elif [ " $distro " = alpine ] && [ " $hold " = 1 ] ; then
echo '重启后进入 Alpine Live OS。'
echo "或者现在运行 \" $reinstall_____ reset\" 以清除该引导项。"
echo
echo 'Reboot to start Alpine Live OS.'
echo "Or run \" $reinstall_____ reset\" now to clear this boot entry."
echo
else
warn false '警告:重装会清除主硬盘的所有数据,包括所有分区!'
echo '重启后开始重装。'
echo "或者现在运行 \" $reinstall_____ reset\" 以取消重装。"
echo
warn false 'Warning: Reinstalling will erase all data on the main disk, including all partitions!'
echo 'Reboot to start the reinstallation.'
echo "Or run \" $reinstall_____ reset\" now to cancel the reinstallation."
fi
2026-03-10 00:09:05 +08:00
echo