cloudatcost-pxe.sh
· 1.2 KiB · Bash
Raw
#!/usr/bin/env bash
cdr2mask ()
{
# Number of args to shift, 255..255, first non-255 byte, zeroes
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
[[ $1 -gt 1 ]] && shift ${1} || shift
echo "${1-0}.${2-0}.${3-0}.${4-0}"
}
cd /boot || exit 1
wget --continue --output-document=/boot/netboot.xyz.lkrn https://boot.netboot.xyz/ipxe/netboot.xyz.lkrn
_Dev="$(ip route list | awk '/^default/ {print $5}')"
_Gateway="$(ip route list | awk '/^default/ {print $3}')"
_IP="$(ip -o -4 addr show "${_Dev}" | awk '{print $4}')"
_Netmask="$(cdr2mask "${_IP#*/}")"
_IP=${_IP%/*}
cat <<_EOF_ > /boot/netboot.xyz-initrd
#!ipxe
#/boot/netboot.xyz-initrd
imgfree
set net0/ip ${_IP%/*}
set net0/netmask ${_Netmask}
set net0/gateway ${_Gateway}
set dns 208.67.222.222
ifopen net0
chain --autofree https://boot.netboot.xyz
_EOF_
cat <<_EOF_ > /etc/grub.d/09_custom
echo "
menuentry 'netboot.xyz' {
set root='hd0,msdos1'
linux16 /netboot.xyz.lkrn
initrd16 /netboot.xyz-initrd
}
"
_EOF_
chmod +x /etc/grub.d/09_custom
update-grub2 || { grub2-mkconfig -o /boot/grub2/grub.cfg; grub2-set-default 0; }
echo "Network Interface: ${_Dev}"
echo "IP: ${_IP%/*}"
echo "Netmask: ${_Netmask}"
echo "Gateway: ${_Gateway}"
reboot
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | cdr2mask () |
| 4 | { |
| 5 | # Number of args to shift, 255..255, first non-255 byte, zeroes |
| 6 | set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 |
| 7 | [[ $1 -gt 1 ]] && shift ${1} || shift |
| 8 | echo "${1-0}.${2-0}.${3-0}.${4-0}" |
| 9 | } |
| 10 | |
| 11 | cd /boot || exit 1 |
| 12 | wget --continue --output-document=/boot/netboot.xyz.lkrn https://boot.netboot.xyz/ipxe/netboot.xyz.lkrn |
| 13 | |
| 14 | _Dev="$(ip route list | awk '/^default/ {print $5}')" |
| 15 | _Gateway="$(ip route list | awk '/^default/ {print $3}')" |
| 16 | _IP="$(ip -o -4 addr show "${_Dev}" | awk '{print $4}')" |
| 17 | _Netmask="$(cdr2mask "${_IP#*/}")" |
| 18 | _IP=${_IP%/*} |
| 19 | |
| 20 | cat <<_EOF_ > /boot/netboot.xyz-initrd |
| 21 | #!ipxe |
| 22 | #/boot/netboot.xyz-initrd |
| 23 | imgfree |
| 24 | set net0/ip ${_IP%/*} |
| 25 | set net0/netmask ${_Netmask} |
| 26 | set net0/gateway ${_Gateway} |
| 27 | set dns 208.67.222.222 |
| 28 | ifopen net0 |
| 29 | chain --autofree https://boot.netboot.xyz |
| 30 | _EOF_ |
| 31 | |
| 32 | cat <<_EOF_ > /etc/grub.d/09_custom |
| 33 | echo " |
| 34 | menuentry 'netboot.xyz' { |
| 35 | set root='hd0,msdos1' |
| 36 | linux16 /netboot.xyz.lkrn |
| 37 | initrd16 /netboot.xyz-initrd |
| 38 | } |
| 39 | " |
| 40 | _EOF_ |
| 41 | chmod +x /etc/grub.d/09_custom |
| 42 | |
| 43 | update-grub2 || { grub2-mkconfig -o /boot/grub2/grub.cfg; grub2-set-default 0; } |
| 44 | |
| 45 | echo "Network Interface: ${_Dev}" |
| 46 | echo "IP: ${_IP%/*}" |
| 47 | echo "Netmask: ${_Netmask}" |
| 48 | echo "Gateway: ${_Gateway}" |
| 49 | |
| 50 | reboot |