Skip to content

Instantly share code, notes, and snippets.

@smoser

smoser/README

Created Jul 13, 2015
Embed
What would you like to do?
loop reboot cloud image
this is just some hacky user-datas that I put together to try to catch failure as in bug 1473527 (http://pad.lv/1473527) . Doing it helped me see ppc64el image bug 1474090 (http://pad.lv/1474090)
very un-finished.
things here:
a.) reboot-loop-userdata.txt
this reboots in a loop up to NUM_BOOTS times
youneed to replace NUM_BOOTS
b.) go-loop
this downloads an image, boots it and runs with logging in a loop.
#!/bin/bash
set -e
pkgs=""
which cloud-localds >/dev/null || pkgs="$pkgs cloud-image-utils"
which qemu-system-ppc64 >/dev/null || pkgs="$pkgs qemu-system-ppc64"
which qemu-img >/dev/null || pkgs="$pkgs qemu-utils"
[ -z "${pkgs# }" ] || { echo "need install : $pkgs"; exit 1; }
rel="wily"
burl="http://cloud-images.ubuntu.com/$rel/current/"
disk_url="${1:-${burl}/$rel-server-cloudimg-amd64-disk1.img}"
disk_img="$rel.img"
disk_raw="$rel.raw"
disk_boot="disk.img"
seed_img="seed.img"
if [ ! -f "$disk_img" ]; then
wget "$disk_url" -O "$disk_img.tmp" && mv "$disk_img.tmp" "$disk_img"
fi
if [ ! -f "$disk_raw" ]; then
echo "converting $disk_img to $disk_raw"
qemu-img convert -O raw "$disk_img" "$disk_raw.tmp" &&
mv "$disk_raw.tmp" "$disk_raw"
fi
if [ -f "$HOME/.ssh/id_rsa.pub" ]; then
keyfile=$HOME/.ssh/id_rsa.pub
else
[ -f ./test-id_rsa.pub ] || ssh-keygen -N '' -f ./test-id_rsa
keyfile="$PWD/test-id_rsa.pub"
fi
pubkey=$(cat $keyfile)
cat > meta-data <<EOF
instance-id: $(uuidgen || echo i-abcdefg)
public-keys:
- "$pubkey"
EOF
boot_system() {
local disk="$1" seed="$2" name="$3"
qemu-system-x86_64 -enable-kvm \
-device virtio-net-pci,netdev=net00 -netdev type=user,id=net00 \
-drive if=virtio,file=$disk -drive if=virtio,file=$seed \
-nographic -m 1024 ${name:+-name "$name"}
}
KID=""
cleanup() {
kill $KID
}
trap cleanup EXIT
myvmprefix="foo-"
while :; do
i=$(($i+1))
log=$(printf "log-%04d" "$i")
#cloud-localds "$seed_img" user-data meta-data
cloud-localds "$seed_img" reboot-loop-userdata.txt meta-data
out=$(qemu-img create -f qcow2 -b "$disk_raw" "$disk_boot" 2>&1) || { echo "failed: $out"; exit 1; }
echo "=== $(date -R) $log ==="
sstart="$SECONDS"
ln -sf "$log" current.log
name=${myvmprefix}${log#log-}
boot_system "$disk_boot" "$seed_img" "$name" > "$log" 2>&1 &
KID=$!
wait
ret=$?
echo "=== $(date -R) $log $ret [$(($SECONDS-$sstart))s]"
[ $ret -eq 0 ] || exit $ret
done
#cloud-config
##
## This user-data will boot a system NUM_BOOTS times
sm_misc:
- &enable_reboot_or_message |
f=/usr/local/bin/reboot-or-message
pbdir="/var/lib/cloud/scripts/per-boot/"
[ -f "$f" -a -x "$pbdir/${f##*/}" ] && exit 0
[ -d "$pbdir" ] || mkdir -p "$pbdir"
[ -e "$pbdir/${f##*/}" ] || ln -sf "$f" "$pbdir/${f##*/}"
[ -f "$f" ] || cat > "$f" <<"EOF"
#!/bin/bash
me="${0##*/}"
datafile="/$me.mark"
tsfile="/$me.tslog"
LOG="/var/log/$me.log"
MSG_LOG="$LOG"
NUM_BOOTS=2
DELAY=3s
PATH=$PATH:/usr/local/bin
set -f
set_consoles() {
local cmdline="" tok=""
CONSOLES=""
read cmdline </proc/cmdline
for tok in $cmdline; do
[ "${tok#console=}" = "$tok" ] && continue
tok=${tok#console=}
tok=${tok#/dev}
tok=${tok%%,*}
CONSOLES="$CONSOLES /dev/$tok"
done
CONSOLES=${CONSOLES# }
[ -z "$CONSOLES" ] && CONSOLES="/dev/console"
}
msg() {
local c="" now=""
now="$(date -R)"
echo "[stdout] $now" "$@"
for c in ${CONSOLES} ${MSG_LOG}; do
echo "[$c] $now" "$@" >> "$c"
done
}
fatal_hang() {
echo "$$" "$1"
sleep 100h
exit 99
}
do_reboot() {
local id="$1" delay="${2:-3}" op="${3:-reboot}"
exec >>"$LOG" 2>&1 </dev/null
[ $? -eq 0 ] || fatal_hang "[$id] FAIL FAIL FAIL FAILED REDIRECT"
MSG_LOG=""
msg "====== [$id] $op after $delay ======"
sleep $delay || fatal_hang "[$id] SLEEP FAILED? REALLY?"
msg "====== [$id] slept $delay $op now ====="
"$op"
ret=$?
msg "====== $op returned $ret ====="
sync
sleep 2m &&
fatal_hang "[$id] WHY AM I STILL AROUND"
exit
}
set_consoles
if [ ! -f "$datafile" ]; then
msg ====== first boot. =======
echo "1" > "$datafile"
: > "$tsfile"
num=1
else
read num < "$datafile"
num=$(($num+1))
echo $num > "$datafile"
fi
read uptime idle < /proc/uptime
now=$(date +%s)
printf "%04d %s %s\n" "$num" "$uptime" "$now" >> "$tsfile"
if [ "$num" -ge "$NUM_BOOTS" ]; then
msg "success [$num boots]"
do_reboot "finished" "$DELAY" poweroff & </dev/null >/dev/null 2>&1
exit 0
fi
do_reboot "$num" $DELAY </dev/null >/dev/null 2>&1 &
EOF
chmod 755 "$f"
bootcmd:
- [sh, -c, *enable_reboot_or_message ]
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment