Ultima attività 6 months ago

cloudatcost-pxe.sh Raw
1#!/usr/bin/env bash
2
3cdr2mask ()
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
11cd /boot || exit 1
12wget --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
20cat <<_EOF_ > /boot/netboot.xyz-initrd
21#!ipxe
22#/boot/netboot.xyz-initrd
23imgfree
24set net0/ip ${_IP%/*}
25set net0/netmask ${_Netmask}
26set net0/gateway ${_Gateway}
27set dns 208.67.222.222
28ifopen net0
29chain --autofree https://boot.netboot.xyz
30_EOF_
31
32cat <<_EOF_ > /etc/grub.d/09_custom
33echo "
34menuentry 'netboot.xyz' {
35 set root='hd0,msdos1'
36 linux16 /netboot.xyz.lkrn
37 initrd16 /netboot.xyz-initrd
38}
39"
40_EOF_
41chmod +x /etc/grub.d/09_custom
42
43update-grub2 || { grub2-mkconfig -o /boot/grub2/grub.cfg; grub2-set-default 0; }
44
45echo "Network Interface: ${_Dev}"
46echo "IP: ${_IP%/*}"
47echo "Netmask: ${_Netmask}"
48echo "Gateway: ${_Gateway}"
49
50reboot