]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
9 wget
-qO - "http://169.254.169.254/latest/${file}"
15 local o1
=$
(( (n
& 0xff000000) >> 24 ))
16 local o2
=$
(( (n
& 0xff0000) >> 16 ))
17 local o3
=$
(( (n
& 0xff00) >> 8 ))
18 local o4
=$
(( (n
& 0xff) ))
20 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
29 for i
in ${address//\./ }; do
30 integer
=$
(( (integer
<< 8) + i ))
33 printf "%d\n" "${integer}"
39 local zeros=$(( 32 - prefix ))
43 for (( i=0; i<${zeros}; i++ )); do
44 netmask=$(( (netmask << 1) ^ 1 ))
47 to_address "$(( netmask ^ 0xffffffff ))"
54 for path in /sys/class/net/*; do
55 local address="$(<${path}/address)"
57 if [ "${mac}" = "${address}" ]; then
66 import_aws_configuration() {
67 local instance_id="$(get meta-data/instance-id)"
69 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
72 echo "${instance_id}" > /var/run/aws-instance-id
74 # Initialise system settings
75 local hostname=$(get meta-data/local-hostname)
78 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
79 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
83 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
84 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
88 if ! getent passwd setup &>/dev/null; then
89 useradd setup -s /usr/bin/run-setup -g nobody -m
95 # Import SSH keys for setup user
97 for line in $(get "meta-data/public-keys/"); do
98 local key_no="${line%=*}"
100 local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
101 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
102 mkdir -p "/home/setup/.ssh"
103 chmod 700 "/home/setup/.ssh"
104 chown setup.nobody "/home/setup/.ssh"
106 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
107 chmod 600 "/home/setup/.ssh/authorized_keys"
108 chown setup.nobody "/home/setup/.ssh/authorized_keys"
112 # Download the user-data script only on the first boot
113 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
115 local user_data="$(get user-data)"
117 # Save user-data script to be executed later
118 if [ "${user_data:0:2}" = "#!" ]; then
119 echo "${user_data}" > /tmp/aws-user-data.script
120 chmod 700 /tmp/aws-user-data.script
122 # Run the user-data script
123 /tmp/aws-user-data.script
125 # Delete the script right away
126 rm /tmp/aws-user-data.script
130 # Import any DNS server settings
131 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
133 # Import network configuration
134 # After this, no network connectivity will be available from this script due to the
135 # renaming of the network interfaces for which they have to be shut down
137 : > /var/ipfire/ethernet/settings
140 for mac in $(get meta-data/network/interfaces/macs/); do
141 # Remove trailing slash
144 local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
145 local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
148 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
149 local ipv4_address_num="$(to_integer "${ipv4_address}")"
152 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
153 local vpc_netaddress="${vpc%/*}"
154 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
157 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
159 local prefix="${subnet#*/}"
160 local netmask="$(prefix2netmask "${prefix}")"
161 local netmask_num="$(to_integer "${netmask}")"
163 # Calculate the network and broadcast addresses
164 local netaddress="${subnet%/*}"
165 local netaddress_num="$(to_integer "${netaddress}")"
166 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
168 case "${device_number}" in
171 local interface_name="red0"
173 # The gateway is always the first IP address in the subnet
174 local gateway="$(to_address $(( netaddress_num + 1 )))"
176 # The AWS internal DNS service is available on the second IP address of the VPC
177 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
181 echo "RED_TYPE=STATIC"
182 echo "RED_DEV=${interface_name}"
183 echo "RED_MACADDR=${mac}"
184 echo "RED_DESCRIPTION='${interface_id}'"
185 echo "RED_ADDRESS=${ipv4_address}"
186 echo "RED_NETMASK=${netmask}"
187 echo "RED_NETADDRESS=${netaddress}"
188 echo "RED_BROADCAST=${broadcast}"
189 echo "DEFAULT_GATEWAY=${gateway}"
190 echo "DNS1=${DNS1:-${dns1}}"
191 echo "DNS2=${DNS2:-${dns2}}"
192 ) >> /var/ipfire/ethernet/settings
194 # Import aliases for RED
195 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
197 done > /var/ipfire/ethernet/aliases
202 local interface_name="green0"
205 echo "GREEN_DEV=${interface_name}"
206 echo "GREEN_MACADDR=${mac}"
207 echo "GREEN_DESCRIPTION='${interface_id}'"
208 echo "GREEN_ADDRESS=${ipv4_address}"
209 echo "GREEN_NETMASK=${netmask}"
210 echo "GREEN_NETADDRESS=${netaddress}"
211 echo "GREEN_BROADCAST=${broadcast}"
212 ) >> /var/ipfire/ethernet/settings
217 local interface_name="orange0"
221 echo "ORANGE_DEV=${interface_name}"
222 echo "ORANGE_MACADDR=${mac}"
223 echo "ORANGE_DESCRIPTION='${interface_id}'"
224 echo "ORANGE_ADDRESS=${ipv4_address}"
225 echo "ORANGE_NETMASK=${netmask}"
226 echo "ORANGE_NETADDRESS=${netaddress}"
227 echo "ORANGE_BROADCAST=${broadcast}"
228 ) >> /var/ipfire/ethernet/settings
233 local interface="$(find_interface "${mac}")"
235 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
236 ip link set "${interface}" down
237 ip link set "${interface}" name "${interface_name}"
242 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
244 # Actions performed only on the very first start
245 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
247 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
249 # Disable SSH password authentication
250 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
252 # Enable SSH key authentication
253 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
256 /usr/local/bin/sshctrl
258 # Mark SSH to start immediately (but not right now)
259 touch /var/ipfire/remote/enablessh
260 chown nobody:nobody /var/ipfire/remote/enablessh
262 # Firewall rules for SSH and WEBIF
264 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
265 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
266 ) >> /var/ipfire/firewall/input
268 # This script has now completed the first steps of setup
269 touch /var/ipfire/main/firstsetup_ok
278 # Bring up the interface
279 ip link set "${interface}" up
282 BOUND|RENEW|REBIND|REBOOT)
283 # Remove any previous IP addresses
284 ip addr flush dev "${interface}"
286 # Add (or re-add) the new IP address
287 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
289 # Add the default route
290 ip route add default via "${new_routers}"
293 for domain_name_server in ${new_domain_name_servers}; do
294 echo "nameserver ${domain_name_server}"
295 done > /etc/resolv.conf
297 # The system is online now
298 touch /var/ipfire/red/active
300 # Import AWS configuration
301 import_aws_configuration
304 EXPIRE|FAIL|RELEASE|STOP)
305 # The system is no longer online
306 rm -f /var/ipfire/red/active
308 # Remove all IP addresses
309 ip addr flush dev "${interface}"
313 echo "Unhandled reason: ${reason}" >&2