-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare-golden.sh
executable file
·117 lines (97 loc) · 3.5 KB
/
prepare-golden.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
# set -x
DISTRO=$1
CWD="/var/lib/libvirt/images"
USER="sub"
FILENAME="/home/sub/authorized_keys"
VIRSH=$(which virsh)
SYSPREP=$(which virt-sysprep)
GUESTFISH=$(which guestfish)
usage () {
echo "usage: $0 [distro]"
}
if [ -z $1 ]; then
usage
exit 1
fi
#####################
### SANITY CHECKS ###
#####################
### Am i Root check
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root, or preceded by sudo."
echo "If sudo does not work, contact your system administrator."
exit 1
fi
## check if virsh command is available
which virsh
if [ $? -ne "0" ]; then
echo "virt-clients not installed, check requirements into README"
exit 1
fi
### the orignal installation should not be running.
$VIRSH list --name | grep -w $DISTRO.orignal > /dev/null 2>&1
if [ $? -eq "0" ]; then
echo "We need to shutdown the original instance before we start preparing a golden image"
echo "Please do that first. Run 'virsh shutdown' followed by the name of the instance."
echo "For example: 'virsh shutdown debian8.original' "
exit 1
fi
### clones using snapshots of this golden image should not run when we re/create the golden image.
$VIRSH list --name | grep -w $DISTRO > /dev/null 2>&1
if [ $? -eq "0" ]; then
echo "We cannot overwrite the golden image of $DISTRO when snapshots of it are in use."
echo "Please shut down all $DISTRO based domains first. These are:"
$VIRSH list --name | grep -w $DISTRO
echo "Run 'virsh shutdown $($VIRSH list --name | grep -w $DISTRO)' "
exit 1
fi
### the same applies if snapshots of the golden image already exist, to overwrite the the image they
### are referring to does not sound like a good idea.
$VIRSH list --name --all | grep -w $DISTRO | grep -v $DISTRO.original > /dev/null 2>&1
if [ $? -eq "0" ]; then
echo "We cannot overwrite the golden image of $DISTRO when snapshots of it exist."
echo "Please nuke all $DISTRO based domains first. These are:"
$VIRSH list --name --all | grep -w $DISTRO | grep -v $DISTRO.original
exit 1
fi
### The original installation image should exist.
### Then we can copy it over.
if [ -f $CWD/$DISTRO.original.img ]; then
cp $CWD/$DISTRO.original.img $CWD/${DISTRO}.golden.img
else
echo "I cannot find the base installation image for $DISTRO"
exit 1
fi
### Once we have a golden image, we don't need the original image anymore. It takes far too much space to
### keep a copy. What we still need is the original configuration file for the VM.
if [ -f $CWD/$DISTRO.golden.img ]; then
if [ -f $CWD/$DISTRO.original.img ]; then
echo "Deleting the original installation medium to save space."
$VIRSH vol-delete $DISTRO.original.img default
fi
fi
##################
### OPERATIONS ###
##################
## check if virt-sysprep command is available
which virt-sysprep
if [ $? -ne "0" ]; then
echo "libguestfs-tools not installed, check requirements into README"
exit 1
fi
## remove all the configuration that would cause problems when creating multiple clones
$SYSPREP \
--enable ssh-hostkeys,udev-persistent-net,net-hwaddr,logfiles,machine-id \
--no-selinux-relabel \
-a $CWD/${DISTRO}.golden.img
if [ ! -f $CWD/${DISTRO}.golden.img ]; then
echo "something went wrong in the preparation of the golden image"
exit 1
fi
## fix SELinux
$GUESTFISH --selinux -i $CWD/${DISTRO}.golden.img <<<'sh "load_policy && restorecon -R -v /"' > /dev/null 2>&1
## at the end of the process, we test put also the golden image in read-only now
chmod u-w $CWD/$DISTRO.golden.img
# chmod u-w $CWD/$DISTRO.original.img
exit 0