]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/system/aws
aws: Add support for a script that can be executed at first boot
[people/pmueller/ipfire-2.x.git] / src / initscripts / system / aws
CommitLineData
1c21ebf8
MT
1#!/bin/sh
2########################################################################
3# Begin $rc_base/init.d/aws
4########################################################################
5
6. /etc/sysconfig/rc
7. ${rc_functions}
8
1c21ebf8
MT
9# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
10running_on_ec2() {
11 local uuid
12
13 # Check if the hypervisor UUID starts with ec2
14 if [ -r "/sys/hypervisor/uuid" ]; then
15 uuid=$(</sys/hypervisor/uuid)
16
17 [ "${uuid:0:3}" = "ec2" ] && return 0
18 fi
19
20 # Check if the DMI product UUID starts with EC2
21 if [ -r "/sys/devices/virtual/dmi/id/product_uuid" ]; then
22 uuid=$(</sys/devices/virtual/dmi/id/product_uuid)
23
24 [ "${uuid:0:3}" = "EC2" ] && return 0
25 fi
26
27 # We are not running on AWS EC2
28 return 1
29}
30
31case "${1}" in
32 start)
33 # Do nothing if we are not running on AWS EC2
464c2755 34 running_on_ec2 || exit 0
1c21ebf8
MT
35
36 # Find the first interface to use
37 for i in /sys/class/net/*; do
38 [ -d "${i}" ] || continue
39 i=$(basename ${i})
40
41 # Skip loopback
42 [ "${i}" = "lo" ] && continue
43
44 # Use whatever we have found
45 intf="${i}"
46 break
47 done
48
49 # Check if we found a network interface
50 if [ ! -n "${intf}" ]; then
51 echo_failure
52
53 boot_mesg -n "Could not find a network interface" ${FAILURE}
54 boot_mesg "" ${NORMAL}
55 fi
56
bd3bcb45
MT
57 # Run a DHCP client and set up the system accordingly
58 dhclient -sf /etc/rc.d/helper/aws-setup "${intf}"
1c21ebf8 59
bd3bcb45 60 # End DHCP client immediately
9a56118b 61 dhclient -sf /etc/rc.d/helper/aws-setup -r "${intf}" &>/dev/null
ba062943 62
4e4c122c
MT
63 # Run AWS user-data script
64 if [ -x "/tmp/aws-user-data.script" ]; then
65 /tmp/aws-user-data.script
66
67 # Delete the script right away
68 rm /tmp/aws-user-data.script
69 fi
70
ba062943 71 exit 0
1c21ebf8
MT
72 ;;
73
74 status)
75 if running_on_ec2; then
76 echo "This system is running on AWS EC2"
77 exit 0
78 else
79 echo "This system is NOT running on AWS EC2"
80 exit 1
81 fi
82 ;;
83
84 *)
85 echo "Usage: ${0} {start|status}"
86 exit 1
87 ;;
88esac
89
90# End $rc_base/init.d/aws