blob: 2b5e418a6a581ac4c31ade9584c5452b7748322d [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
Joe Gordonbb1e0782014-03-06 09:40:27 -080076apt-get install -y curl wget ssh openssh-server python-pip git sudo python-netaddr
Mate Lakat0b3804b2013-05-07 16:58:17 +010077pip install xenapi
Anthony Youngb62b4ca2011-10-26 22:29:08 -070078
John Garbuttdaadf742012-04-27 18:28:28 +010079# Install XenServer guest utilities
Mate Lakat0b3804b2013-05-07 16:58:17 +010080dpkg -i $XS_TOOLS_PATH
81update-rc.d -f xe-linux-distribution remove
82update-rc.d xe-linux-distribution defaults
Anthony Youngb62b4ca2011-10-26 22:29:08 -070083
84# Make a small cracklib dictionary, so that passwd still works, but we don't
85# have the big dictionary.
Mate Lakat0b3804b2013-05-07 16:58:17 +010086mkdir -p /usr/share/cracklib
87echo a | cracklib-packer
Anthony Youngb62b4ca2011-10-26 22:29:08 -070088
89# Make /etc/shadow, and set the root password
Mate Lakat0b3804b2013-05-07 16:58:17 +010090pwconv
91echo "root:$GUEST_PASSWORD" | chpasswd
Anthony Youngb62b4ca2011-10-26 22:29:08 -070092
93# Put the VPX into UTC.
Mate Lakat0b3804b2013-05-07 16:58:17 +010094rm -f /etc/localtime
Anthony Youngb62b4ca2011-10-26 22:29:08 -070095
96# Add stack user
Mate Lakat0b3804b2013-05-07 16:58:17 +010097groupadd libvirtd
98useradd $STACK_USER -s /bin/bash -d /opt/stack -G libvirtd
99echo $STACK_USER:$GUEST_PASSWORD | chpasswd
100echo "$STACK_USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700101
Mate Lakatd15c8a02014-02-04 12:38:14 +0000102setup_domzero_user "$DOMZERO_USER"
103
Mate Lakatb1dc9bd2013-08-29 11:52:20 +0100104# Add an udev rule, so that new block devices could be written by stack user
105cat > /etc/udev/rules.d/50-openstack-blockdev.rules << EOF
106KERNEL=="xvd[b-z]", GROUP="$STACK_USER", MODE="0660"
107EOF
108
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700109# Give ownership of /opt/stack to stack user
Mate Lakat0b3804b2013-05-07 16:58:17 +0100110chown -R $STACK_USER /opt/stack
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700111
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700112function setup_vimrc {
113 if [ ! -e $1 ]; then
114 # Simple but usable vimrc
115 cat > $1 <<EOF
116syntax on
117se ts=4
118se expandtab
119se shiftwidth=4
120EOF
121 fi
122}
123
124# Setup simple .vimrcs
Mate Lakat0b3804b2013-05-07 16:58:17 +0100125setup_vimrc /root/.vimrc
126setup_vimrc /opt/stack/.vimrc
John Garbuttdaadf742012-04-27 18:28:28 +0100127
128# remove self from local.rc
129# so this script is not run again
130rm -rf /etc/rc.local
Mate Lakat0b3804b2013-05-07 16:58:17 +0100131
132# Restore rc.local file
133cp /etc/rc.local.preparebackup /etc/rc.local
John Garbuttdaadf742012-04-27 18:28:28 +0100134
135# shutdown to notify we are done
136shutdown -h now