]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/gcp-setup
077a96dc6320146b8b9e3bb37521c83db8b67f57
6 # Set PATH to find our own executables
7 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
12 wget
--header="Metadata-Flavor: Google" -qO - "http://169.254.169.254/computeMetadata/v1/${file}"
18 local o1
=$
(( (n
& 0xff000000) >> 24 ))
19 local o2
=$
(( (n
& 0xff0000) >> 16 ))
20 local o3
=$
(( (n
& 0xff00) >> 8 ))
21 local o4
=$
(( (n
& 0xff) ))
23 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
32 for i
in ${address//\./ }; do
33 integer
=$
(( (integer
<< 8) + i ))
36 printf "%d\n" "${integer}"
42 local zeros=$(( 32 - prefix ))
46 for (( i=0; i<${zeros}; i++ )); do
47 netmask=$(( (netmask << 1) ^ 1 ))
50 to_address "$(( netmask ^ 0xffffffff ))"
53 import_gcp_configuration() {
54 local instance_id="$(get instance/id)"
56 boot_mesg "Importing Google Compute Platform configuration for instance ${instance_id}..."
59 echo "${instance_id}" > /var/run/gcp-instance-id
61 # Initialise system settings
62 local hostname=$(get instance/hostname)
65 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
66 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
70 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
71 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
75 if ! getent passwd setup &>/dev/null; then
76 useradd setup -s /usr/bin/run-setup -g nobody -m
82 # Import SSH keys for setup user
84 while read -r line; do
85 # Strip the username part from the key
86 local key="${line#*:}"
88 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
89 mkdir -p "/home/setup/.ssh"
90 chmod 700 "/home/setup/.ssh"
91 chown setup.nobody "/home/setup/.ssh"
93 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
94 chmod 600 "/home/setup/.ssh/authorized_keys"
95 chown setup.nobody "/home/setup/.ssh/authorized_keys"
97 done <<<"$(get instance/attributes/ssh-keys)"
99 # Download the user-data script only on the first boot
100 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
101 # Download a startup script
102 local script="$(get instance/attributes/startup-script)"
105 if [ "${script:0:2}" = "#!" ]; then
106 echo "${script}" > /tmp/gcp-startup.script
107 chmod 700 /tmp/gcp-startup.script
110 local now="$(date -u +"%s")"
111 /tmp/gcp-startup.script &>/var/log/startup-script.log.${now}
113 # Delete the script right away
114 rm /tmp/gcp-startup.script
118 # Import network configuration
119 # After this, no network connectivity will be available from this script due to the
120 # renaming of the network interfaces for which they have to be shut down
122 : > /var/ipfire/ethernet/settings
125 for device_number in $(get instance/network-interfaces/); do
126 # Remove trailing slash
127 device_number="${device_number//\//}"
129 local mac="$(get "instance/network-interfaces/${device_number}/mac")"
131 # XXX TODO read the MTU because Google seems to only support 1460
134 local ipv4_address="$(get "instance/network-interfaces/${device_number}/ip")"
135 local ipv4_address_num="$(to_integer "${ipv4_address}")"
137 local netmask="$(get "instance/network-interfaces/${device_number}/subnetmask")"
138 local netmask_num="$(to_integer "${netmask}")"
140 # Calculate the network and broadcast addresses
141 local netaddress="$(to_address $(( ipv4_address_num & netmask_num )))"
142 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
144 case "${device_number}" in
147 local interface_name="red0"
148 local gateway="$(get instance/network-interfaces/${device_number}/gateway)"
151 echo "RED_TYPE=STATIC"
152 echo "RED_DEV=${interface_name}"
153 echo "RED_MACADDR=${mac}"
154 echo "RED_DESCRIPTION='${interface_id}'"
155 echo "RED_ADDRESS=${ipv4_address}"
156 echo "RED_NETMASK=${netmask}"
157 echo "RED_NETADDRESS=${netaddress}"
158 echo "RED_BROADCAST=${broadcast}"
159 echo "DEFAULT_GATEWAY=${gateway}"
160 ) >> /var/ipfire/ethernet/settings
162 # Import aliases for RED
163 for alias in $(get "instance/network-interfaces/${device_number}/ip-aliases"); do
165 done > /var/ipfire/ethernet/aliases
170 local interface_name="green0"
173 echo "GREEN_DEV=${interface_name}"
174 echo "GREEN_MACADDR=${mac}"
175 echo "GREEN_DESCRIPTION='${interface_id}'"
176 echo "GREEN_ADDRESS=${ipv4_address}"
177 echo "GREEN_NETMASK=${netmask}"
178 echo "GREEN_NETADDRESS=${netaddress}"
179 echo "GREEN_BROADCAST=${broadcast}"
180 ) >> /var/ipfire/ethernet/settings
185 local interface_name="orange0"
189 echo "ORANGE_DEV=${interface_name}"
190 echo "ORANGE_MACADDR=${mac}"
191 echo "ORANGE_DESCRIPTION='${interface_id}'"
192 echo "ORANGE_ADDRESS=${ipv4_address}"
193 echo "ORANGE_NETMASK=${netmask}"
194 echo "ORANGE_NETADDRESS=${netaddress}"
195 echo "ORANGE_BROADCAST=${broadcast}"
196 ) >> /var/ipfire/ethernet/settings
202 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
204 # Actions performed only on the very first start
205 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
206 # Disable using ISP nameservers
207 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
210 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
212 # Disable SSH password authentication
213 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
215 # Enable SSH key authentication
216 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
219 /usr/local/bin/sshctrl
221 # Mark SSH to start immediately (but not right now)
222 touch /var/ipfire/remote/enablessh
223 chown nobody:nobody /var/ipfire/remote/enablessh
225 # Firewall rules for SSH and WEBIF
227 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
228 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
229 ) >> /var/ipfire/firewall/input
231 # This script has now completed the first steps of setup
232 touch /var/ipfire/main/firstsetup_ok
241 # Bring up the interface
242 ip link set "${interface}" up
245 BOUND|RENEW|REBIND|REBOOT)
246 # Remove any previous IP addresses
247 ip addr flush dev "${interface}"
249 # Add (or re-add) the new IP address
250 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
252 # Add the default route
253 ip route add default via "${new_routers}"
256 for domain_name_server in ${new_domain_name_servers}; do
257 echo "nameserver ${domain_name_server}"
258 done > /etc/resolv.conf
260 # The system is online now
261 touch /var/ipfire/red/active
263 # Import GCP configuration
264 import_gcp_configuration
267 EXPIRE|FAIL|RELEASE|STOP)
268 # The system is no longer online
269 rm -f /var/ipfire/red/active
271 # Remove all IP addresses
272 ip addr flush dev "${interface}"
274 # Shut down the interface
275 ip link set "${interface}" down
279 echo "Unhandled reason: ${reason}" >&2