]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/system/aws
Add initscript that automatically configures IPFire on AWS EC2
[ipfire-2.x.git] / src / initscripts / system / aws
1 #!/bin/sh
2 ########################################################################
3 # Begin $rc_base/init.d/aws
4 ########################################################################
5
6 . /etc/sysconfig/rc
7 . ${rc_functions}
8
9 MD_URL="http://169.254.169.254/latest/meta-data"
10
11 # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
12 running_on_ec2() {
13 local uuid
14
15 # Check if the hypervisor UUID starts with ec2
16 if [ -r "/sys/hypervisor/uuid" ]; then
17 uuid=$(</sys/hypervisor/uuid)
18
19 [ "${uuid:0:3}" = "ec2" ] && return 0
20 fi
21
22 # Check if the DMI product UUID starts with EC2
23 if [ -r "/sys/devices/virtual/dmi/id/product_uuid" ]; then
24 uuid=$(</sys/devices/virtual/dmi/id/product_uuid)
25
26 [ "${uuid:0:3}" = "EC2" ] && return 0
27 fi
28
29 # We are not running on AWS EC2
30 return 1
31 }
32
33 case "${1}" in
34 start)
35 # Do nothing if we are not running on AWS EC2
36 running_on_ec2 || exit 0
37
38 boot_mesg "Setting up system to run on AWS EC2..."
39
40 # Find the first interface to use
41 for i in /sys/class/net/*; do
42 [ -d "${i}" ] || continue
43 i=$(basename ${i})
44
45 # Skip loopback
46 [ "${i}" = "lo" ] && continue
47
48 # Use whatever we have found
49 intf="${i}"
50 break
51 done
52
53 # Check if we found a network interface
54 if [ ! -n "${intf}" ]; then
55 echo_failure
56
57 boot_mesg -n "Could not find a network interface" ${FAILURE}
58 boot_mesg "" ${NORMAL}
59 fi
60
61 # Assign ourselves an IP address to communicate with the meta-data service
62 ip addr add 169.254.169.1/24 dev "${intf}"
63 ip link set "${intf}" up
64
65 # Initialise system settings
66 if [ ! -s "/var/ipfire/main/settings" ]; then
67 hostname=$(curl ${MD_URL}/local-hostname)
68
69 (
70 echo "HOSTNAME=${hostname%%.*}"
71 echo "DOMAINNAME=${hostname#*.}"
72 echo "THEME=ipfire"
73 echo "LANGUAGE=en"
74 echo "KEYMAP=/lib/kbd/keymaps/i386/qwerty/us.map.gz"
75 echo "TIMEZONE=/usr/share/zoneinfo/posix/UTC"
76 ) > /var/ipfire/main/settings
77 fi
78
79 # Remove any IP addresses
80 ip addr flush dev "${intf}"
81 echo_ok
82
83 # This script has now completed the first steps of setup
84 touch /var/ipfire/main/firstsetup_ok
85 ;;
86
87 status)
88 if running_on_ec2; then
89 echo "This system is running on AWS EC2"
90 exit 0
91 else
92 echo "This system is NOT running on AWS EC2"
93 exit 1
94 fi
95 ;;
96
97 *)
98 echo "Usage: ${0} {start|status}"
99 exit 1
100 ;;
101 esac
102
103 # End $rc_base/init.d/aws