core: 重构代码

This commit is contained in:
bin456789 2023-07-25 00:21:08 +08:00
parent e6654dab05
commit ba456c65eb
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

156
trans.sh
View File

@ -20,6 +20,11 @@ error() {
wall "Error: $*"
}
error_and_exit() {
error "$@"
exit 1
}
add_community_repo() {
if ! grep -x 'http.*/community' /etc/apk/repositories; then
alpine_ver=$(cut -d. -f1,2 </etc/alpine-release)
@ -106,6 +111,16 @@ EOF
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() {
apk add lighttpd
ln -sf /reinstall.html /var/www/localhost/htdocs/index.html
@ -164,42 +179,7 @@ clear_previous() {
} 2>/dev/null || true
}
clear_previous
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 不自带lsblkliveos安装的软件也会被带到新系统所以不用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
install_alpine() {
# 还原改动,不然本脚本会被复制到新系统
rm -f /etc/local.d/trans.start
rm -f /etc/runlevels/default/local
@ -247,9 +227,10 @@ if [ "$distro" = "alpine" ]; then
# alpine默认使用 syslinux (efi 环境除外),这里强制使用 grub方便用脚本再次重装
export BOOTLOADER="grub"
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
gzip) prog=gzip ;;
xz) prog=xz ;;
@ -263,23 +244,20 @@ elif [ "$distro" = "dd" ]; then
curl -L $img | $prog -dc >/dev/$xda
sync
else
echo 'Not supported'
sleep 1m
fi
if [ "$sleep" = 2 ]; then
exit
fi
exec reboot
error_and_exit 'Not supported'
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
# 脚本开始
add_community_repo
if ! apk add util-linux aria2 grub udev hdparm e2fsprogs curl parted; then
echo 'Unable to install package!'
sleep 1m
exec reboot
fi
apk add util-linux aria2 grub udev hdparm e2fsprogs curl parted
# 打开dev才能刷新分区名
rc-service udev start
@ -291,9 +269,6 @@ false && vgchange -an
# 移除 lsblk 显示的分区
partx -d /dev/$xda
disk_size=$(blockdev --getsize64 /dev/$xda)
disk_2t=$((2 * 1024 * 1024 * 1024 * 1024))
# xda*1 星号用于 nvme0n1p1 的字母 p
if [ "$distro" = windows ]; then
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.ext4 -F -L os /dev/$xda*2 #2 os
mkfs.ext4 -F -L installer /dev/$xda*3 #3 installer
elif [ "$disk_size" -ge "$disk_2t" ]; then
# bios 2t
elif is_xda_gt_2t; then
# bios > 2t
parted /dev/$xda -s -- \
mklabel gpt \
mkpart '" "' ext4 1MiB 2MiB \
@ -377,8 +352,9 @@ else
fi
update_part /dev/$xda
}
if is_use_cloud_image; then
install_cloud_image() {
apk add qemu-img lsblk
mkdir -p /installer
@ -434,8 +410,9 @@ if is_use_cloud_image; then
fi
# shellcheck disable=SC2154
download $confhome/nocloud.yaml /os$subvol/etc/cloud/cloud.cfg.d/99_nocloud.cfg
exit
else
}
mount_part() {
# 挂载主分区
mkdir -p /os
mount /dev/disk/by-label/os /os
@ -450,10 +427,10 @@ else
mount_args="-t ntfs3"
fi
mount $mount_args /dev/disk/by-label/installer /os/installer
fi
}
install_windows() {
# shellcheck disable=SC2154
if [ "$distro" = "windows" ]; then
download $iso /os/windows.iso
mkdir -p /iso
mount /os/windows.iso /iso
@ -723,13 +700,9 @@ EOF
}
EOF
fi
if [ "$sleep" = 2 ]; then
cd /
sleep infinity
fi
exec reboot
fi
}
install_redhat_ubuntu() {
# 安装 grub2
if is_efi; then
# 注意低版本的grub无法启动f38 arm的内核
@ -790,4 +763,53 @@ else
}
EOF
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