Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 3 | # 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 Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 13 | set -o errexit |
| 14 | set -o nounset |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 15 | set -o xtrace |
Renuka Apte | ce59d64 | 2012-02-02 16:09:23 -0800 | [diff] [blame] | 16 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 17 | # Configurable nuggets |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 18 | GUEST_PASSWORD="$1" |
Bob Ball | 5b9adb6 | 2015-02-10 08:09:08 +0000 | [diff] [blame] | 19 | STACK_USER="$2" |
| 20 | DOMZERO_USER="$3" |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 21 | |
| 22 | |
Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 23 | function setup_domzero_user { |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 24 | 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 |
| 39 | EOF |
| 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 |
| 45 | set -eux |
| 46 | |
| 47 | DOMID=\$(sudo xenstore-read domid) |
| 48 | sudo xenstore-exists /local/domain/\$DOMID/authorized_keys/$username |
| 49 | sudo xenstore-read /local/domain/\$DOMID/authorized_keys/$username > /home/$username/xenstore_value |
| 50 | cat /home/$username/xenstore_value > /home/$username/.ssh/authorized_keys |
| 51 | EOF |
| 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 |
| 68 | EOF |
| 69 | |
| 70 | } |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 71 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 72 | # Make a small cracklib dictionary, so that passwd still works, but we don't |
| 73 | # have the big dictionary. |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 74 | mkdir -p /usr/share/cracklib |
| 75 | echo a | cracklib-packer |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 76 | |
| 77 | # Make /etc/shadow, and set the root password |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 78 | pwconv |
| 79 | echo "root:$GUEST_PASSWORD" | chpasswd |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 80 | |
| 81 | # Put the VPX into UTC. |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 82 | rm -f /etc/localtime |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 83 | |
| 84 | # Add stack user |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 85 | groupadd libvirtd |
| 86 | useradd $STACK_USER -s /bin/bash -d /opt/stack -G libvirtd |
| 87 | echo $STACK_USER:$GUEST_PASSWORD | chpasswd |
| 88 | echo "$STACK_USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 89 | |
Mate Lakat | d15c8a0 | 2014-02-04 12:38:14 +0000 | [diff] [blame] | 90 | setup_domzero_user "$DOMZERO_USER" |
| 91 | |
Mate Lakat | b1dc9bd | 2013-08-29 11:52:20 +0100 | [diff] [blame] | 92 | # Add an udev rule, so that new block devices could be written by stack user |
| 93 | cat > /etc/udev/rules.d/50-openstack-blockdev.rules << EOF |
| 94 | KERNEL=="xvd[b-z]", GROUP="$STACK_USER", MODE="0660" |
| 95 | EOF |
| 96 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 97 | # Give ownership of /opt/stack to stack user |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 98 | chown -R $STACK_USER /opt/stack |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 99 | |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 100 | function setup_vimrc { |
| 101 | if [ ! -e $1 ]; then |
| 102 | # Simple but usable vimrc |
| 103 | cat > $1 <<EOF |
Anthony Young | b62b4ca | 2011-10-26 22:29:08 -0700 | [diff] [blame] | 104 | se ts=4 |
| 105 | se expandtab |
| 106 | se shiftwidth=4 |
| 107 | EOF |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | # Setup simple .vimrcs |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 112 | setup_vimrc /root/.vimrc |
| 113 | setup_vimrc /opt/stack/.vimrc |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 114 | |
| 115 | # remove self from local.rc |
| 116 | # so this script is not run again |
| 117 | rm -rf /etc/rc.local |
Mate Lakat | 0b3804b | 2013-05-07 16:58:17 +0100 | [diff] [blame] | 118 | |
| 119 | # Restore rc.local file |
| 120 | cp /etc/rc.local.preparebackup /etc/rc.local |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 121 | |
| 122 | # shutdown to notify we are done |
| 123 | shutdown -h now |