]>
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/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}..."
71 # Initialise system settings
72 local hostname=$(get local-hostname)
75 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
76 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
80 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
81 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
86 for line in $(get "public-keys/"); do
87 local key_no="${line%=*}"
89 local key="$(get public-keys/${key_no}/openssh-key)"
90 if [ -n "${key}" ] && ! grep -q "^${key}$" /root/.ssh/authorized_keys 2>/dev/null; then
94 echo "${key}" >> /root/.ssh/authorized_keys
95 chmod 600 /root/.ssh/authorized_keys
99 # Import any DNS server settings
100 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
102 # Import network configuration
103 # After this, no network connectivity will be available from this script due to the
104 # renaming of the network interfaces for which they have to be shut down
106 : > /var/ipfire/ethernet/settings
109 for mac in $(get network/interfaces/macs/); do
110 # Remove trailing slash
113 local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
114 local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
117 local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
118 local ipv4_address_num="$(to_integer "${ipv4_address}")"
121 local vpc="$(get "network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
122 local vpc_netaddress="${vpc%/*}"
123 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
126 local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
128 local prefix="${subnet#*/}"
129 local netmask="$(prefix2netmask "${prefix}")"
130 local netmask_num="$(to_integer "${netmask}")"
132 # Calculate the network and broadcast addresses
133 local netaddress="${subnet%/*}"
134 local netaddress_num="$(to_integer "${netaddress}")"
135 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
137 case "${device_number}" in
140 local interface_name="red0"
142 # The gateway is always the first IP address in the subnet
143 local gateway="$(to_address $(( netaddress_num + 1 )))"
145 # The AWS internal DNS service is available on the second IP address of the VPC
146 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
150 echo "RED_TYPE=STATIC"
151 echo "RED_DEV=${interface_name}"
152 echo "RED_MACADDR=${mac}"
153 echo "RED_DESCRIPTION='${interface_id}'"
154 echo "RED_ADDRESS=${ipv4_address}"
155 echo "RED_NETMASK=${netmask}"
156 echo "RED_NETADDRESS=${netaddress}"
157 echo "RED_BROADCAST=${broadcast}"
158 echo "DEFAULT_GATEWAY=${gateway}"
159 echo "DNS1=${DNS1:-${dns1}}"
160 echo "DNS2=${DNS2:-${dns2}}"
161 ) >> /var/ipfire/ethernet/settings
163 # Import aliases for RED
164 for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
166 done > /var/ipfire/ethernet/aliases
171 local interface_name="green0"
174 echo "GREEN_DEV=${interface_name}"
175 echo "GREEN_MACADDR=${mac}"
176 echo "GREEN_DESCRIPTION='${interface_id}'"
177 echo "GREEN_ADDRESS=${ipv4_address}"
178 echo "GREEN_NETMASK=${netmask}"
179 echo "GREEN_NETADDRESS=${netaddress}"
180 echo "GREEN_BROADCAST=${broadcast}"
181 ) >> /var/ipfire/ethernet/settings
186 local interface_name="orange0"
190 echo "ORANGE_DEV=${interface_name}"
191 echo "ORANGE_MACADDR=${mac}"
192 echo "ORANGE_DESCRIPTION='${interface_id}'"
193 echo "ORANGE_ADDRESS=${ipv4_address}"
194 echo "ORANGE_NETMASK=${netmask}"
195 echo "ORANGE_NETADDRESS=${netaddress}"
196 echo "ORANGE_BROADCAST=${broadcast}"
197 ) >> /var/ipfire/ethernet/settings
202 local interface="$(find_interface "${mac}")"
204 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
205 ip link set "${interface}" down
206 ip link set "${interface}" name "${interface_name}"
211 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
213 # Actions performed only on the very first start
214 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
216 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
218 touch /var/ipfire/remote/enablessh
219 chown nobody:nobody /var/ipfire/remote/enablessh
221 # Enable SSH key authentication
222 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
225 /usr/local/bin/sshctrl
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 default via "${new_routers}"
257 # Import AWS configuration
258 import_aws_configuration
261 EXPIRE|FAIL|RELEASE|STOP)
262 # Remove all IP addresses
263 ip addr flush dev "${interface}"
267 echo "Unhandled reason: ${reason}" >&2