]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
d8c7a358cb4015a65bf009b29956b7f1c194de53
9 wget
-qO - "http://169.254.169.254/latest/meta-data/${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 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 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 "public-keys/"); do
98 local key_no="${line%=*}"
100 local key="$(get 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 # Import any DNS server settings
113 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
115 # Import network configuration
116 # After this, no network connectivity will be available from this script due to the
117 # renaming of the network interfaces for which they have to be shut down
119 : > /var/ipfire/ethernet/settings
122 for mac in $(get network/interfaces/macs/); do
123 # Remove trailing slash
126 local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
127 local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
130 local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
131 local ipv4_address_num="$(to_integer "${ipv4_address}")"
134 local vpc="$(get "network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
135 local vpc_netaddress="${vpc%/*}"
136 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
139 local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
141 local prefix="${subnet#*/}"
142 local netmask="$(prefix2netmask "${prefix}")"
143 local netmask_num="$(to_integer "${netmask}")"
145 # Calculate the network and broadcast addresses
146 local netaddress="${subnet%/*}"
147 local netaddress_num="$(to_integer "${netaddress}")"
148 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
150 case "${device_number}" in
153 local interface_name="red0"
155 # The gateway is always the first IP address in the subnet
156 local gateway="$(to_address $(( netaddress_num + 1 )))"
158 # The AWS internal DNS service is available on the second IP address of the VPC
159 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
163 echo "RED_TYPE=STATIC"
164 echo "RED_DEV=${interface_name}"
165 echo "RED_MACADDR=${mac}"
166 echo "RED_DESCRIPTION='${interface_id}'"
167 echo "RED_ADDRESS=${ipv4_address}"
168 echo "RED_NETMASK=${netmask}"
169 echo "RED_NETADDRESS=${netaddress}"
170 echo "RED_BROADCAST=${broadcast}"
171 echo "DEFAULT_GATEWAY=${gateway}"
172 echo "DNS1=${DNS1:-${dns1}}"
173 echo "DNS2=${DNS2:-${dns2}}"
174 ) >> /var/ipfire/ethernet/settings
176 # Import aliases for RED
177 for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
179 done > /var/ipfire/ethernet/aliases
184 local interface_name="green0"
187 echo "GREEN_DEV=${interface_name}"
188 echo "GREEN_MACADDR=${mac}"
189 echo "GREEN_DESCRIPTION='${interface_id}'"
190 echo "GREEN_ADDRESS=${ipv4_address}"
191 echo "GREEN_NETMASK=${netmask}"
192 echo "GREEN_NETADDRESS=${netaddress}"
193 echo "GREEN_BROADCAST=${broadcast}"
194 ) >> /var/ipfire/ethernet/settings
199 local interface_name="orange0"
203 echo "ORANGE_DEV=${interface_name}"
204 echo "ORANGE_MACADDR=${mac}"
205 echo "ORANGE_DESCRIPTION='${interface_id}'"
206 echo "ORANGE_ADDRESS=${ipv4_address}"
207 echo "ORANGE_NETMASK=${netmask}"
208 echo "ORANGE_NETADDRESS=${netaddress}"
209 echo "ORANGE_BROADCAST=${broadcast}"
210 ) >> /var/ipfire/ethernet/settings
215 local interface="$(find_interface "${mac}")"
217 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
218 ip link set "${interface}" down
219 ip link set "${interface}" name "${interface_name}"
224 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
226 # Actions performed only on the very first start
227 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
229 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
231 # Enable SSH key authentication
232 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
235 /usr/local/bin/sshctrl
237 # Mark SSH to start immediately (but not right now)
238 touch /var/ipfire/remote/enablessh
239 chown nobody:nobody /var/ipfire/remote/enablessh
241 # Firewall rules for SSH and WEBIF
243 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
244 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
245 ) >> /var/ipfire/firewall/input
247 # This script has now completed the first steps of setup
248 touch /var/ipfire/main/firstsetup_ok
257 # Bring up the interface
258 ip link set "${interface}" up
261 BOUND|RENEW|REBIND|REBOOT)
262 # Remove any previous IP addresses
263 ip addr flush dev "${interface}"
265 # Add (or re-add) the new IP address
266 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
268 # Add the default route
269 ip route add default via "${new_routers}"
271 # Import AWS configuration
272 import_aws_configuration
275 EXPIRE|FAIL|RELEASE|STOP)
276 # Remove all IP addresses
277 ip addr flush dev "${interface}"
281 echo "Unhandled reason: ${reason}" >&2