Last active
January 31, 2021 12:45
-
-
Save ym/6af571f704aff35c9ce7ea5597b5f7c1 to your computer and use it in GitHub Desktop.
install_ripe.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| # constants | |
| MIRROR='http://mirror.us.leaseweb.net/centos/' | |
| HOSTNAME="$(hostname)" | |
| SSH_KEYS="$(cat /root/.ssh/authorized_keys)" | |
| function decompress_initrd() { | |
| local initrd_tmp="$(mktemp)" | |
| wget -O "${initrd_tmp}" "${MIRROR}7/os/x86_64/isolinux/initrd.img" | |
| wget -O '/boot/vmlinuz_ripe' "${MIRROR}7/os/x86_64/isolinux/vmlinuz" | |
| local initrd_type="$(file ${initrd_tmp} |grep -o ':.*compressed data' |cut -d' ' -f2 |sed -r 's/(.*)/\L\1/' |head -n1)" | |
| [[ -z "$initrd_type" ]] && echo "Detect compressed type fail." && exit 1; | |
| local initrd_compressed='0' | |
| for comp_type in `echo -en 'gzip\nlzma\nxz'` | |
| do | |
| if [[ "${initrd_type}" == "${comp_type}" ]]; then | |
| initrd_compressed='1' | |
| if [[ "${comp_type}" == 'gzip' ]]; then | |
| initrd_new="${initrd_tmp}.gz" | |
| else | |
| initrd_new="${initrd_tmp}.${comp_type}" | |
| fi | |
| mv -f "${initrd_tmp}" "${initrd_new}" | |
| break; | |
| fi | |
| done | |
| [[ "$initrd_compressed" != '1' ]] && echo "Detect compressed type not support." && exit 1; | |
| [[ "$comp_type" == 'lzma' ]] && decompress_cmd='xz --format=lzma --decompress'; | |
| [[ "$comp_type" == 'xz' ]] && decompress_cmd='xz --decompress'; | |
| [[ "$comp_type" == 'gzip' ]] && decompress_cmd='gzip -d'; | |
| $decompress_cmd < "${initrd_new}" | cpio --extract --verbose --make-directories --no-absolute-filenames >>/dev/null 2>&1 | |
| rm -rf "${initrd_new}" "${initrd_tmp}" | |
| } | |
| function build_kickstart() { | |
| cat > ks.cfg <<EOF | |
| text | |
| skipx | |
| install | |
| lang en_US.UTF-8 | |
| keyboard us | |
| rootpw --iscrypted !! --lock | |
| firewall --service=ssh | |
| timezone --utc UTC | |
| bootloader --location=mbr --append="crashkernel=auto" | |
| clearpart --drives=/dev/vda --all --initlabel | |
| zerombr | |
| part /boot --fstype=ext4 --size=1024 --ondisk=/dev/vda | |
| part pv.01 --size=1 --grow --ondisk=/dev/vda | |
| volgroup vg01 --pesize=4096 pv.01 | |
| logvol / --fstype=ext4 --name=lv_root --vgname=vg01 --size=1 --grow --fsoptions="noatime,nodiratime,discard" | |
| network --bootproto=dhcp --hostname=${HOSTNAME} --onboot=yes | |
| reboot | |
| %packages | |
| %end | |
| %post | |
| # Bootstrap SSH key | |
| mkdir -m 0700 -p /root/.ssh | |
| cat << EEOOFF > /root/.ssh/authorized_keys | |
| ${SSH_KEYS} | |
| EEOOFF | |
| restorecon -R /root/.ssh | |
| %end | |
| EOF | |
| } | |
| function build_initrd() { | |
| find . | cpio -H newc --create --verbose | gzip -9 > /boot/initrd_ripe.img; | |
| } | |
| function update_grub() { | |
| cat > /etc/grub.d/40_custom <<EOF | |
| #!/bin/sh | |
| exec tail -n +3 \$0 | |
| menuentry "Install RIPE Atlas Anchor" { | |
| linux /boot/vmlinuz_ripe ks=file://ks.cfg text console=tty0 console=ttyS0,115200n8 serial inst.repo=${MIRROR}7/os/x86_64/ | |
| initrd /boot/initrd_ripe.img | |
| } | |
| EOF | |
| chmod +x /etc/grub.d/40_custom | |
| grub2-mkconfig --output=/boot/grub2/grub.cfg | |
| grub2-reboot "Install RIPE Atlas Anchor" | |
| } | |
| # create temporary directory | |
| TMP_DIR=$(mktemp -d) | |
| pushd "${TMP_DIR}" | |
| decompress_initrd | |
| build_kickstart | |
| build_initrd | |
| update_grub | |
| reboot |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wget -O- https://gist.githubusercontent.com/ym/6af571f704aff35c9ce7ea5597b5f7c1/raw/bf09b21a55cc29e780b55adac85038aca6ea085a/install_ripe.sh | bash