-
Notifications
You must be signed in to change notification settings - Fork 0
/
kexec.sh
executable file
·192 lines (167 loc) · 4.02 KB
/
kexec.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# Any failure here is fatal
set -x -e
CRIU_GIT="https://github.com/xemul/criu.git -b master"
HOME_PWD="$(pwd)"
PATCH_DIR="${HOME_PWD}/patches"
CRIU_BIN="${HOME_PWD}/${CRIU_DIR}/criu/criu"
CRIT_BIN="${HOME_PWD}/${CRIU_DIR}/crit/crit"
NR_CPU=$(grep -c ^processor /proc/cpuinfo)
NEW_KERNEL=""
PROCESS_TREE=""
SSHD_PID=
IMAGES_PATH=/imgs
RESTORE_SCRIPT="$(pwd)/kexec-restore.sh"
PKGS="protobuf-c-compiler libprotobuf-c0-dev libaio-dev libprotobuf-dev
protobuf-compiler python-ipaddr libcap-dev libnl-3-dev gcc-multilib
libc6-dev-i386 gdb bash python-protobuf libnet-dev util-linux asciidoc
kexec-tools libssl-dev libelf-dev strace ccache"
export CC="ccache gcc"
export CXX="ccache g++"
export CCACHE_DIR="/home/travis/.ccache"
export PATH="/usr/lib/ccache:$PATH"
### Preparations
function install_pkgs()
{
apt-get update -qq
apt-get install -qq ${PKGS}
pip install dropbox
}
function prepare_criu()
{
echo "Cloning CRIU..."
git clone ${CRIU_GIT} "${CRIU_DIR}"
cd "${CRIU_DIR}"
echo "Applying CRIU patches..."
git am ${PATCH_DIR}/criu/*
echo "Building CRIU..."
time make -j$((NR_CPU+1))
echo "Prepared $(git describe) CRIU."
ccache -s
cd -
make pidns
echo "Checking CRIU:"
${CRIU_BIN} check --extra --all || true # fails, but it's OK
}
function prepare_kernel()
{
echo "Cloning kernel..."
git clone --depth 1 ${KGIT} "${KPATH}"
echo "Building kernel..."
cp "${KCONFIG}" "${KPATH}/.config"
cd "${KPATH}"
make olddefconfig
# get rid of modules in config
yes "" | make localyesconfig
time make -j$((NR_CPU+1))
NEW_KERNEL="$(make -s --no-print-directory kernelrelease)"
echo "Prepared ${NEW_KERNEL} kernel."
ccache -s
cd -
if [[ ${DEBUG} == "y" ]] ; then
pwd
ls -l ${KPATH}/arch/x86/boot/bzImage
fi
}
#
# init (pid = 1)
# \_ /usr/sbin/sshd -D (our process tree)
# \_ sshd: travis [priv] (sshd daemon with some fifo)
# \_ ...
# \_ ...
function find_process_tree_for_cr()
{
PROCESS_TREE=$$
SSHD_PID=$$
local pid=$$
while [[ $pid -ne 1 ]] ; do
SSHD_PID=${PROCESS_TREE}
PROCESS_TREE=${pid}
pid=$(awk '{ if ($1=="PPid:") print $2 }' \
/proc/${PROCESS_TREE}/status)
done
}
function prepare_env()
{
mkdir -p "${LOGS}"
rm -rf ${LOGS}/*
mkdir -p ${IMAGES_PATH}
rm -rf ${IMAGES_PATH}/*
find_process_tree_for_cr
local SYSTEMD_FIFO="$(lsof -p ${SSHD_PID} | \
grep /run/systemd/sessions | awk '{ print $9 }')"
echo "Systemd fifo file ${SYSTEMD_FIFO}"
modprobe tun
modprobe macvlan
modprobe veth
# Disable Docker daemon start after reboot; upstart way
echo manual > /etc/init/docker.override
if [[ -f /etc/init/criu.conf ]] ; then
unlink /etc/init/criu.conf
fi
cat > /etc/init/criu.conf <<-EOF
start on runlevel [2345]
stop on runlevel [016]
exec ${RESTORE_SCRIPT} "$(pwd)" "${SYSTEMD_FIFO}" "${LOGS}" "${CRIU_DIR}" "${DEBUG}"
EOF
if [[ ${DEBUG} == "y" ]] ; then
cat /etc/init/criu.conf
fi
cat > /etc/network/if-pre-up.d/iptablesload <<-EOF
#!/bin/sh
iptables-restore < /etc/iptables.rules
unlink /etc/network/if-pre-up.d/iptablesload
unlink /etc/iptables.rules
exit 0
EOF
chmod +x /etc/network/if-pre-up.d/iptablesload
iptables-save -c > /etc/iptables.rules
echo $TRAVIS_BUILD_ID > /travis_id
echo $KNAME > /kname
set +x
echo $DROPBOX_TOKEN > /dropbox
set -x
}
function debug_preparations()
{
set -x
cat /proc/cpuinfo
echo "NR_CPU = ${NR_CPU}"
uname -a
lsmod
ip a
ip r
iptables -L
cat /proc/cmdline
cat /proc/self/mountinfo
ps axf
set +x
echo "Process tree to CR:"
ps -fH --ppid ${PROCESS_TREE}
}
install_pkgs
prepare_criu
prepare_kernel
prepare_env
debug_preparations 2>&1 > "${LOGS}/prepare.log"
### Kexec
export CRIU_BIN IMAGES_PATH CRIT_BIN SSHD_PID
setsid bash -c "setsid ./kexec-dump.sh &"
for i in `seq 10`; do
echo "Waiting for kexec... kernel is $(uname -a)"
sleep 15
if [[ -f /rebooted ]]; then
if [[ "${NEW_KERNEL}" == "$(uname -r)" ]] ; then
exec bash -c ${TEST_CMD}
else
echo "Rebooted, but failed to check kernel $(uname -a)"
exit 1
fi
fi
if [[ -f /reboot.failed ]] ; then
echo "Kexec failed"
exit 1
fi
done
echo "Kexec timeouted"
exit 1