Skip to content

Instantly share code, notes, and snippets.

@elitak
Last active August 28, 2015 01:03
Show Gist options
  • Save elitak/bf175942b292a33b1e7e to your computer and use it in GitHub Desktop.
Save elitak/bf175942b292a33b1e7e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Rather than scping to then invoking this script on the remote host, you can run:
# sed s,^tarpath=.*,tarpath=theURL, thisfile | ssh targethost
# TODO: add this as an invocation option that can be run on local host.
set -a
if [[ "$BASH_SOURCE" = "$0" ]]; then
[[ `whoami` == "root" ]] || { echo "Run as root."; exit 1; }
[[ -r "$1" ]] || { echo "Give path to tarball or URL as first argument."; exit 2; }
fi
tarpath="$1"
grubdisk=/dev/vda
hostname=gentoo
sshpubid=
mountdir=`mktemp -d`
pushd "$mountdir"
mount -B / "$mountdir"
cd "$mountdir"
umount -R /usr/portage # and any other mounts that will just get in the way.
if [[ -r "$tarpath" ]]; then
tarpath="`dirs -l +1`/$tarpath"
tar -xa --recursive-unlink -f "$tarpath"
else
# TODO check suffix of URL and select correct tar flag
wget -qO- "$tarpath" | tar -xj --recursive-unlink
fi
echo "config_eth0='
`ip a s eth0 | grep 'inet ' | head -n1 | awk '{print $2}'`
`ip a s eth0 | grep 'inet6 .*scope global' | head -n1 | awk '{print $2}'`
'
routes_eth0='
`ip r | grep default | awk '{print $1, $2, $3}'`
`ip -6 r | grep default | awk '{print $1, $2, $3}'`
'
" > /etc/conf.d/net
echo hostname=$hostname > /etc/conf.d/hostname
kver=`ls /lib/modules`
dracut --kver=$kver
cp /boot/kernel-genkernel-x86_64-$kver /boot/vmlinuz-$kver # so that mkconfig picks up the dracut img.
things_handled_in_stage4_already() {
passwd -d root # OR, better yet, just set .ssh/authorized_keys, as below:
rm -f /etc/ssh/*key*
ssh-keygen -A
mkdir -pm 0700 /root/.ssh
install -m 0600 <(echo "$sshpubid") /root/.ssh/authorized_keys
pkill -HUP -f /usr/sbin/sshd
# (check that it works by sshing from client in another shell)
# TODO re-add runlevels, other stuff from fsscript.common
}
# use force in case of gpt (need blocklists)
grub2-install "${grubdisk}" --force
grub2-mkconfig -o /boot/grub/grub.cfg #FIXME: add dolvm as kernelopt, else lvm wont be detected
popd
umount "$mountdir"
sync
#TODO#(sleep 1; echo b > /proc/sysrq-trigger)& disown
echo b > /proc/sysrq-trigger
# TODO remove "build" useflag from make.conf and perform other cleanup
# TODO learn/ is selinux activate,working? other hardened kernel features should be on like pax,grsecurity?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment