]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
6 # Set PATH to find our own executables
7 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
12 wget
-qO - "http://169.254.169.254/latest/${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 ))"
57 for path in /sys/class/net/*; do
58 local address="$(<${path}/address)"
60 if [ "${mac}" = "${address}" ]; then
69 import_aws_configuration() {
70 local instance_id="$(get meta-data/instance-id)"
72 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
75 echo "${instance_id}" > /var/run/aws-instance-id
77 # Initialise system settings
78 local hostname=$(get meta-data/local-hostname)
81 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
82 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
86 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
87 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
91 if ! getent passwd setup &>/dev/null; then
92 useradd setup -s /usr/bin/run-setup -g nobody -m
98 # Import SSH keys for setup user
100 for line in $(get "meta-data/public-keys/"); do
101 local key_no="${line%=*}"
103 local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
104 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
105 mkdir -p "/home/setup/.ssh"
106 chmod 700 "/home/setup/.ssh"
107 chown setup.nobody "/home/setup/.ssh"
109 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
110 chmod 600 "/home/setup/.ssh/authorized_keys"
111 chown setup.nobody "/home/setup/.ssh/authorized_keys"
115 # Download the user-data script only on the first boot
116 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
118 /etc/init.d/pakfire start
120 # Install all available updates
121 pakfire update && pakfire upgrade -y
123 # If an update requires a reboot, we will do it
124 if [ -e "/var/run/need_reboot" ]; then
130 local user_data="$(get user-data)"
132 # Save user-data script to be executed later
133 if [ "${user_data:0:2}" = "#!" ]; then
134 echo "${user_data}" > /tmp/aws-user-data.script
135 chmod 700 /tmp/aws-user-data.script
137 # Run the user-data script
138 /tmp/aws-user-data.script &>/root/user-data.log
140 # Delete the script right away
141 rm /tmp/aws-user-data.script
145 # Import any DNS server settings
146 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
148 # Import network configuration
149 # After this, no network connectivity will be available from this script due to the
150 # renaming of the network interfaces for which they have to be shut down
152 : > /var/ipfire/ethernet/settings
155 for mac in $(get meta-data/network/interfaces/macs/); do
156 # Remove trailing slash
159 local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
160 local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
163 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
164 local ipv4_address_num="$(to_integer "${ipv4_address}")"
167 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
168 local vpc_netaddress="${vpc%/*}"
169 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
172 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
174 local prefix="${subnet#*/}"
175 local netmask="$(prefix2netmask "${prefix}")"
176 local netmask_num="$(to_integer "${netmask}")"
178 # Calculate the network and broadcast addresses
179 local netaddress="${subnet%/*}"
180 local netaddress_num="$(to_integer "${netaddress}")"
181 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
183 case "${device_number}" in
186 local interface_name="red0"
188 # The gateway is always the first IP address in the subnet
189 local gateway="$(to_address $(( netaddress_num + 1 )))"
191 # The AWS internal DNS service is available on the second IP address of the VPC
192 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
196 echo "RED_TYPE=STATIC"
197 echo "RED_DEV=${interface_name}"
198 echo "RED_MACADDR=${mac}"
199 echo "RED_DESCRIPTION='${interface_id}'"
200 echo "RED_ADDRESS=${ipv4_address}"
201 echo "RED_NETMASK=${netmask}"
202 echo "RED_NETADDRESS=${netaddress}"
203 echo "RED_BROADCAST=${broadcast}"
204 echo "DEFAULT_GATEWAY=${gateway}"
205 echo "DNS1=${DNS1:-${dns1}}"
206 echo "DNS2=${DNS2:-${dns2}}"
207 ) >> /var/ipfire/ethernet/settings
209 # Import aliases for RED
210 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
212 done > /var/ipfire/ethernet/aliases
217 local interface_name="green0"
220 echo "GREEN_DEV=${interface_name}"
221 echo "GREEN_MACADDR=${mac}"
222 echo "GREEN_DESCRIPTION='${interface_id}'"
223 echo "GREEN_ADDRESS=${ipv4_address}"
224 echo "GREEN_NETMASK=${netmask}"
225 echo "GREEN_NETADDRESS=${netaddress}"
226 echo "GREEN_BROADCAST=${broadcast}"
227 ) >> /var/ipfire/ethernet/settings
232 local interface_name="orange0"
236 echo "ORANGE_DEV=${interface_name}"
237 echo "ORANGE_MACADDR=${mac}"
238 echo "ORANGE_DESCRIPTION='${interface_id}'"
239 echo "ORANGE_ADDRESS=${ipv4_address}"
240 echo "ORANGE_NETMASK=${netmask}"
241 echo "ORANGE_NETADDRESS=${netaddress}"
242 echo "ORANGE_BROADCAST=${broadcast}"
243 ) >> /var/ipfire/ethernet/settings
248 local interface="$(find_interface "${mac}")"
250 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
251 ip link set "${interface}" down
252 ip link set "${interface}" name "${interface_name}"
257 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
259 # Actions performed only on the very first start
260 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
262 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
264 # Disable SSH password authentication
265 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
267 # Enable SSH key authentication
268 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
271 /usr/local/bin/sshctrl
273 # Mark SSH to start immediately (but not right now)
274 touch /var/ipfire/remote/enablessh
275 chown nobody:nobody /var/ipfire/remote/enablessh
277 # Firewall rules for SSH and WEBIF
279 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
280 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
281 ) >> /var/ipfire/firewall/input
283 # This script has now completed the first steps of setup
284 touch /var/ipfire/main/firstsetup_ok
293 # Bring up the interface
294 ip link set "${interface}" up
297 BOUND|RENEW|REBIND|REBOOT)
298 # Remove any previous IP addresses
299 ip addr flush dev "${interface}"
301 # Add (or re-add) the new IP address
302 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
304 # Add the default route
305 ip route add default via "${new_routers}"
308 for domain_name_server in ${new_domain_name_servers}; do
309 echo "nameserver ${domain_name_server}"
310 done > /etc/resolv.conf
312 # The system is online now
313 touch /var/ipfire/red/active
315 # Import AWS configuration
316 import_aws_configuration
319 EXPIRE|FAIL|RELEASE|STOP)
320 # The system is no longer online
321 rm -f /var/ipfire/red/active
323 # Remove all IP addresses
324 ip addr flush dev "${interface}"
328 echo "Unhandled reason: ${reason}" >&2