summaryrefslogtreecommitdiff
path: root/ec2-init
blob: bee4655b34e3a8ba156ba26bad591b32bcbac944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/sh
### BEGIN INIT INFO
# Provides:          ec2-init
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      $named
# Should-Stop:
# Default-Start:     S
# Default-Stop:      1
# Short-Description: Initialises system for use on Amazon EC2
# Description:       Fetches login credentials and handles various quirks
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=ec2-init

. /lib/lsb/init-functions

run_once() {
    per_id=$1
    action_id=$2

    semaphore="/var/lib/ec2/$action_id.$per_id"

    if ! [ -e "$semaphore" ]
    then
        touch "$semaphore"
        return 0
    fi
    return 1
}

run_once_per_ami() {
    action_id=$1
    ami=`ec2-get-info --ami-id | cut -f2 -d\ `
    run_once $ami $action_id
}

run_once_ever() {
    action_id=$1
    run_once ever $action_id
}

regenerate_ssh_host_keys() {
    rm -f /etc/ssh/ssh_host_*_key*

    ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''
    ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''

    # This allows user to get host keys securely through console log
    echo
    echo
    echo "#############################################################"
    echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----"
    ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub
    ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub
    echo "-----END SSH HOST KEY FINGERPRINTS-----"
    echo "#############################################################"
}

# fix LP bug 458850
# the ephemeral mounts provided in eucalyptus instances differ from
# those found in ec2 in 2 ways:
#   1. independent of arch type, the filesystem is on /dev/sda2
#   2. the filesystem is ext2, not ext3
fix_euca_fstab() {
    
    local edev="/dev/sda2" eedev='\/dev\/sda2' 

    [ -e "${edev}" ] || return 0

    local sops=""; # sed operations
    local mntinfo="" file_out="" sops="" umdev=${edev}

    # if /dev/sdb is set to mount to /mnt, then we
    # want to rewrite that to be /dev/sda2
    mntinfo=$(awk '$2 == "/mnt" { printf("dev=%s fs=%s\n",$1,$3); }' /etc/fstab)
    case "${mntinfo}" in
        dev=/dev/sdb\ *)
            umdev=/dev/sdb;
            sops="${sops:+${sops};}s,^/dev/sdb,${edev},";;
    esac

    # if fstab says ext3, but fs on edev is ext2, switch fstab
    case "${mntinfo}" in
        *\ fs=ext3)
            file_out=$(file --special-files "${edev}")
            case "${file_out}" in
                *ext2*) sops="${sops:+${sops};}/^${eedev}/s/ext3/ext2/;";;
            esac
            ;;
    esac

    # if there were no sed operations to preform, then nothing to do
    [ -n "${sops}" ] || return 0

    log_daemon_msg "Fixing fstab for eucalyptus"
    sed -i "${sops}" /etc/fstab
    # subsequent boots, /etc/fstab will be updated, and the mount
    # here isn't needed, but if modifications were made, it is
    umount "${edev}" >/dev/null 2>&1
    [ "${edev}" = "${umdev}" ] || umount "${umdev}" >/dev/null 2>&1
    mount "${edev}"
    log_end_msg $?
}

case "$1" in
    start)
        if ! ec2-is-compat-env --quiet; then
           log_daemon_msg "ec2-init disabled"
           log_end_msg 0
           exit 0
        fi
        if [ ! -d /var/run/ec2 ]; then
            mkdir /var/run/ec2
        fi

        log_daemon_msg "Waiting for EC2 meta-data service"
        if ec2-wait-for-meta-data-service
        then
            log_end_msg 0
        else
            log_end_msg 1
            exit 1
        fi

        # fix euca_fstab for ephemeral mounts one time ever
        # on rebundle, it should collect the fixed /etc/fstab
        if run_once_ever euca-fix-fstab-for-ephemeral; then
            fix_euca_fstab
        fi

        if run_once_per_ami ssh_host_key_regeneration
        then
            # we can't be certain that rsyslog is up (or configured to send
            # messages to console), but we want to make sure this goes to
            # console. So write to /dev/console directly through tee.
            # Change priority of message, so if user.notice (logger's default)
            # also goes to /dev/console , we could avoid dup messages
            regenerate_ssh_host_keys 2>&1 | 
                logger -p user.info -s -t "ec2" 2>&1 |
                tee /dev/console
        fi

        if run_once_ever ec2-defaults
        then
            log_daemon_msg "Setting EC2 defaults"
            if ec2-set-defaults 2> /dev/null
            then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        fi

        if run_once_per_ami ssh_authorized_keys
        then
            log_daemon_msg "Fetching EC2 SSH keys"
            if ec2-fetch-credentials 2> /dev/null
            then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        fi

        log_daemon_msg "Setting hostname to EC2 localhostname"
        if ec2-set-hostname 2> /dev/null
        then
            log_end_msg 0
            invoke-rc.d rsyslog reload
        else
            log_end_msg 1
        fi

        ;;
    stop)
        exit 0
        ;;
    restart|force-reload)
        exec $0 start
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0