From 8c7bbe82be9be6b6247c778ef78e54aa2fb37722 Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Tue, 17 Jun 2014 11:11:19 +0200 Subject: [PATCH] Enable autostart in osbash This change enables autostart functionality for osbash. activate_autostart is called by a minimal script written by preseed/kickstart files (if configured). It creates (and grants sudo privileges to) the osbash user if that has not been done by preseed/kickstart. It uses template-osbashauto to generate /etc/init.d/osbashauto and configures the system to run the script immediately and during every subsequent system boot. osbashauto iterates over all files in the autostart directory (in the VirtualBox shared folder; filled by osbash from the host side) and executes them as the osbash user. It writes its log files into the log directory in the shared folder. Partial-Bug: 1312764 Implements: blueprint openstack-training-labs Change-Id: I0394c5ea4e3f75a9d9bee30d119e047a762e86ce --- labs/scripts/osbash/activate_autostart.sh | 73 +++++++++++++++++++++++ labs/scripts/osbash/template-osbashauto | 54 +++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 labs/scripts/osbash/activate_autostart.sh create mode 100644 labs/scripts/osbash/template-osbashauto diff --git a/labs/scripts/osbash/activate_autostart.sh b/labs/scripts/osbash/activate_autostart.sh new file mode 100644 index 00000000..df333abc --- /dev/null +++ b/labs/scripts/osbash/activate_autostart.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# This script is the first to run and the only one to run as root. + +# XXX The name activate_autostart.sh is hard-coded in ks.cfg and preseed.cfg. + +# Remove files that called us (written by {ks,preseed}.cfg) +rm -f /etc/init.d/osbash /etc/rc2.d/S40osbash + +TOP_DIR=$(cd $(dirname "$0")/.. && pwd) +source "$TOP_DIR/config/paths" +# source_deploy doesn't work here +source "$CONFIG_DIR/deploy.osbash" +source "$LIB_DIR/functions.guest" + +readonly RCAUTOSTART=osbashauto + +indicate_current_auto + +# This guest script doesn't write to $HOME; the log file's extension is .auto +exec_logfile "$LOG_DIR" "auto" + +# Some guest additions leave a broken symlink /sbin/mount.vboxsf +as_root_fix_mount_vboxsf_link + +if ! id -u "$VM_SHELL_USER" >/dev/null 2>&1; then + # User doesn't exist -> add + useradd osbash -G vboxsf + echo "$VM_SHELL_USER:$VM_SHELL_USER" | chpasswd +elif ! id -Gn "$VM_SHELL_USER" >/dev/null 2>&1 | grep -q vboxsf; then + # User isn't in group vboxsf -> add + usermod -a -G vboxsf "$VM_SHELL_USER" +fi + +as_root_inject_sudoer + +if [ ! -f "$OSBASH_SCRIPTS_DIR/template-$RCAUTOSTART" ]; then + echo "Template not found: $OSBASH_SCRIPTS_DIR/template-$RCAUTOSTART" + exit 1 +fi + +# LOG_DIR and SHARE_DIR are based on the temporary mount point /media/sf_* +# which won't be there after reboot; use new paths for osbashauto + +NLOG_DIR="/$SHARE_NAME/$(basename "$LOG_DIR")" + +sed -e " + s,%SHARE_NAME%,$SHARE_NAME,g; + s,%VM_SHELL_USER%,$VM_SHELL_USER,g; + s,%NLOG_DIR%,$NLOG_DIR,g; + s,%RCAUTOSTART%,$RCAUTOSTART,g; + " "$OSBASH_SCRIPTS_DIR/template-$RCAUTOSTART" > "/etc/init.d/$RCAUTOSTART" + +chmod 755 "/etc/init.d/$RCAUTOSTART" + +if is_fedora; then + cat << SERVICE > /etc/systemd/system/$RCAUTOSTART.service +[Unit] +Description=OpenStack autostart +Requires=vboxadd-service.service +After=vboxadd-service.service vboxadd.service + +[Service] +ExecStart=/etc/init.d/$RCAUTOSTART + +[Install] +WantedBy=multi-user.target +SERVICE + + systemctl enable "$RCAUTOSTART.service" + systemctl start "$RCAUTOSTART.service" +else + ln -s "../init.d/$RCAUTOSTART" "/etc/rc2.d/S99$RCAUTOSTART" +fi diff --git a/labs/scripts/osbash/template-osbashauto b/labs/scripts/osbash/template-osbashauto new file mode 100644 index 00000000..216aad22 --- /dev/null +++ b/labs/scripts/osbash/template-osbashauto @@ -0,0 +1,54 @@ +#!/bin/bash + +# By default, this file is /etc/init.d/osbashauto on the guest system. +# On boot-up, it executes in order all files that have been put into +# the autostart folder. + +# The name of this file is hard-coded in activate_autostart.sh. + +SHARE_NAME=%SHARE_NAME% + +# Make sure we have a mount point for the shared directory +mkdir -p /$SHARE_NAME + +if ! mountpoint -q /$SHARE_NAME; then + mount -t vboxsf -ouid=%VM_SHELL_USER%,gid=%VM_SHELL_USER% $SHARE_NAME /$SHARE_NAME +fi + +# LOG_DIR is set in activate_autostart.sh as NLOG_DIR +LOG_DIR=%NLOG_DIR% + +STATUS_DIR=%NLOG_DIR%/status + +mkdir -p $STATUS_DIR + +TOP_DIR=/$SHARE_NAME + +source "$TOP_DIR/config/paths" +source "$LIB_DIR/functions.guest" + +exec_logpath "$LOG_DIR/%RCAUTOSTART%.log" + +echo "$(date) starting" + +shopt -s nullglob +for AUTODIR in "/$SHARE_NAME/autostart" "/$SHARE_NAME/autostart/$HOSTNAME"; do + if [ -d "$AUTODIR" ]; then + echo "$(date) autodir $AUTODIR" + for SCRIPT in $AUTODIR/*.sh; do + as_root_exec_script "$SCRIPT" + + # Remove script after execution + rm "$SCRIPT" + done + fi +done + +echo "$(date) autostart done" + +# This file is seen and removed by scripts running on the host +touch "$STATUS_DIR/done" + +exit 0 + +# vim: set ai ts=4 sw=4 et: