-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudstamp.sh
executable file
·79 lines (71 loc) · 2.3 KB
/
cloudstamp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
# Halt on any errors
set -e
# Constants
# The OS disk size
IMG_SIZE=20G
# The installer memory provision
# Careful: This will probably affect the image swap size
INST_MEM=2048
# Find the tools
QEMU_IMG_BIN=`which qemu-img`
# Attempt to find a qemu version we can use
if [ -e "/dev/kvm" ]; then
# We have KVM support, try to find a matching qemu
if [ `which qemu-kvm 2>&-` ]; then
QEMU_BIN=`which qemu-kvm`
elif [ -x "/usr/libexec/qemu-kvm" ]; then
QEMU_BIN=/usr/libexec/qemu-kvm
else
QEMU_BIN=`which qemu-system-x86_64`
fi
else
# No KVM support default to full emulation
QEMU_BIN=`which qemu-system-x86_64`
fi
GZIP_BIN=`which gzip`
TEE_BIN=`which tee`
# Check the arguments
if [ "$#" -ne "2" ]; then
echo "Usage: cloudstamp.sh ostype config " >&2
# TODO: Known types
exit 1
fi
OS_NAME="${1}"
CONF_NAME="${2}"
IMG_NAME=${CONF_NAME}-${OS_NAME}.img
OUTDIR=output
TMPDIR=${OUTDIR}/tmp-${CONF_NAME}-${OS_NAME}
# Check the config actually exists to prevent mistakes
if [ ! -f "config/nodes/${CONF_NAME}.pp" ]; then
echo "Error: config/nodes/${CONF_NAME}.pp not found." >&2
exit 1
fi
# Update our initrd
echo "[*] Injecting files into initrd..." >&2
rm -rf ${TMPDIR}
mkdir -p ${TMPDIR}
cp boot/${OS_NAME}_initrd.img ${TMPDIR}/initrd.tmp.gz
${GZIP_BIN} -d ${TMPDIR}/initrd.tmp.gz
find config -print | cpio --quiet -ocAO ${TMPDIR}/initrd.tmp
${GZIP_BIN} -9 ${TMPDIR}/initrd.tmp
echo "[*] Creating disk image..." >&2
${QEMU_IMG_BIN} create -f qcow2 "${TMPDIR}/${IMG_NAME}" ${IMG_SIZE} >&-
echo "[*] Booting Installer..." >&2
${QEMU_BIN} -nographic \
-m ${INST_MEM} \
-kernel boot/${OS_NAME}_vmlinuz \
-initrd ${TMPDIR}/initrd.tmp.gz \
-append "console=ttyS0 ks=file:/config/ks/${OS_NAME}.cfg ksc=${CONF_NAME}" \
-drive "file=${TMPDIR}/${IMG_NAME},if=virtio" \
-net nic,model=virtio -net user \
| ${TEE_BIN} ${OUTDIR}/${IMG_NAME}.log
echo "[*] Welcome back... Resetting your console in 3 seconds." >&2
sleep 3; reset
echo "[*] Compressing image..." >&2
${QEMU_IMG_BIN} convert -c -O qcow2 "${TMPDIR}/${IMG_NAME}" "${OUTDIR}/${IMG_NAME}"
echo "[*] Tidying up..." >&2
rm -rf ${TMPDIR}
echo "[*] Complete." >&2
echo "Your image file is here: ${OUTDIR}/${IMG_NAME}"
echo "Your build log file is here: ${OUTDIR}/${IMG_NAME}.log"