Update alpineInit.sh

This commit is contained in:
Molly Lau 2023-05-29 17:38:07 +09:00 committed by GitHub
parent 457e637092
commit a79dd4dc00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,87 +1,71 @@
#!/bin/ash #!/bin/bash
# shellcheck shell=dash #
# alpine 默认使用 busybox ash # Alpine Linux use "ash" as default shell.
# 显示输出到前台
# 似乎script更优雅但 alpine 不带 script 命令
# script -f/dev/tty0
exec >/dev/tty0 2>&1 exec >/dev/tty0 2>&1
add_community_repo() { addCommunityRepo() {
alpine_ver=$(cut -d. -f1,2 </etc/alpine-release) alpineVer=$(cut -d. -f1,2 </etc/alpine-release)
echo http://dl-cdn.alpinelinux.org/alpine/v$alpine_ver/community >>/etc/apk/repositories echo LinuxMirror/v$alpineVer/community >>/etc/apk/repositories
} }
# 找到主硬盘 # Find the main disk.
xda=$(ls /dev/ | grep -Ex '[shv]da|nvme0n1') mainDisk="IncDisk"
# arm要手动从硬件同步时间避免访问https出错 # Synchronize time from hardware
hwclock -s hwclock -s
# 安装并打开 ssh # Install and enable ssh
echo root:LeitboGi0ro | chpasswd echo root:LeitboGi0ro | chpasswd
printf '\nyes' | setup-sshd printf '\nyes' | setup-sshd
sed -ri 's/^#?Port.*/Port sshPORT/g' /etc/ssh/sshd_config
# 还原改动,不然本脚本会被复制到新系统 # Delete the initial script itself to prevent to be executed in the new system.
rm -f /etc/local.d/alpineConf.start rm -f /etc/local.d/alpineConf.start
rm -f /etc/runlevels/default/local rm -f /etc/runlevels/default/local
# 网络 # Localization
setup-interfaces -a
# 生成 /etc/network/interfaces
rc-update add networking boot
# 设置
setup-keymap us us setup-keymap us us
setup-timezone -i Asia/Shanghai setup-timezone -i TimeZone
setup-ntp chrony setup-ntp chrony
# 在 arm netboot initramfs init 中 # In arm netboot initramfs init,
# 如果识别到rtc硬件就往系统添加hwclock服务否则添加swclock # If rtc hardware is detected, add hwclock for system, otherwise add swclock
# 这个设置也被复制到安装的系统中 # This settings will be copied to the new system
# 但是从initramfs chroot到真正的系统后是能识别rtc硬件的 # But the new system boot from initramfs chroot can detect rtc hardwa1 correctly
# 所以我们手动改用hwclock修复这个问题 # So we use hwclock manually to fix it
rc-update del swclock boot rc-update del swclock boot
rc-update add hwclock boot rc-update add hwclock boot
# 通过 setup-alpine 安装会多启用几个服务 # Setting Alpine Linux by "setup-alpine" will enable the following services
# https://github.com/alpinelinux/alpine-conf/blob/c5131e9a038b09881d3d44fb35e86851e406c756/setup-alpine.in#L189 # https://github.com/alpinelinux/alpine-conf/blob/c5131e9a038b09881d3d44fb35e86851e406c756/setup-alpine.in#L189
# acpid | default # acpid | default
# crond | default # crond | default
# seedrng | boot # seedrng | boot
# 添加 virt-what 用到的社区仓库 # Add virt-what to community repository
add_community_repo addCommunityRepo
# 如果是 vm 就用 virt 内核 # Use kernel "virt" if be executed on virtual machine
cp /etc/apk/world /tmp/world.old cp /etc/apk/world /tmp/world.old
[[ -n "$(virt-what)" ]] && kernel_opt="-k virt" [[ -n "$(virt-what)" ]] && kernelOpt="-k virt"
# 重置为官方仓库配置 # Reset configurations of repositories
true >/etc/apk/repositories true >/etc/apk/repositories
setup-apkrepos -1 setup-apkrepos -1
setup-apkcache /var/cache/apk setup-apkcache /var/cache/apk
# 去掉仓库 URL 配置中所有被注释的条目 # Delete comment in the repositories
sed -i 's/#//' /etc/apk/repositories sed -i 's/#//' /etc/apk/repositories
# 加入 edge testing 作为仓库源之一 # Add edge testing to the repositories
sed -i '$a\http://dl-cdn.alpinelinux.org/alpine/edge/testing' /etc/apk/repositories sed -i '$a\AlpineTestRepository' /etc/apk/repositories
apk update apk update
apk add bash bash-doc bash-completion bind-tools coreutils cpio curl figlet grep gzip lsblk net-tools python3 py3-pip sed vim apk add bash bash-doc bash-completion bind-tools coreutils cpio curl figlet grep gzip lsblk net-tools python3 py3-pip sed vim
# 加入定制 motd # Install to hard drive.
rm -rf /etc/motd
wget --no-check-certificate -O /etc/profile.d/motd.sh https://gitee.com/mb9e8j2/Tools/raw/master/Linux_reinstall/Alpine/motd.sh
chmod a+x /etc/profile.d/motd.sh
# 替换 Alpine Linux 自带的 ash 为 bash.
sed -i 's/ash/bash/' /etc/passwd
# 安装到硬盘
# 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 $kernelOpt -s 0 $mainDisk
# 重启,此时内存中的系统会被自动写入到硬盘中 # Reboot, the system in the memory will all be written to the hard drive.
exec reboot exec reboot