]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/helper/azure-setup
6 # Set PATH to find our own executables
7 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
12 wget
-qO - --header="Metadata:true" "http://169.254.169.254/metadata/instance/${file}?api-version=2019-06-01&format=text"
18 echo "${mac:0:2}:${mac:2:2}:${mac:4:2}:${mac:6:2}:${mac:8:2}:${mac:10:2}"
24 local o1
=$
(( (n
& 0xff000000) >> 24 ))
25 local o2
=$
(( (n
& 0xff0000) >> 16 ))
26 local o3
=$
(( (n
& 0xff00) >> 8 ))
27 local o4
=$
(( (n
& 0xff) ))
29 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
38 for i
in ${address//\./ }; do
39 integer
=$
(( (integer
<< 8) + i ))
42 printf "%d\n" "${integer}"
48 local zeros=$(( 32 - prefix ))
52 for (( i=0; i<${zeros}; i++ )); do
53 netmask=$(( (netmask << 1) ^ 1 ))
56 to_address "$(( netmask ^ 0xffffffff ))"
59 import_azure_configuration() {
60 local instance_id="$(get compute/vmId)"
62 boot_mesg "Importing Microsoft Azure configuration for instance ${instance_id}..."
65 echo "${instance_id}" > /var/run/azure-instance-id
67 # Initialise system settings
68 local hostname=$(get compute/name)
71 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
72 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
76 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
77 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
80 # Import SSH keys for setup user
82 for line in $(get "compute/publicKeys/"); do
83 # Remove trailing slash
84 local key_no="${line//\//}"
86 # Get the path where this key should be installed
87 local path="$(get "compute/publicKeys/${key_no}/path")"
88 local key="$(get "compute/publicKeys/${key_no}/keyData")"
91 if [[ "${path}" =~ ^/home ]]; then
95 # Cannot process this user
99 # Create user if it does not exist
100 if ! getent passwd "${user}" &>/dev/null; then
101 useradd "${user}" -s /usr/bin/run-setup -g nobody -m
104 usermod -p "x" "${user}"
107 if [ -n "${key}" ] && ! grep -q "^${key}$" "${path}" 2>/dev/null; then
108 local dir="$(dirname "${path}")"
113 chown "${user}.nobody" "${dir}"
116 echo "${key}" >> "${path}"
118 chown "${user}.nobody" "${path}"
122 # Download the user-data script only on the first boot
123 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
125 local user_data="$(get customData)"
127 # Save user-data script to be executed later
128 if [ "${user_data:0:2}" = "#!" ]; then
129 echo "${user_data}" > /tmp/azure-user-data.script
130 chmod 700 /tmp/azure-user-data.script
132 # Run the user-data script
133 local now="$(date -u +"%s")"
134 /tmp/azure-user-data.script &>/var/log/user-data.log.${now}
136 # Delete the script right away
137 rm /tmp/azure-user-data.script
141 # Import network configuration
142 # After this, no network connectivity will be available from this script due to the
143 # renaming of the network interfaces for which they have to be shut down
145 : > /var/ipfire/ethernet/settings
148 for device_number in $(get network/interface); do
149 # Remove trailing slash
150 device_number="${device_number//\//}"
152 local mac="$(get "network/interface/${device_number}/macAddress")"
153 mac="$(format_mac "${mac}")"
156 local ipv4_address="$(get "network/interface/${device_number}/ipv4/ipAddress/0/privateIpAddress")"
157 local ipv4_address_num="$(to_integer "${ipv4_address}")"
158 local prefix="$(get "network/interface/${device_number}/ipv4/subnet/0/prefix")"
159 local netmask="$(prefix2netmask "${prefix}")"
160 local netmask_num="$(to_integer "${netmask}")"
162 # Calculate the network and broadcast addresses
163 local netaddress="$(get "network/interface/${device_number}/ipv4/subnet/0/address")"
164 local netaddress_num="$(to_integer "${netaddress}")"
165 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
167 case "${device_number}" in
170 local interface_name="red0"
172 # The gateway is always the first IP address in the subnet
173 local gateway="$(to_address $(( netaddress_num + 1 )))"
176 echo "RED_TYPE=STATIC"
177 echo "RED_DEV=${interface_name}"
178 echo "RED_MACADDR=${mac}"
179 echo "RED_DESCRIPTION='${interface_id}'"
180 echo "RED_ADDRESS=${ipv4_address}"
181 echo "RED_NETMASK=${netmask}"
182 echo "RED_NETADDRESS=${netaddress}"
183 echo "RED_BROADCAST=${broadcast}"
184 echo "DEFAULT_GATEWAY=${gateway}"
185 ) >> /var/ipfire/ethernet/settings
187 # Import aliases for RED
189 for address_no in $(get "network/interface/0/ipv4/ipAddress"); do
190 # Remove trailing slash
191 address_no="${address_no//\//}"
193 # Skip the first address
194 [ "${address_no}" = "0" ] && continue
196 # Fetch the IP address
197 local alias="$(get "network/interface/0/ipv4/ipAddress/${address_no}/privateIpAddress")"
199 done > /var/ipfire/ethernet/aliases
204 local interface_name="green0"
207 echo "GREEN_DEV=${interface_name}"
208 echo "GREEN_MACADDR=${mac}"
209 echo "GREEN_DESCRIPTION='${interface_id}'"
210 echo "GREEN_ADDRESS=${ipv4_address}"
211 echo "GREEN_NETMASK=${netmask}"
212 echo "GREEN_NETADDRESS=${netaddress}"
213 echo "GREEN_BROADCAST=${broadcast}"
214 ) >> /var/ipfire/ethernet/settings
219 local interface_name="orange0"
223 echo "ORANGE_DEV=${interface_name}"
224 echo "ORANGE_MACADDR=${mac}"
225 echo "ORANGE_DESCRIPTION='${interface_id}'"
226 echo "ORANGE_ADDRESS=${ipv4_address}"
227 echo "ORANGE_NETMASK=${netmask}"
228 echo "ORANGE_NETADDRESS=${netaddress}"
229 echo "ORANGE_BROADCAST=${broadcast}"
230 ) >> /var/ipfire/ethernet/settings
236 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
238 # Actions performed only on the very first start
239 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
241 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
243 # Disable SSH password authentication
244 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
246 # Enable SSH key authentication
247 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
250 /usr/local/bin/sshctrl
252 # Mark SSH to start immediately (but not right now)
253 touch /var/ipfire/remote/enablessh
254 chown nobody:nobody /var/ipfire/remote/enablessh
256 # Firewall rules for SSH and WEBIF
258 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
259 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
260 ) >> /var/ipfire/firewall/input
262 # This script has now completed the first steps of setup
263 touch /var/ipfire/main/firstsetup_ok
272 # Bring up the interface
273 ip link set "${interface}" up
276 BOUND|RENEW|REBIND|REBOOT)
277 # Remove any previous IP addresses
278 ip addr flush dev "${interface}"
280 # Add (or re-add) the new IP address
281 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
283 # Add the default route
284 ip route add default via "${new_routers}"
287 for domain_name_server in ${new_domain_name_servers}; do
288 echo "nameserver ${domain_name_server}"
289 done > /etc/resolv.conf
291 # The system is online now
292 touch /var/ipfire/red/active
294 # Import Azure configuration
295 import_azure_configuration
298 EXPIRE|FAIL|RELEASE|STOP)
299 # The system is no longer online
300 rm -f /var/ipfire/red/active
302 # Remove all IP addresses
303 ip addr flush dev "${interface}"
305 # Shut down the interface
306 ip link set "${interface}" down
310 echo "Unhandled reason: ${reason}" >&2