2023-09-27 21:29:38 +08:00
#!/bin/ash
#
# Alpine Linux use "ash" as the default shell.
2023-09-25 18:36:46 +08:00
2023-09-27 21:29:38 +08:00
exec >/dev/tty0 2>& 1
# Delete the initial script itself to prevent to be executed in the new system.
rm -f /etc/local.d/rhelConf.start
rm -f /etc/runlevels/default/local
# Install necessary components.
apk update
2023-09-30 03:56:47 +08:00
apk add coreutils grep sed
2023-09-27 21:29:38 +08:00
# Get RHEL Linux configurations.
2023-09-30 23:57:44 +08:00
confFile = '/root/alpine.config'
cloudInitFile = '/mnt/etc/cloud/cloud.cfg.d/99-fake_cloud.cfg'
2023-09-27 21:29:38 +08:00
# Read configs from initial file.
2023-10-03 15:05:49 +08:00
rhelArchitecture = $( grep "rhelArchitecture" $confFile | awk '{print $2}' )
2023-09-27 21:29:38 +08:00
IncDisk = $( grep "IncDisk" $confFile | awk '{print $2}' )
2023-09-28 21:57:41 +08:00
LinuxMirror = $( grep -w "LinuxMirror" $confFile | awk '{print $2}' )
2023-09-27 21:29:38 +08:00
alpineVer = $( grep "alpineVer" $confFile | awk '{print $2}' )
TimeZone1 = $( grep "TimeZone" $confFile | awk '{print $2}' | cut -d'/' -f 1)
TimeZone2 = $( grep "TimeZone" $confFile | awk '{print $2}' | cut -d'/' -f 2)
tmpWORD = $( grep -w "tmpWORD" $confFile | awk '{print $2}' )
sshPORT = $( grep "sshPORT" $confFile | awk '{print $2}' )
networkAdapter = $( grep "networkAdapter" $confFile | awk '{print $2}' )
IPv4 = $( grep "IPv4" $confFile | awk '{print $2}' )
MASK = $( grep "MASK" $confFile | awk '{print $2}' )
ipPrefix = $( grep "ipPrefix" $confFile | awk '{print $2}' )
actualIp4Prefix = $( grep "actualIp4Prefix" $confFile | awk '{print $2}' )
GATE = $( grep "GATE" $confFile | awk '{print $2}' )
ipDNS1 = $( grep "ipDNS1" $confFile | awk '{print $2}' )
ipDNS2 = $( grep "ipDNS2" $confFile | awk '{print $2}' )
iAddrNum = $( grep "iAddrNum" $confFile | awk '{print $2}' )
writeIpsCmd = $( grep "writeIpsCmd" $confFile | awk '{print $2}' )
ip6Addr = $( grep "ip6Addr" $confFile | awk '{print $2}' )
ip6Mask = $( grep "ip6Mask" $confFile | awk '{print $2}' )
actualIp6Prefix = $( grep "actualIp6Prefix" $confFile | awk '{print $2}' )
ip6Gate = $( grep "ip6Gate" $confFile | awk '{print $2}' )
ip6DNS1 = $( grep "ip6DNS1" $confFile | awk '{print $2}' )
ip6DNS2 = $( grep "ip6DNS2" $confFile | awk '{print $2}' )
i6AddrNum = $( grep "i6AddrNum" $confFile | awk '{print $2}' )
writeIp6sCmd = $( grep "writeIp6sCmd" $confFile | awk '{print $2}' )
setIPv6 = $( grep "setIPv6" $confFile | awk '{print $2}' )
HostName = $( grep "HostName" $confFile | awk '{print $2}' )
DDURL = $( grep "DDURL" $confFile | awk '{print $2}' )
DEC_CMD = $( grep "DEC_CMD" $confFile | awk '{print $2}' )
cloudInitUrl = $( grep "cloudInitUrl" $confFile | awk '{print $2}' )
2023-10-01 05:29:47 +08:00
RedHatSeries = $( grep "RedHatSeries" $confFile | awk '{print $2}' )
2023-10-03 08:39:26 +08:00
lowMemMode = $( grep "lowMemMode" $confFile | awk '{print $2}' )
2023-09-27 21:29:38 +08:00
2023-10-01 00:58:28 +08:00
# Reset configurations of repositories.
2023-09-27 21:29:38 +08:00
true >/etc/apk/repositories
setup-apkrepos $LinuxMirror /$alpineVer /main
setup-apkcache /var/cache/apk
2023-10-01 00:58:28 +08:00
# Add community mirror.
2023-09-27 21:29:38 +08:00
sed -i '$a\' $LinuxMirror '/' $alpineVer '/community' /etc/apk/repositories
# Add edge testing to the repositories
# sed -i '$a\'$LinuxMirror'/edge/testing' /etc/apk/repositories
2023-10-01 00:58:28 +08:00
# Synchronize time from hardware.
2023-09-27 21:29:38 +08:00
hwclock -s
# Install necessary components.
apk update
2023-10-01 19:22:41 +08:00
apk add hdparm multipath-tools util-linux wget xz
2023-09-27 21:29:38 +08:00
2023-09-30 23:57:44 +08:00
# Start dd.
2023-10-01 22:47:40 +08:00
wget --no-check-certificate --report-speed= bits --tries= 0 --timeout= 10 --wait= 5 -O- " $DDURL " | $DEC_CMD | dd of = " $IncDisk " status = progress
2023-09-27 21:29:38 +08:00
2023-09-30 23:57:44 +08:00
# Get valid loop device.
2023-09-27 21:29:38 +08:00
loopDevice = $( echo $( losetup -f) )
loopDeviceNum = $( echo $( losetup -f) | cut -d'/' -f 3)
2023-09-30 23:57:44 +08:00
# Make a soft link between valid loop device and disk.
2023-09-27 21:29:38 +08:00
losetup $loopDevice $IncDisk
2023-09-30 23:57:44 +08:00
# Get mapper partition.
2023-09-27 21:29:38 +08:00
mapperDevice = $( kpartx -av $loopDevice | grep " $loopDeviceNum " | sort -rn | sed -n '1p' | awk '{print $3}' )
2023-09-30 23:57:44 +08:00
# Mount RHEL dd partition to /mnt .
2023-09-27 21:29:38 +08:00
mount /dev/mapper/$mapperDevice /mnt
2023-09-30 23:57:44 +08:00
# Download cloud init file.
wget --no-check-certificate -qO $cloudInitFile '' $cloudInitUrl ''
2023-09-27 21:29:38 +08:00
2023-09-30 23:57:44 +08:00
# User config.
sed -ri 's/HostName/' ${ HostName } '/g' $cloudInitFile
sed -ri 's/tmpWORD/' ${ tmpWORD } '/g' $cloudInitFile
2023-10-01 03:10:01 +08:00
sed -ri 's/sshPORT/' ${ sshPORT } '/g' $cloudInitFile
2023-09-30 23:57:44 +08:00
sed -ri 's/TimeZone/' ${ TimeZone1 } '\/' ${ TimeZone2 } '/g' $cloudInitFile
sed -ri 's/networkAdapter/' ${ networkAdapter } '/g' $cloudInitFile
2023-09-27 21:29:38 +08:00
if [ [ " $iAddrNum " -ge "2" ] ] ; then
2023-10-12 16:19:12 +08:00
sed -ri 's#IPv4/ipPrefix#' ${ writeIpsCmd } '#g' $cloudInitFile
2023-09-27 21:29:38 +08:00
else
2023-10-12 16:19:12 +08:00
sed -ri 's/IPv4/' ${ IPv4 } '/g' $cloudInitFile
sed -ri 's/ipPrefix/' ${ ipPrefix } '/g' $cloudInitFile
sed -ri " s/ ${ IPv4 } \/ ${ ipPrefix } / ${ IPv4 } \/ ${ actualIp4Prefix } /g " $cloudInitFile
2023-09-27 21:29:38 +08:00
fi
2023-09-30 23:57:44 +08:00
sed -ri 's/GATE/' ${ GATE } '/g' $cloudInitFile
sed -ri 's/ipDNS1/' ${ ipDNS1 } '/g' $cloudInitFile
sed -ri 's/ipDNS2/' ${ ipDNS2 } '/g' $cloudInitFile
2023-09-27 21:29:38 +08:00
if [ [ " $i6AddrNum " -ge "2" ] ] ; then
2023-10-12 16:19:12 +08:00
sed -ri 's#ip6Addr/ip6Mask#' ${ writeIp6sCmd } '#g' $cloudInitFile
2023-09-27 21:29:38 +08:00
else
2023-10-12 16:19:12 +08:00
sed -ri 's/ip6Addr/' ${ ip6Addr } '/g' $cloudInitFile
sed -ri 's/ip6Mask/' ${ ip6Mask } '/g' $cloudInitFile
sed -ri " s/ ${ ip6Addr } \/ ${ ip6Mask } / ${ ip6Addr } \/ ${ actualIp6Prefix } /g " $cloudInitFile
2023-09-27 21:29:38 +08:00
fi
2023-09-30 23:57:44 +08:00
sed -ri 's/ip6Gate/' ${ ip6Gate } '/g' $cloudInitFile
sed -ri 's/ip6DNS1/' ${ ip6DNS1 } '/g' $cloudInitFile
sed -ri 's/ip6DNS2/' ${ ip6DNS2 } '/g' $cloudInitFile
2023-09-27 21:29:38 +08:00
2023-09-30 23:57:44 +08:00
# Disable SELinux permanently.
2023-09-29 07:55:55 +08:00
sed -ri 's/^SELINUX=.*/SELINUX=disabled/g' /mnt/etc/selinux/config
2023-09-30 23:57:44 +08:00
# Disable IPv6.
[ [ " $setIPv6 " = = "0" ] ] && sed -ri 's/net.ifnames=0 biosdevname=0/net.ifnames=0 biosdevname=0 ipv6.disable=1/g' /mnt/etc/default/grub
2023-10-06 08:35:37 +08:00
# Add tty console for grub.
2023-10-09 23:48:23 +08:00
sed -ri 's/console=tty0//g' /mnt/etc/default/grub
sed -ri 's/console=ttyS0,115200n8/console=ttyS0,115200n8 console=tty1/g' /mnt/etc/default/grub
2023-10-06 08:35:37 +08:00
2023-09-30 23:57:44 +08:00
# Permit root user login by password, change ssh port.
sed -ri 's/^#?PermitRootLogin.*/PermitRootLogin yes/g' /mnt/etc/ssh/sshd_config
sed -ri 's/^#?PasswordAuthentication.*/PasswordAuthentication yes/g' /mnt/etc/ssh/sshd_config
sed -ri 's/^#?Port.*/Port ' ${ sshPORT } '/g' /mnt/etc/ssh/sshd_config
2023-10-03 08:39:26 +08:00
# Disable allocate swap.
2023-10-03 13:19:29 +08:00
# Note: Swap allowcation in "runcmd:" stage during execution of cloud init on aarch64 CPU architecture would cause a fatal because of "cloud-final.service" runs failed.
2023-10-03 15:05:49 +08:00
[ [ " $lowMemMode " != "1" || " $rhelArchitecture " = = "aarch64" ] ] && sed -i '/swapspace/d' $cloudInitFile
2023-10-03 08:39:26 +08:00
2023-09-30 23:57:44 +08:00
# Hack cloud init.
2023-10-12 16:19:12 +08:00
# Note: this trick has a great effect on Ubuntu 20.04+, AlmaLinux / Rocky 9+ in almost any cloud platforms but unfortunately it
2023-10-01 05:31:58 +08:00
# is not suitable for Rocky 8 otherwise cloud init will meet a fatal may because of the lower version of python3.6(others are 3.9).
2023-10-01 05:29:47 +08:00
# More details: https://github.com/leitbogioro/Tools/blob/master/Linux_reinstall/Ubuntu/ubuntuInit.sh
[ [ " $RedHatSeries " -ge "9" ] ] && {
2023-10-12 16:19:12 +08:00
utilProgram = $( find /mnt/usr/lib/python* -name "util.py" | grep "cloudinit" | head -n 1)
sed -ri 's/iso9660/osi9876/g' $utilProgram
sed -ri 's#"blkid"#"echo"#g' $utilProgram
2023-10-01 05:29:47 +08:00
}
2023-09-30 23:57:44 +08:00
# Umount mounted directory and loop device.
umount /mnt
kpartx -dv $loopDevice
losetup -d $loopDevice
2023-09-27 21:29:38 +08:00
2023-10-03 13:23:35 +08:00
# Reboot, the temporary intermediary system of AlpineLinux which is executing in memory will be destroyed during the power down and then server will reboot to the newly installed system immediately.
2023-09-27 21:29:38 +08:00
exec reboot