blob: 6de1afc19957455d7370723e27c68752df3993f2 [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"
Bob Ball5b9adb62015-02-10 08:09:08 +000019STACK_USER="$2"
20DOMZERO_USER="$3"
Mate Lakatd15c8a02014-02-04 12:38:14 +000021
22
Ian Wienandaee18c72014-02-21 15:35:08 +110023function setup_domzero_user {
Mate Lakatd15c8a02014-02-04 12:38:14 +000024 local username
25
26 username="$1"
27
28 local key_updater_script
29 local sudoers_file
30 key_updater_script="/home/$username/update_authorized_keys.sh"
31 sudoers_file="/etc/sudoers.d/allow_$username"
32
33 # Create user
34 adduser --disabled-password --quiet "$username" --gecos "$username"
35
36 # Give passwordless sudo
37 cat > $sudoers_file << EOF
38 $username ALL = NOPASSWD: ALL
39EOF
40 chmod 0440 $sudoers_file
41
42 # A script to populate this user's authenticated_keys from xenstore
43 cat > $key_updater_script << EOF
44#!/bin/bash
45set -eux
46
47DOMID=\$(sudo xenstore-read domid)
48sudo xenstore-exists /local/domain/\$DOMID/authorized_keys/$username
49sudo xenstore-read /local/domain/\$DOMID/authorized_keys/$username > /home/$username/xenstore_value
50cat /home/$username/xenstore_value > /home/$username/.ssh/authorized_keys
51EOF
52
53 # Give the key updater to the user
54 chown $username:$username $key_updater_script
55 chmod 0700 $key_updater_script
56
57 # Setup the .ssh folder
58 mkdir -p /home/$username/.ssh
59 chown $username:$username /home/$username/.ssh
60 chmod 0700 /home/$username/.ssh
61 touch /home/$username/.ssh/authorized_keys
62 chown $username:$username /home/$username/.ssh/authorized_keys
63 chmod 0600 /home/$username/.ssh/authorized_keys
64
65 # Setup the key updater as a cron job
66 crontab -u $username - << EOF
67* * * * * $key_updater_script
68EOF
69
70}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070071
Anthony Youngb62b4ca2011-10-26 22:29:08 -070072# Make a small cracklib dictionary, so that passwd still works, but we don't
73# have the big dictionary.
Mate Lakat0b3804b2013-05-07 16:58:17 +010074mkdir -p /usr/share/cracklib
75echo a | cracklib-packer
Anthony Youngb62b4ca2011-10-26 22:29:08 -070076
77# Make /etc/shadow, and set the root password
Mate Lakat0b3804b2013-05-07 16:58:17 +010078pwconv
79echo "root:$GUEST_PASSWORD" | chpasswd
Anthony Youngb62b4ca2011-10-26 22:29:08 -070080
81# Put the VPX into UTC.
Mate Lakat0b3804b2013-05-07 16:58:17 +010082rm -f /etc/localtime
Anthony Youngb62b4ca2011-10-26 22:29:08 -070083
84# Add stack user
Mate Lakat0b3804b2013-05-07 16:58:17 +010085groupadd libvirtd
86useradd $STACK_USER -s /bin/bash -d /opt/stack -G libvirtd
87echo $STACK_USER:$GUEST_PASSWORD | chpasswd
88echo "$STACK_USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
Anthony Youngb62b4ca2011-10-26 22:29:08 -070089
Mate Lakatd15c8a02014-02-04 12:38:14 +000090setup_domzero_user "$DOMZERO_USER"
91
Mate Lakatb1dc9bd2013-08-29 11:52:20 +010092# Add an udev rule, so that new block devices could be written by stack user
93cat > /etc/udev/rules.d/50-openstack-blockdev.rules << EOF
94KERNEL=="xvd[b-z]", GROUP="$STACK_USER", MODE="0660"
95EOF
96
Anthony Youngb62b4ca2011-10-26 22:29:08 -070097# Give ownership of /opt/stack to stack user
Mate Lakat0b3804b2013-05-07 16:58:17 +010098chown -R $STACK_USER /opt/stack
Anthony Youngb62b4ca2011-10-26 22:29:08 -070099
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700100function setup_vimrc {
101 if [ ! -e $1 ]; then
102 # Simple but usable vimrc
103 cat > $1 <<EOF
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700104se ts=4
105se expandtab
106se shiftwidth=4
107EOF
108 fi
109}
110
111# Setup simple .vimrcs
Mate Lakat0b3804b2013-05-07 16:58:17 +0100112setup_vimrc /root/.vimrc
113setup_vimrc /opt/stack/.vimrc
John Garbuttdaadf742012-04-27 18:28:28 +0100114
115# remove self from local.rc
116# so this script is not run again
117rm -rf /etc/rc.local
Mate Lakat0b3804b2013-05-07 16:58:17 +0100118
119# Restore rc.local file
120cp /etc/rc.local.preparebackup /etc/rc.local
John Garbuttdaadf742012-04-27 18:28:28 +0100121
122# shutdown to notify we are done
123shutdown -h now