Tools/Linux_reinstall/Alpine/alpineInit.sh

128 lines
4.4 KiB
Bash
Raw Normal View History

2023-05-29 21:37:17 +08:00
#!/bin/ash
2023-05-29 16:38:07 +08:00
#
2023-05-29 23:09:05 +08:00
# Alpine Linux use "ash" as the default shell.
2023-05-28 14:03:19 +08:00
exec >/dev/tty0 2>&1
2023-05-29 22:10:52 +08:00
# Delete the initial script itself to prevent to be executed in the new system.
rm -f /etc/local.d/alpineConf.start
rm -f /etc/runlevels/default/local
2023-05-29 21:41:03 +08:00
# Install necessary components.
apk update
2023-05-31 00:37:14 +08:00
apk add bash bash bash-doc bash-completion coreutils sed
2023-05-29 21:41:03 +08:00
# Get Alpine Linux configurations.
confFile="/root/alpine.config"
# Read configs from initial file.
2023-06-10 01:26:38 +08:00
IncDisk=$(grep "IncDisk" $confFile | awk '{print $2}')
2023-05-29 21:41:03 +08:00
LinuxMirror=$(grep "LinuxMirror" $confFile | awk '{print $2}')
2023-06-09 22:55:13 +08:00
alpineVer=$(grep "alpineVer" $confFile | awk '{print $2}')
2023-05-29 21:41:03 +08:00
TimeZone=$(grep "TimeZone" $confFile | awk '{print $2}')
2023-06-12 15:33:38 +08:00
tmpWORD=$(grep -w "tmpWORD" $confFile | awk '{print $2}')
2023-05-29 21:41:03 +08:00
sshPORT=$(grep "sshPORT" $confFile | awk '{print $2}')
2023-06-09 22:49:13 +08:00
AlpineTestRepository=$(grep "AlpineTestRepository" $confFile | awk '{print $2}')
2023-05-29 23:09:05 +08:00
IPv4=$(grep "IPv4" $confFile | awk '{print $2}')
MASK=$(grep "MASK" $confFile | awk '{print $2}')
GATE=$(grep "GATE" $confFile | awk '{print $2}')
ip6Addr=$(grep "ip6Addr" $confFile | awk '{print $2}')
ip6Mask=$(grep "ip6Mask" $confFile | awk '{print $2}')
2023-05-30 00:27:18 +08:00
ip6Gate=$(grep "ip6Gate" $confFile | awk '{print $2}')
HostName=$(grep "HostName" $confFile | awk '{print $2}')
2023-05-29 21:41:03 +08:00
2023-05-29 21:11:13 +08:00
# Setting Alpine Linux by "setup-alpine" will enable the following services
# https://github.com/alpinelinux/alpine-conf/blob/c5131e9a038b09881d3d44fb35e86851e406c756/setup-alpine.in#L189
2023-05-30 03:39:50 +08:00
acpid | default
crond | default
seedrng | boot
2023-05-29 21:11:13 +08:00
2023-06-09 22:38:18 +08:00
# Reset configurations of repositories
true >/etc/apk/repositories
2023-06-10 00:00:09 +08:00
setup-apkrepos $LinuxMirror/$alpineVer/main
2023-06-09 22:38:18 +08:00
setup-apkcache /var/cache/apk
2023-06-10 00:09:06 +08:00
# Add community mirror
2023-06-10 00:00:09 +08:00
sed -i '$a\'$LinuxMirror'/'$alpineVer'/community' /etc/apk/repositories
2023-06-10 00:09:06 +08:00
# Add edge testing to the repositories
2023-06-13 01:13:11 +08:00
# sed -i '$a\'$LinuxMirror'/edge/testing' /etc/apk/repositories
2023-06-09 22:49:13 +08:00
2023-05-29 16:38:07 +08:00
# Synchronize time from hardware
2023-05-28 14:03:19 +08:00
hwclock -s
2023-05-29 16:38:07 +08:00
# Install and enable ssh
2023-05-29 21:11:13 +08:00
echo root:${tmpWORD} | chpasswd
2023-05-28 14:03:19 +08:00
printf '\nyes' | setup-sshd
2023-05-29 21:57:11 +08:00
sed -ri 's/^#?Port.*/Port '${sshPORT}'/g' /etc/ssh/sshd_config
2023-05-28 14:03:19 +08:00
2023-05-29 16:52:06 +08:00
# Network configurations.
2023-05-30 01:18:00 +08:00
# https://wiki.alpinelinux.org/wiki/Configure_Networking
2023-05-29 16:52:06 +08:00
# Setup adapter.
setup-interfaces -a
# Generate network file of "/etc/network/interfaces"
rc-update add networking boot
# Delete network file and replace it by us.
2023-05-29 22:24:24 +08:00
rm -rf /etc/network/interfaces
mv /etc/network/tmp_interfaces /etc/network/interfaces
2023-05-29 23:09:05 +08:00
# Static network configurating
sed -ri 's/IPv4/'${IPv4}'/g' /etc/network/interfaces
sed -ri 's/MASK/'${MASK}'/g' /etc/network/interfaces
sed -ri 's/GATE/'${GATE}'/g' /etc/network/interfaces
sed -ri 's/ip6Addr/'${ip6Addr}'/g' /etc/network/interfaces
sed -ri 's/ip6Mask/'${ip6Mask}'/g' /etc/network/interfaces
sed -ri 's/ip6Gate/'${ip6Gate}'/g' /etc/network/interfaces
# Restoring access permission.
2023-05-29 22:24:24 +08:00
chmod a+x /etc/network/interfaces
2023-05-29 23:42:24 +08:00
# Enable IPv6
modprobe ipv6
2023-05-30 04:04:49 +08:00
# Rebuild hosts
rm -rf /etc/hosts
# Add special IPv4 addresses
echo "127.0.0.1 $HostName localhost.localdomain" >> /etc/hosts
2023-05-30 00:27:18 +08:00
# Add special IPv6 addresses
2023-05-30 04:04:49 +08:00
echo "::1 $HostName localhost.localdomain ipv6-localhost ipv6-loopback" >> /etc/hosts
2023-05-30 00:27:18 +08:00
echo "fe00::0 ipv6-localnet" >> /etc/hosts
echo "ff00::0 ipv6-mcastprefix" >> /etc/hosts
echo "ff02::1 ipv6-allnodes" >> /etc/hosts
echo "ff02::2 ipv6-allrouters" >> /etc/hosts
echo "ff02::3 ipv6-allhosts" >> /etc/hosts
# Hostname
rm -rf /etc/hostname
echo "$HostName" > /etc/hostname
hostname -F /etc/hostname
2023-05-29 16:52:06 +08:00
2023-05-29 16:38:07 +08:00
# Localization
2023-05-28 14:03:19 +08:00
setup-keymap us us
2023-05-29 21:11:13 +08:00
setup-timezone -i ${TimeZone}
2023-05-28 14:03:19 +08:00
setup-ntp chrony
2023-05-29 16:38:07 +08:00
# In arm netboot initramfs init,
2023-05-30 17:03:18 +08:00
# if rtc hardware is detected, add hwclock for system, otherwise add swclock,
# this settings will be copied to the new system,
# but the new system boot from initramfs chroot can detect rtc hardwa1 correctly,
# so we use hwclock manually to fix it.
2023-05-28 14:03:19 +08:00
rc-update del swclock boot
rc-update add hwclock boot
2023-05-29 17:30:47 +08:00
# Replace "ash" to "bash" as the default shell of the Alpine Linux.
2023-05-29 22:10:52 +08:00
sed -ri 's/ash/bash/g' /etc/passwd
2023-05-29 22:24:24 +08:00
# Insall more components.
2023-06-09 22:49:13 +08:00
apk update
2023-06-15 22:32:49 +08:00
apk add axel bind-tools cpio curl e2fsprogs figlet grep grub gzip hdparm lsblk lsof net-tools parted udev util-linux virt-what vim wget
2023-05-31 00:12:26 +08:00
2023-05-30 17:03:18 +08:00
# Use kernel "virt" if be executed on virtual machine.
2023-05-30 00:34:48 +08:00
cp /etc/apk/world /tmp/world.old
[[ -n "$(virt-what)" ]] && kernelOpt="-k virt"
2023-05-30 17:03:18 +08:00
# Make a blank motd to avoid Alpine Linux writes a new one.
2023-06-16 16:38:55 +08:00
rm -rf /etc/motd
touch /etc/motd
2023-05-30 16:21:39 +08:00
2023-05-29 22:24:24 +08:00
# Install to hard drive.
export BOOTLOADER="grub"
2023-06-10 01:26:38 +08:00
printf 'y' | setup-disk -m sys $kernelOpt -s 0 $IncDisk
2023-05-29 22:24:24 +08:00
2023-05-29 16:38:07 +08:00
# Reboot, the system in the memory will all be written to the hard drive.
exec reboot