mirror of
https://github.com/bin456789/reinstall.git
synced 2025-03-18 21:29:31 +08:00
core: 重构代码
This commit is contained in:
parent
e6654dab05
commit
ba456c65eb
156
trans.sh
156
trans.sh
@ -20,6 +20,11 @@ error() {
|
|||||||
wall "Error: $*"
|
wall "Error: $*"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
error_and_exit() {
|
||||||
|
error "$@"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
add_community_repo() {
|
add_community_repo() {
|
||||||
if ! grep -x 'http.*/community' /etc/apk/repositories; then
|
if ! grep -x 'http.*/community' /etc/apk/repositories; then
|
||||||
alpine_ver=$(cut -d. -f1,2 </etc/alpine-release)
|
alpine_ver=$(cut -d. -f1,2 </etc/alpine-release)
|
||||||
@ -106,6 +111,16 @@ EOF
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setup_nginx_if_enough_ram() {
|
||||||
|
total_ram=$(free -m | awk '{print $2}' | sed -n '2p')
|
||||||
|
# 避免后面没内存安装程序,谨慎起见,512内存才安装
|
||||||
|
if [ $total_ram -gt 400 ]; then
|
||||||
|
# lighttpd 虽然运行占用内存少,但安装占用空间大
|
||||||
|
# setup_lighttpd
|
||||||
|
setup_nginx
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
setup_lighttpd() {
|
setup_lighttpd() {
|
||||||
apk add lighttpd
|
apk add lighttpd
|
||||||
ln -sf /reinstall.html /var/www/localhost/htdocs/index.html
|
ln -sf /reinstall.html /var/www/localhost/htdocs/index.html
|
||||||
@ -164,42 +179,7 @@ clear_previous() {
|
|||||||
} 2>/dev/null || true
|
} 2>/dev/null || true
|
||||||
}
|
}
|
||||||
|
|
||||||
clear_previous
|
install_alpine() {
|
||||||
setup_tty_and_log
|
|
||||||
extract_env_from_cmdline
|
|
||||||
|
|
||||||
# 安装 nginx,目标系统是 alpine 除外
|
|
||||||
# shellcheck disable=SC2154
|
|
||||||
if [ "$distro" != "alpine" ]; then
|
|
||||||
total_ram=$(free -m | awk '{print $2}' | sed -n '2p')
|
|
||||||
# 避免后面没内存安装程序,谨慎起见,512内存才安装
|
|
||||||
if [ $total_ram -gt 400 ]; then
|
|
||||||
# lighttpd 虽然运行占用内存少,但安装占用空间大
|
|
||||||
# setup_lighttpd
|
|
||||||
setup_nginx
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 找到主硬盘
|
|
||||||
# alpine 不自带lsblk,liveos安装的软件也会被带到新系统,所以不用lsblk
|
|
||||||
# xda=$(lsblk -dn -o NAME | grep -E 'nvme0n1|.da')
|
|
||||||
# shellcheck disable=SC2010
|
|
||||||
xda=$(ls /dev/ | grep -Ex 'sda|hda|xda|vda|xvda|nvme0n1')
|
|
||||||
|
|
||||||
# arm要手动从硬件同步时间,避免访问https出错
|
|
||||||
hwclock -s
|
|
||||||
|
|
||||||
# 安装并打开 ssh
|
|
||||||
echo root:123@@@ | chpasswd
|
|
||||||
printf '\nyes' | setup-sshd
|
|
||||||
|
|
||||||
# shellcheck disable=SC2154
|
|
||||||
if [ "$sleep" = 1 ]; then
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# shellcheck disable=SC2154
|
|
||||||
if [ "$distro" = "alpine" ]; then
|
|
||||||
# 还原改动,不然本脚本会被复制到新系统
|
# 还原改动,不然本脚本会被复制到新系统
|
||||||
rm -f /etc/local.d/trans.start
|
rm -f /etc/local.d/trans.start
|
||||||
rm -f /etc/runlevels/default/local
|
rm -f /etc/runlevels/default/local
|
||||||
@ -247,9 +227,10 @@ if [ "$distro" = "alpine" ]; then
|
|||||||
# alpine默认使用 syslinux (efi 环境除外),这里强制使用 grub,方便用脚本再次重装
|
# alpine默认使用 syslinux (efi 环境除外),这里强制使用 grub,方便用脚本再次重装
|
||||||
export BOOTLOADER="grub"
|
export BOOTLOADER="grub"
|
||||||
printf 'y' | setup-disk -m sys $kernel_opt -s 0 /dev/$xda
|
printf 'y' | setup-disk -m sys $kernel_opt -s 0 /dev/$xda
|
||||||
exec reboot
|
}
|
||||||
|
|
||||||
elif [ "$distro" = "dd" ]; then
|
# shellcheck disable=SC2154
|
||||||
|
install_dd() {
|
||||||
case "$img_type" in
|
case "$img_type" in
|
||||||
gzip) prog=gzip ;;
|
gzip) prog=gzip ;;
|
||||||
xz) prog=xz ;;
|
xz) prog=xz ;;
|
||||||
@ -263,23 +244,20 @@ elif [ "$distro" = "dd" ]; then
|
|||||||
curl -L $img | $prog -dc >/dev/$xda
|
curl -L $img | $prog -dc >/dev/$xda
|
||||||
sync
|
sync
|
||||||
else
|
else
|
||||||
echo 'Not supported'
|
error_and_exit 'Not supported'
|
||||||
sleep 1m
|
|
||||||
fi
|
|
||||||
if [ "$sleep" = 2 ]; then
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
exec reboot
|
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
is_xda_gt_2t() {
|
||||||
|
disk_size=$(blockdev --getsize64 /dev/$xda)
|
||||||
|
disk_2t=$((2 * 1024 * 1024 * 1024 * 1024))
|
||||||
|
[ "$disk_size" -gt "$disk_2t" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
create_part() {
|
||||||
# 目标系统非 alpine 和 dd
|
# 目标系统非 alpine 和 dd
|
||||||
# 脚本开始
|
# 脚本开始
|
||||||
add_community_repo
|
apk add util-linux aria2 grub udev hdparm e2fsprogs curl parted
|
||||||
if ! apk add util-linux aria2 grub udev hdparm e2fsprogs curl parted; then
|
|
||||||
echo 'Unable to install package!'
|
|
||||||
sleep 1m
|
|
||||||
exec reboot
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 打开dev才能刷新分区名
|
# 打开dev才能刷新分区名
|
||||||
rc-service udev start
|
rc-service udev start
|
||||||
@ -291,9 +269,6 @@ false && vgchange -an
|
|||||||
# 移除 lsblk 显示的分区
|
# 移除 lsblk 显示的分区
|
||||||
partx -d /dev/$xda
|
partx -d /dev/$xda
|
||||||
|
|
||||||
disk_size=$(blockdev --getsize64 /dev/$xda)
|
|
||||||
disk_2t=$((2 * 1024 * 1024 * 1024 * 1024))
|
|
||||||
|
|
||||||
# xda*1 星号用于 nvme0n1p1 的字母 p
|
# xda*1 星号用于 nvme0n1p1 的字母 p
|
||||||
if [ "$distro" = windows ]; then
|
if [ "$distro" = windows ]; then
|
||||||
apk add ntfs-3g-progs virt-what wimlib rsync dos2unix
|
apk add ntfs-3g-progs virt-what wimlib rsync dos2unix
|
||||||
@ -351,8 +326,8 @@ else
|
|||||||
mkfs.fat -F 32 -n efi /dev/$xda*1 #1 efi
|
mkfs.fat -F 32 -n efi /dev/$xda*1 #1 efi
|
||||||
mkfs.ext4 -F -L os /dev/$xda*2 #2 os
|
mkfs.ext4 -F -L os /dev/$xda*2 #2 os
|
||||||
mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
|
mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
|
||||||
elif [ "$disk_size" -ge "$disk_2t" ]; then
|
elif is_xda_gt_2t; then
|
||||||
# bios 2t
|
# bios > 2t
|
||||||
parted /dev/$xda -s -- \
|
parted /dev/$xda -s -- \
|
||||||
mklabel gpt \
|
mklabel gpt \
|
||||||
mkpart '" "' ext4 1MiB 2MiB \
|
mkpart '" "' ext4 1MiB 2MiB \
|
||||||
@ -377,8 +352,9 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
update_part /dev/$xda
|
update_part /dev/$xda
|
||||||
|
}
|
||||||
|
|
||||||
if is_use_cloud_image; then
|
install_cloud_image() {
|
||||||
apk add qemu-img lsblk
|
apk add qemu-img lsblk
|
||||||
|
|
||||||
mkdir -p /installer
|
mkdir -p /installer
|
||||||
@ -434,8 +410,9 @@ if is_use_cloud_image; then
|
|||||||
fi
|
fi
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
download $confhome/nocloud.yaml /os$subvol/etc/cloud/cloud.cfg.d/99_nocloud.cfg
|
download $confhome/nocloud.yaml /os$subvol/etc/cloud/cloud.cfg.d/99_nocloud.cfg
|
||||||
exit
|
}
|
||||||
else
|
|
||||||
|
mount_part() {
|
||||||
# 挂载主分区
|
# 挂载主分区
|
||||||
mkdir -p /os
|
mkdir -p /os
|
||||||
mount /dev/disk/by-label/os /os
|
mount /dev/disk/by-label/os /os
|
||||||
@ -450,10 +427,10 @@ else
|
|||||||
mount_args="-t ntfs3"
|
mount_args="-t ntfs3"
|
||||||
fi
|
fi
|
||||||
mount $mount_args /dev/disk/by-label/installer /os/installer
|
mount $mount_args /dev/disk/by-label/installer /os/installer
|
||||||
fi
|
}
|
||||||
|
|
||||||
|
install_windows() {
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
if [ "$distro" = "windows" ]; then
|
|
||||||
download $iso /os/windows.iso
|
download $iso /os/windows.iso
|
||||||
mkdir -p /iso
|
mkdir -p /iso
|
||||||
mount /os/windows.iso /iso
|
mount /os/windows.iso /iso
|
||||||
@ -723,13 +700,9 @@ EOF
|
|||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
if [ "$sleep" = 2 ]; then
|
}
|
||||||
cd /
|
|
||||||
sleep infinity
|
|
||||||
fi
|
|
||||||
exec reboot
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
install_redhat_ubuntu() {
|
||||||
# 安装 grub2
|
# 安装 grub2
|
||||||
if is_efi; then
|
if is_efi; then
|
||||||
# 注意低版本的grub无法启动f38 arm的内核
|
# 注意低版本的grub无法启动f38 arm的内核
|
||||||
@ -790,4 +763,53 @@ else
|
|||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 脚本入口
|
||||||
|
# arm要手动从硬件同步时间,避免访问https出错
|
||||||
|
hwclock -s
|
||||||
|
|
||||||
|
# 设置密码,安装并打开 ssh
|
||||||
|
echo root:123@@@ | chpasswd
|
||||||
|
printf '\nyes' | setup-sshd
|
||||||
|
|
||||||
|
extract_env_from_cmdline
|
||||||
|
# shellcheck disable=SC2154
|
||||||
|
if [ "$sleep" = 1 ]; then
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
setup_tty_and_log
|
||||||
|
clear_previous
|
||||||
|
|
||||||
|
# 找到主硬盘
|
||||||
|
# shellcheck disable=SC2010
|
||||||
|
xda=$(ls /dev/ | grep -Ex 'sda|hda|xda|vda|xvda|nvme0n1')
|
||||||
|
|
||||||
|
# shellcheck disable=SC2154
|
||||||
|
if [ "$distro" != "alpine" ]; then
|
||||||
|
setup_nginx_if_enough_ram
|
||||||
|
add_community_repo
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$distro" = "alpine" ]; then
|
||||||
|
install_alpine
|
||||||
|
elif [ "$distro" = "dd" ]; then
|
||||||
|
install_dd
|
||||||
|
elif is_use_cloud_image; then
|
||||||
|
create_part
|
||||||
|
install_cloud_image
|
||||||
|
else
|
||||||
|
create_part
|
||||||
|
mount_part
|
||||||
|
if [ "$distro" = "windows" ]; then
|
||||||
|
install_windows
|
||||||
|
else
|
||||||
|
install_redhat_ubuntu
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$sleep" = 2 ]; then
|
||||||
|
exit
|
||||||
|
fi
|
||||||
reboot
|
reboot
|
||||||
|
Loading…
x
Reference in New Issue
Block a user