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
118
119
120
121
122 | #!/bin/bash
#
# This install script is run *inside* the image
#
#
SCRIPTS_D=${1}; shift
APT_OPTS="--quiet --assume-yes --no-install-recommends"
APT_OPTS="$APT_OPTS --option=Dpkg::options::=--force-unsafe-io"
APT_OPTS="$APT_OPTS --option=Dpkg::Options::=--force-confold"
#
# prep for modification
#
# disable daemons
fpath="/usr/sbin/policy-rc.d"
tee $fpath << EOF
#!/bin/sh
# see invoke-rc.d for exit codes. 101 is "do not run"
while true; do
case "\$1" in
-*) shift;;
makedev|x11-common) exit 0;;
*) exit 101;;
esac
done
EOF
chmod 0755 $fpath
# disable initramfs update, not needed
update_initramfs="/etc/initramfs-tools/update-initramfs.conf"
sed -i.injectdist \
-e 's,update_initramfs=yes,update_initramfs=no,g' $update_initramfs
#
# modification start here
#
ROOTFS=""
LOGGING="etc/cloud/cloud.cfg.d/05_logging.cfg"
SYSTEMD="lib/systemd/system"
CI_LOCAL="cloud-init-local.service"
CI_FINAL="cloud-final.service"
CI_PROFILER="cloud-init-profiler.service"
PROFILE_ENABLE="etc/cloud/profiler-enable"
PROFILE_PKGS="blktrace linux-tools-common linux-tools-%KREL% dstat"
PROFILE_SCRIPTS_DIR="${SCRIPTS_D}/profiling"
echo "Modifying image"
# logging
echo "Enable cloud-init common logging format"
sed -i.dist -e "s,%(asctime)s -,%(asctime)s [CLOUDINIT],g" \
-e "/log_base.*log_syslog/s/^/#/g" \
${ROOTFS}/${LOGGING}
# enable profiling
echo "Enable cloud-init profiling"
sed -i.dist \
-e '/After=local-fs.target/a Requires=cloud-init-profiler.service' \
${ROOTFS}/${SYSTEMD}/${CI_LOCAL}
sed -i.dist \
-e '/ExecStart=/a ExecStart=/bin/systemctl stop cloud-init-profiler' \
${ROOTFS}/${SYSTEMD}/${CI_FINAL}
tee ${ROOTFS}/${SYSTEMD}/${CI_PROFILER} >/dev/null << EOF
[Unit]
Description=Cloud-init profiling service
ConditionPathExists=/${PROFILE_ENABLE}
DefaultDependencies=no
Before=cloud-init-local.service
[Service]
Type=forking
ExecStart=/sbin/cloud-init-profiler start
ExecStop=/sbin/cloud-init-profiler stop
TimeoutSec=0
[Install]
WantedBy=cloud-init.target
EOF
systemctl enable ${CI_PROFILER} &>/dev/null
touch ${ROOTFS}/${PROFILE_ENABLE}
for s in `find ${PROFILE_SCRIPTS_DIR} -type f`; do
profiler=`basename ${s}`
echo "Installing profiler: ${profiler}"
install --mode=0755 ${s} ${ROOTFS}/sbin/${profiler}
done
# in a vm install profiler packages
if [ -e "/vmlinuz" ]; then
echo "Installing profiling tools"
# Expecting a VM image here, we can do package installs
# guess target kernel release; needed for perf in linux-tools-$krel
KREL="$(basename `readlink -f /vmlinuz` | sed -e 's,vmlinuz-,,')"
# inject krel in package list
PKGS=( `echo $PROFILE_PKGS | sed -e "s,%KREL%,$KREL,g"` )
# check if we have them installed already
INSTALLED=$(dpkg-query --show --showformat \$\{db:Status-Abbrev}\\\n ${PKGS[@]} 2>/dev/null | wc -l)
if [ "${INSTALLED}" != "${#PKGS[@]}" ]; then
# do the install
echo "Installing profiling packages: ${PKGS[@]}"
apt-get -yq update
apt-get install ${APT_OPTS} ${PKGS[@]}
else
echo "Already installed profiling packages: ${PKGS[@]}"
fi
else
echo -n "Skipping installation of profiling tools, "
echo "LXD instances don't support some profiling tools"
fi
#
# cleanup
#
# reenable daemons
rm -f $fpath
# reenable initramfs update
mv ${update_initramfs}.injectdist ${update_initramfs}
|