Anthony Young | c1024d8 | 2011-11-09 18:58:07 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Echo commands |
| 4 | set -o xtrace |
| 5 | |
| 6 | # Exit on error to stop unexpected errors |
| 7 | set -o errexit |
| 8 | |
| 9 | # Keep track of the current directory |
| 10 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 11 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
| 12 | |
| 13 | # Change dir to top of devstack |
| 14 | cd $TOP_DIR |
| 15 | |
| 16 | # Echo usage |
| 17 | usage() { |
| 18 | echo "Add stack user and keys" |
| 19 | echo "" |
| 20 | echo "Usage: $0 [full path to raw uec base image]" |
| 21 | } |
| 22 | |
| 23 | # Make sure this is a raw image |
| 24 | if ! qemu-img info $1 | grep -q "file format: raw"; then |
| 25 | usage |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | # Mount the image |
| 30 | DEST=/opt/stack |
| 31 | STAGING_DIR=/tmp/`echo $1 | sed "s/\//_/g"`.stage.user |
| 32 | mkdir -p $STAGING_DIR |
| 33 | umount $STAGING_DIR || true |
| 34 | sleep 1 |
| 35 | mount -t ext4 -o loop $1 $STAGING_DIR |
| 36 | mkdir -p $STAGING_DIR/$DEST |
| 37 | |
| 38 | # Create a stack user that is a member of the libvirtd group so that stack |
| 39 | # is able to interact with libvirt. |
| 40 | chroot $STAGING_DIR groupadd libvirtd || true |
| 41 | chroot $STAGING_DIR useradd stack -s /bin/bash -d $DEST -G libvirtd || true |
| 42 | |
Anthony Young | e228093 | 2011-11-09 22:29:22 -0800 | [diff] [blame] | 43 | # Add a simple password - pass |
Anthony Young | c1024d8 | 2011-11-09 18:58:07 -0800 | [diff] [blame] | 44 | echo stack:pass | chroot $STAGING_DIR chpasswd |
| 45 | |
Anthony Young | e228093 | 2011-11-09 22:29:22 -0800 | [diff] [blame] | 46 | # Configure sudo |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 47 | ( umask 226 && echo "stack ALL=(ALL) NOPASSWD:ALL" \ |
| 48 | > $STAGING_DIR/etc/sudoers.d/50_stack_sh ) |
Anthony Young | e228093 | 2011-11-09 22:29:22 -0800 | [diff] [blame] | 49 | |
Anthony Young | c1024d8 | 2011-11-09 18:58:07 -0800 | [diff] [blame] | 50 | # Gracefully cp only if source file/dir exists |
| 51 | function cp_it { |
| 52 | if [ -e $1 ] || [ -d $1 ]; then |
| 53 | cp -pRL $1 $2 |
| 54 | fi |
| 55 | } |
| 56 | |
| 57 | # Copy over your ssh keys and env if desired |
| 58 | cp_it ~/.ssh $STAGING_DIR/$DEST/.ssh |
| 59 | cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/$DEST/.ssh/authorized_keys |
| 60 | cp_it ~/.gitconfig $STAGING_DIR/$DEST/.gitconfig |
| 61 | cp_it ~/.vimrc $STAGING_DIR/$DEST/.vimrc |
| 62 | cp_it ~/.bashrc $STAGING_DIR/$DEST/.bashrc |
| 63 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 64 | # Copy devstack |
| 65 | rm -rf $STAGING_DIR/$DEST/devstack |
| 66 | cp_it . $STAGING_DIR/$DEST/devstack |
| 67 | |
Anthony Young | c1024d8 | 2011-11-09 18:58:07 -0800 | [diff] [blame] | 68 | # Give stack ownership over $DEST so it may do the work needed |
| 69 | chroot $STAGING_DIR chown -R stack $DEST |
| 70 | |
Anthony Young | e228093 | 2011-11-09 22:29:22 -0800 | [diff] [blame] | 71 | # Unmount |
| 72 | umount $STAGING_DIR |