blob: 7fe032a0648ff11a099a567f8574e8febb863081 [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2
John Garbuttdaadf742012-04-27 18:28:28 +01003# This script is run on an Ubuntu VM.
4# This script is inserted into the VM by prepare_guest_template.sh
5# and is run when that VM boots.
6# It customizes a fresh Ubuntu install, so it is ready
7# to run stack.sh
8#
9# This includes installing the XenServer tools,
10# creating the user called "stack",
11# and shuts down the VM to signal the script has completed
12
Mate Lakat0b3804b2013-05-07 16:58:17 +010013set -o errexit
14set -o nounset
John Garbuttdaadf742012-04-27 18:28:28 +010015set -o xtrace
Renuka Aptece59d642012-02-02 16:09:23 -080016
Anthony Youngb62b4ca2011-10-26 22:29:08 -070017# Configurable nuggets
Mate Lakat0b3804b2013-05-07 16:58:17 +010018GUEST_PASSWORD="$1"
19XS_TOOLS_PATH="$2"
20STACK_USER="$3"
Mate Lakatd15c8a02014-02-04 12:38:14 +000021DOMZERO_USER="$4"
22
23
Ian Wienandaee18c72014-02-21 15:35:08 +110024function setup_domzero_user {
Mate Lakatd15c8a02014-02-04 12:38:14 +000025 local username
26
27 username="$1"
28
29 local key_updater_script
30 local sudoers_file
31 key_updater_script="/home/$username/update_authorized_keys.sh"
32 sudoers_file="/etc/sudoers.d/allow_$username"
33
34 # Create user
35 adduser --disabled-password --quiet "$username" --gecos "$username"
36
37 # Give passwordless sudo
38 cat > $sudoers_file << EOF
39 $username ALL = NOPASSWD: ALL
40EOF
41 chmod 0440 $sudoers_file
42
43 # A script to populate this user's authenticated_keys from xenstore
44 cat > $key_updater_script << EOF
45#!/bin/bash
46set -eux
47
48DOMID=\$(sudo xenstore-read domid)
49sudo xenstore-exists /local/domain/\$DOMID/authorized_keys/$username
50sudo xenstore-read /local/domain/\$DOMID/authorized_keys/$username > /home/$username/xenstore_value
51cat /home/$username/xenstore_value > /home/$username/.ssh/authorized_keys
52EOF
53
54 # Give the key updater to the user
55 chown $username:$username $key_updater_script
56 chmod 0700 $key_updater_script
57
58 # Setup the .ssh folder
59 mkdir -p /home/$username/.ssh
60 chown $username:$username /home/$username/.ssh
61 chmod 0700 /home/$username/.ssh
62 touch /home/$username/.ssh/authorized_keys
63 chown $username:$username /home/$username/.ssh/authorized_keys
64 chmod 0600 /home/$username/.ssh/authorized_keys
65
66 # Setup the key updater as a cron job
67 crontab -u $username - << EOF
68* * * * * $key_updater_script
69EOF
70
71}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070072
Anthony Youngb62b4ca2011-10-26 22:29:08 -070073# Install basics
Mate Lakat0b3804b2013-05-07 16:58:17 +010074apt-get update
75apt-get install -y cracklib-runtime curl wget ssh openssh-server tcpdump ethtool
Bob Ballbe485222014-12-10 10:33:53 +000076apt-get install -y git sudo python-netaddr coreutils
Anthony Youngb62b4ca2011-10-26 22:29:08 -070077
John Garbuttdaadf742012-04-27 18:28:28 +010078# Install XenServer guest utilities
Mate Lakat0b3804b2013-05-07 16:58:17 +010079dpkg -i $XS_TOOLS_PATH
80update-rc.d -f xe-linux-distribution remove
81update-rc.d xe-linux-distribution defaults
Anthony Youngb62b4ca2011-10-26 22:29:08 -070082
83# Make a small cracklib dictionary, so that passwd still works, but we don't
84# have the big dictionary.
Mate Lakat0b3804b2013-05-07 16:58:17 +010085mkdir -p /usr/share/cracklib
86echo a | cracklib-packer
Anthony Youngb62b4ca2011-10-26 22:29:08 -070087
88# Make /etc/shadow, and set the root password
Mate Lakat0b3804b2013-05-07 16:58:17 +010089pwconv
90echo "root:$GUEST_PASSWORD" | chpasswd
Anthony Youngb62b4ca2011-10-26 22:29:08 -070091
92# Put the VPX into UTC.
Mate Lakat0b3804b2013-05-07 16:58:17 +010093rm -f /etc/localtime
Anthony Youngb62b4ca2011-10-26 22:29:08 -070094
95# Add stack user
Mate Lakat0b3804b2013-05-07 16:58:17 +010096groupadd libvirtd
97useradd $STACK_USER -s /bin/bash -d /opt/stack -G libvirtd
98echo $STACK_USER:$GUEST_PASSWORD | chpasswd
99echo "$STACK_USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700100
Mate Lakatd15c8a02014-02-04 12:38:14 +0000101setup_domzero_user "$DOMZERO_USER"
102
Mate Lakatb1dc9bd2013-08-29 11:52:20 +0100103# Add an udev rule, so that new block devices could be written by stack user
104cat > /etc/udev/rules.d/50-openstack-blockdev.rules << EOF
105KERNEL=="xvd[b-z]", GROUP="$STACK_USER", MODE="0660"
106EOF
107
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700108# Give ownership of /opt/stack to stack user
Mate Lakat0b3804b2013-05-07 16:58:17 +0100109chown -R $STACK_USER /opt/stack
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700110
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700111function setup_vimrc {
112 if [ ! -e $1 ]; then
113 # Simple but usable vimrc
114 cat > $1 <<EOF
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700115se ts=4
116se expandtab
117se shiftwidth=4
118EOF
119 fi
120}
121
122# Setup simple .vimrcs
Mate Lakat0b3804b2013-05-07 16:58:17 +0100123setup_vimrc /root/.vimrc
124setup_vimrc /opt/stack/.vimrc
John Garbuttdaadf742012-04-27 18:28:28 +0100125
126# remove self from local.rc
127# so this script is not run again
128rm -rf /etc/rc.local
Mate Lakat0b3804b2013-05-07 16:58:17 +0100129
130# Restore rc.local file
131cp /etc/rc.local.preparebackup /etc/rc.local
John Garbuttdaadf742012-04-27 18:28:28 +0100132
133# shutdown to notify we are done
134shutdown -h now