Tools/Linux_reinstall/Ubuntu/ubuntuInit.sh

115 lines
5.2 KiB
Bash
Raw Normal View History

2023-06-07 21:42:25 +08:00
#!/bin/ash
#
# Alpine Linux use "ash" as the default shell.
exec >/dev/tty0 2>&1
# Delete the initial script itself to prevent to be executed in the new system.
2023-06-07 21:43:38 +08:00
rm -f /etc/local.d/ubuntuConf.start
2023-06-07 21:42:25 +08:00
rm -f /etc/runlevels/default/local
2023-06-10 02:15:43 +08:00
# Install necessary components.
apk update
2023-06-10 02:29:07 +08:00
apk add bash bash bash-doc bash-completion coreutils grep sed
2023-06-10 02:15:43 +08:00
2023-06-07 21:42:25 +08:00
# Get Ubuntu Linux configurations.
confFile="/root/alpine.config"
# Read configs from initial file.
2023-06-10 01:23:12 +08:00
IncDisk=$(grep "IncDisk" $confFile | awk '{print $2}')
2023-06-10 02:28:16 +08:00
LinuxMirror=$(grep -w "LinuxMirror" $confFile | awk '{print $2}')
2023-06-10 00:58:51 +08:00
alpineVer=$(grep "alpineVer" $confFile | awk '{print $2}')
2023-06-14 03:57:43 +08:00
TimeZone1=$(grep "TimeZone" $confFile | awk '{print $2}' | cut -d'/' -f 1)
TimeZone2=$(grep "TimeZone" $confFile | awk '{print $2}' | cut -d'/' -f 2)
2023-06-12 15:33:00 +08:00
tmpWORD=$(grep -w "tmpWORD" $confFile | awk '{print $2}')
2023-06-07 21:42:25 +08:00
sshPORT=$(grep "sshPORT" $confFile | awk '{print $2}')
2023-08-23 17:24:36 +08:00
networkAdapter=$(grep "networkAdapter" $confFile | awk '{print $2}')
2023-06-07 21:42:25 +08:00
IPv4=$(grep "IPv4" $confFile | awk '{print $2}')
MASK=$(grep "MASK" $confFile | awk '{print $2}')
2023-06-08 04:48:56 +08:00
ipPrefix=$(grep "ipPrefix" $confFile | awk '{print $2}')
2023-07-01 22:26:15 +08:00
actualIp4Prefix=$(grep "actualIp4Prefix" $confFile | awk '{print $2}')
2023-06-08 04:56:03 +08:00
GATE=$(grep "GATE" $confFile | awk '{print $2}')
2023-06-08 04:30:59 +08:00
ipDNS1=$(grep "ipDNS1" $confFile | awk '{print $2}')
ipDNS2=$(grep "ipDNS2" $confFile | awk '{print $2}')
2023-06-07 21:42:25 +08:00
ip6Addr=$(grep "ip6Addr" $confFile | awk '{print $2}')
ip6Mask=$(grep "ip6Mask" $confFile | awk '{print $2}')
2023-07-01 22:26:15 +08:00
actualIp6Prefix=$(grep "actualIp6Prefix" $confFile | awk '{print $2}')
2023-06-07 21:42:25 +08:00
ip6Gate=$(grep "ip6Gate" $confFile | awk '{print $2}')
2023-06-08 04:30:59 +08:00
ip6DNS1=$(grep "ip6DNS1" $confFile | awk '{print $2}')
ip6DNS2=$(grep "ip6DNS2" $confFile | awk '{print $2}')
2023-06-16 07:06:25 +08:00
setIPv6=$(grep "setIPv6" $confFile | awk '{print $2}')
2023-06-07 21:42:25 +08:00
HostName=$(grep "HostName" $confFile | awk '{print $2}')
2023-06-07 23:31:30 +08:00
DDURL=$(grep "DDURL" $confFile | awk '{print $2}')
2023-06-08 04:35:18 +08:00
targetLinuxMirror=$(grep "targetLinuxMirror" $confFile | awk '{print $2}')
cloudInitUrl=$(grep "cloudInitUrl" $confFile | awk '{print $2}')
2023-06-07 21:42:25 +08:00
# Reset configurations of repositories
true >/etc/apk/repositories
2023-06-10 00:58:51 +08:00
setup-apkrepos $LinuxMirror/$alpineVer/main
2023-06-07 21:42:25 +08:00
setup-apkcache /var/cache/apk
2023-06-10 00:58:51 +08:00
# Add community mirror
sed -i '$a\'$LinuxMirror'/'$alpineVer'/community' /etc/apk/repositories
2023-06-07 21:42:25 +08:00
# Add edge testing to the repositories
2023-06-13 01:16:00 +08:00
sed -i '$a\'$LinuxMirror'/edge/testing' /etc/apk/repositories
2023-06-07 21:42:25 +08:00
2023-06-10 02:15:43 +08:00
# Synchronize time from hardware
hwclock -s
2023-06-07 21:42:25 +08:00
# Install necessary components.
apk update
2023-06-10 02:15:43 +08:00
apk add ca-certificates e2fsprogs hdparm multipath-tools parted util-linux wget
2023-06-07 21:42:25 +08:00
# start dd
2023-06-10 01:23:12 +08:00
wget --no-check-certificate -qO- "$DDURL" | dd of="$IncDisk"
2023-06-07 21:42:25 +08:00
# get valid loop device
loopDevice=$(echo $(losetup -f))
loopDeviceNum=$(echo $(losetup -f) | cut -d'/' -f 3)
# make a soft link between valid loop device and disk
2023-06-10 01:23:12 +08:00
losetup $loopDevice $IncDisk
2023-06-07 21:42:25 +08:00
# get mapper partition
mapperDevice=$(kpartx -av $loopDevice | grep "$loopDeviceNum" | head -n 1 | awk '{print $3}')
# mount Ubuntu dd partition to /mnt
mount /dev/mapper/$mapperDevice /mnt
# download cloud init file
2023-06-08 07:01:08 +08:00
wget --no-check-certificate -qO /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg ''$cloudInitUrl''
2023-06-07 21:42:25 +08:00
2023-06-08 02:29:47 +08:00
# user config
sed -ri 's/HostName/'${HostName}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/tmpWORD/'${tmpWORD}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/sshPORT/'${sshPORT}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-14 03:57:43 +08:00
sed -ri 's/TimeZone/'${TimeZone1}'\/'${TimeZone2}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 04:14:34 +08:00
sed -ri 's/targetLinuxMirror/'${targetLinuxMirror}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-08-23 17:24:36 +08:00
sed -ri 's/networkAdapter/'${networkAdapter}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 04:56:03 +08:00
sed -ri 's/IPv4/'${IPv4}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 07:22:10 +08:00
sed -ri 's/MASK/'${MASK}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 04:56:03 +08:00
sed -ri 's/ipPrefix/'${ipPrefix}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-07-01 22:28:01 +08:00
sed -ri "s/${IPv4}\/${ipPrefix}/${IPv4}\/${actualIp4Prefix}/g" /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 04:56:03 +08:00
sed -ri 's/GATE/'${GATE}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/ipDNS1/'${ipDNS1}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/ipDNS2/'${ipDNS2}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/ip6Addr/'${ip6Addr}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/ip6Mask/'${ip6Mask}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-07-01 22:28:01 +08:00
sed -ri "s/${ip6Addr}\/${ip6Mask}/${ip6Addr}\/${actualIp6Prefix}/g" /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 04:56:03 +08:00
sed -ri 's/ip6Gate/'${ip6Gate}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/ip6DNS1/'${ip6DNS1}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
sed -ri 's/ip6DNS2/'${ip6DNS2}'/g' /mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg
2023-06-08 02:29:47 +08:00
2023-07-29 22:34:25 +08:00
# Disable any datahouse
# Reference: https://github.com/canonical/cloud-init/issues/3772
echo 'datasource_list: [ NoCloud, None ]' > /mnt/etc/cloud/cloud.cfg.d/90_dpkg.cfg
2023-06-16 06:02:41 +08:00
# disable IPv6
[[ "$setIPv6" == "0" ]] && {
sed -ri 's/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 ipv6.disable=1"/g' /mnt/etc/default/grub
sed -ri 's/ro net.ifnames=0 biosdevname=0/ro net.ifnames=0 biosdevname=0 ipv6.disable=1/g' /mnt/boot/grub/grub.cfg
}
2023-06-07 21:42:25 +08:00
# Reboot, the system in the memory will all be written to the hard drive.
2023-06-14 04:52:41 +08:00
exec reboot