]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/helper/gcp-setup
6 # Set PATH to find our own executables
7 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
9 # GCP only supports an MTU of 1460
15 wget
--header="Metadata-Flavor: Google" -qO - "http://169.254.169.254/computeMetadata/v1/${file}"
21 local o1
=$
(( (n
& 0xff000000) >> 24 ))
22 local o2
=$
(( (n
& 0xff0000) >> 16 ))
23 local o3
=$
(( (n
& 0xff00) >> 8 ))
24 local o4
=$
(( (n
& 0xff) ))
26 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
35 for i
in ${address//\./ }; do
36 integer
=$
(( (integer
<< 8) + i ))
39 printf "%d\n" "${integer}"
45 local zeros=$(( 32 - prefix ))
49 for (( i=0; i<${zeros}; i++ )); do
50 netmask=$(( (netmask << 1) ^ 1 ))
53 to_address "$(( netmask ^ 0xffffffff ))"
56 import_gcp_configuration() {
57 local instance_id="$(get instance/id)"
59 boot_mesg "Importing Google Compute Platform configuration for instance ${instance_id}..."
62 echo "${instance_id}" > /var/run/gcp-instance-id
64 # Initialise system settings
65 local hostname=$(get instance/hostname)
68 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
69 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
73 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
74 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
78 if ! getent passwd setup &>/dev/null; then
79 useradd setup -s /usr/bin/run-setup -g nobody -m
85 # Import SSH keys for setup user
87 while read -r line; do
88 # Strip the username part from the key
89 local key="${line#*:}"
91 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
92 mkdir -p "/home/setup/.ssh"
93 chmod 700 "/home/setup/.ssh"
94 chown setup.nobody "/home/setup/.ssh"
96 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
97 chmod 600 "/home/setup/.ssh/authorized_keys"
98 chown setup.nobody "/home/setup/.ssh/authorized_keys"
100 done <<<"$(get instance/attributes/ssh-keys)"
102 # Download the user-data script only on the first boot
103 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
104 # Download a startup script
105 local script="$(get instance/attributes/startup-script)"
108 if [ "${script:0:2}" = "#!" ]; then
109 echo "${script}" > /tmp/gcp-startup.script
110 chmod 700 /tmp/gcp-startup.script
113 local now="$(date -u +"%s")"
114 /tmp/gcp-startup.script &>/var/log/startup-script.log.${now}
116 # Delete the script right away
117 rm /tmp/gcp-startup.script
121 # Import network configuration
122 # After this, no network connectivity will be available from this script due to the
123 # renaming of the network interfaces for which they have to be shut down
125 : > /var/ipfire/ethernet/settings
128 for device_number in $(get instance/network-interfaces/); do
129 # Remove trailing slash
130 device_number="${device_number//\//}"
132 local mac="$(get "instance/network-interfaces/${device_number}/mac")"
134 # XXX TODO read the MTU because Google seems to only support 1460
137 local ipv4_address="$(get "instance/network-interfaces/${device_number}/ip")"
138 local ipv4_address_num="$(to_integer "${ipv4_address}")"
140 local netmask="$(get "instance/network-interfaces/${device_number}/subnetmask")"
141 local netmask_num="$(to_integer "${netmask}")"
143 # Calculate the network and broadcast addresses
144 local netaddress="$(to_address $(( ipv4_address_num & netmask_num )))"
146 case "${device_number}" in
149 local interface_name="red0"
150 local gateway="$(get instance/network-interfaces/${device_number}/gateway)"
153 echo "RED_TYPE=STATIC"
154 echo "RED_DEV=${interface_name}"
155 echo "RED_MACADDR=${mac}"
156 echo "RED_DESCRIPTION='${interface_id}'"
157 echo "RED_ADDRESS=${ipv4_address}"
158 echo "RED_NETMASK=${netmask}"
159 echo "RED_NETADDRESS=${netaddress}"
160 echo "RED_MTU=${DEFAULT_MTU}"
161 echo "DEFAULT_GATEWAY=${gateway}"
162 ) >> /var/ipfire/ethernet/settings
164 # Import aliases for RED
165 for alias in $(get "instance/network-interfaces/${device_number}/ip-aliases"); do
167 done > /var/ipfire/ethernet/aliases
172 local interface_name="green0"
175 echo "GREEN_DEV=${interface_name}"
176 echo "GREEN_MACADDR=${mac}"
177 echo "GREEN_DESCRIPTION='${interface_id}'"
178 echo "GREEN_ADDRESS=${ipv4_address}"
179 echo "GREEN_NETMASK=${netmask}"
180 echo "GREEN_NETADDRESS=${netaddress}"
181 echo "GREEN_MTU=${DEFAULT_MTU}"
182 ) >> /var/ipfire/ethernet/settings
187 local interface_name="orange0"
191 echo "ORANGE_DEV=${interface_name}"
192 echo "ORANGE_MACADDR=${mac}"
193 echo "ORANGE_DESCRIPTION='${interface_id}'"
194 echo "ORANGE_ADDRESS=${ipv4_address}"
195 echo "ORANGE_NETMASK=${netmask}"
196 echo "ORANGE_NETADDRESS=${netaddress}"
197 echo "ORANGE_MTU=${DEFAULT_MTU}"
198 ) >> /var/ipfire/ethernet/settings
204 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
206 # Actions performed only on the very first start
207 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
208 # Disable using ISP nameservers
209 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
212 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
214 # Disable SSH password authentication
215 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
217 # Enable SSH key authentication
218 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
221 /usr/local/bin/sshctrl
223 # Mark SSH to start immediately (but not right now)
224 touch /var/ipfire/remote/enablessh
225 chown nobody:nobody /var/ipfire/remote/enablessh
227 # Firewall rules for SSH and WEBIF
229 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
230 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
231 ) >> /var/ipfire/firewall/input
233 # This script has now completed the first steps of setup
234 touch /var/ipfire/main/firstsetup_ok
243 # Bring up the interface
244 ip link set "${interface}" up
247 BOUND|RENEW|REBIND|REBOOT)
248 # Remove any previous IP addresses
249 ip addr flush dev "${interface}"
251 # Add (or re-add) the new IP address
252 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
254 # Add the default route
255 ip route add "${new_routers}" dev "${interface}"
256 ip route add default via "${new_routers}"
259 for domain_name_server in ${new_domain_name_servers}; do
260 echo "nameserver ${domain_name_server}"
261 done > /etc/resolv.conf
263 # The system is online now
264 touch /var/ipfire/red/active
266 # Import GCP configuration
267 import_gcp_configuration
270 EXPIRE|FAIL|RELEASE|STOP)
271 # The system is no longer online
272 rm -f /var/ipfire/red/active
274 # Remove all IP addresses
275 ip addr flush dev "${interface}"
277 # Shut down the interface
278 ip link set "${interface}" down
282 echo "Unhandled reason: ${reason}" >&2