| 1 |
# |
| 2 |
# z/VM specific functions |
| 3 |
# |
| 4 |
################################################################ |
| 5 |
# |
| 6 |
# Copyright (c) 1995-2014 SUSE Linux Products GmbH |
| 7 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License version 2 or 3 as |
| 10 |
# published by the Free Software Foundation. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program (see the file COPYING); if not, write to the |
| 19 |
# Free Software Foundation, Inc., |
| 20 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 21 |
# |
| 22 |
################################################################ |
| 23 |
|
| 24 |
ZVM_CLEANUP= |
| 25 |
ZVM_VOLUME_ROOT=${ZVM_VOLUME_ROOT:-0100} |
| 26 |
ZVM_VOLUME_SWAP=${ZVM_VOLUME_SWAP:-0200} |
| 27 |
|
| 28 |
# z/VM: use default kernel image from local machine |
| 29 |
# lets go with the default parameters. However zvm_initrd will be a required parameter |
| 30 |
# zvm_kernel=/boot/image |
| 31 |
#zvm_initrd=/boot/initrd_worker |
| 32 |
zvm_param="root=/dev/disk/by-path/ccw-0.0.${ZVM_VOLUME_ROOT}-part1 hvc_iucv=8 console=hvc0 hardened_usercopy=off $vm_linux_kernel_parameter" |
| 33 |
zvm_mult_pass="THR4ME" |
| 34 |
zvm_init_script="/.build/build" |
| 35 |
|
| 36 |
|
| 37 |
####################################################################################### |
| 38 |
|
| 39 |
# this once was in zvm_functions |
| 40 |
|
| 41 |
zvm_fatal() { |
| 42 |
echo "$1" |
| 43 |
test -n "$ZVM_CLEANUP" && exit 1 |
| 44 |
cleanup_and_exit 1 |
| 45 |
} |
| 46 |
|
| 47 |
zvm_prevent_detach() { |
| 48 |
if test "$1" = "150" -o "$1" = "0150"; then |
| 49 |
zvm_fatal "don't detach local root" |
| 50 |
fi |
| 51 |
} |
| 52 |
|
| 53 |
zvm_memset() { |
| 54 |
# defining the worker also resets the operating system. Be careful |
| 55 |
# $1: user name |
| 56 |
# $2: amount in MB |
| 57 |
# Note, that this is also limited by the worker definition in the user directory |
| 58 |
if test -n "$2"; then |
| 59 |
if ! vmcp send $1 cp define storage ${2}M ; then |
| 60 |
zvm_fatal "Could not redefine storage of $1 to ${2}M" |
| 61 |
fi |
| 62 |
fi |
| 63 |
} |
| 64 |
|
| 65 |
zvm_logon() { |
| 66 |
# kill machine if it already runs |
| 67 |
# autolog machine |
| 68 |
# Needs machine name as $1 |
| 69 |
if test -n "$1" ; then |
| 70 |
if $(vmcp q "$1" >& /dev/null) ; then |
| 71 |
vmcp force $1 |
| 72 |
sleep 1 |
| 73 |
fi |
| 74 |
if ! $(vmcp q "$1" >& /dev/null) ; then |
| 75 |
if ! $(vmcp xautolog $1 >& /dev/null) ; then |
| 76 |
zvm_fatal "Could not start machine $1. Is $1 defined in the user directory?" |
| 77 |
else |
| 78 |
# give the worker a moment to initialize |
| 79 |
sleep 2 |
| 80 |
zvm_memset $1 $VM_MEMSIZE |
| 81 |
sleep 2 |
| 82 |
fi |
| 83 |
fi |
| 84 |
fi |
| 85 |
} |
| 86 |
|
| 87 |
zvm_ipl() { |
| 88 |
# IPL worker. Needs user as $1 and ipl device as $2. |
| 89 |
if test -n "$1" -a -n "$2" ; then |
| 90 |
if ! $(vmcp q "$1" >& /dev/null); then |
| 91 |
zvm_fatal "User $1 not logged on." |
| 92 |
else |
| 93 |
if ! $(vmcp send $1 cp ipl $2); then |
| 94 |
zvm_fatal "Could not send command to $1" |
| 95 |
fi |
| 96 |
fi |
| 97 |
else |
| 98 |
zvm_fatal "Not enough arguments for ipl. Need user and device number." |
| 99 |
fi |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
zvm_destroy() { |
| 104 |
# Destroy build. Done by killing the worker machine. |
| 105 |
# needs user as $1 |
| 106 |
if test -n "$1"; then |
| 107 |
if ! $(vmcp force $1 ) ; then |
| 108 |
zvm_fatal "Could not force $1" |
| 109 |
fi |
| 110 |
fi |
| 111 |
} |
| 112 |
|
| 113 |
zvm_get_local_devnr() { |
| 114 |
# $1 is base address as defined in ZVM_VOLUME_ROOT and ZVM_VOLUME_SWAP (top of file) |
| 115 |
# $2 is worker number |
| 116 |
# there is room for up to 100 workers for this controlling guest, however in our setup I expect only up to 10 workers. |
| 117 |
if test "$2" -ge 100 ; then |
| 118 |
zvm_fatal "Not more than 100 workers supported by one controlling guest." |
| 119 |
fi |
| 120 |
if test "$1" = "${ZVM_VOLUME_ROOT}" ; then |
| 121 |
DEVNR=$((300+$2)) |
| 122 |
else |
| 123 |
if test "$1" = "${ZVM_VOLUME_SWAP}" ; then |
| 124 |
DEVNR=$((400+$2)) |
| 125 |
else |
| 126 |
zvm_fatal "The disk devices for root and swap must be ${ZVM_VOLUME_ROOT} and ${ZVM_VOLUME_SWAP} respectively." |
| 127 |
fi |
| 128 |
fi |
| 129 |
echo $DEVNR |
| 130 |
} |
| 131 |
|
| 132 |
zvm_volume_link_local() { |
| 133 |
# attach worker disk to local system as preparation for |
| 134 |
# a) prepare worker for build |
| 135 |
# b) get rpms of the swap disk after build finished |
| 136 |
# disk must be detached from worker first |
| 137 |
# The following arguments are needed: |
| 138 |
# 1. Worker user name |
| 139 |
# 2. Worker disk device number |
| 140 |
# 3. Mult password for the disk |
| 141 |
# 4. Worker number to generate a uniq local device number |
| 142 |
if test -n "$4"; then |
| 143 |
DEVNR=$(zvm_get_local_devnr $2 $4) |
| 144 |
if ! vmcp link $1 $2 $DEVNR MW >& /dev/null ; then |
| 145 |
zvm_fatal "Could not link disk $2 from user $1 to local device $DEVNR." |
| 146 |
fi |
| 147 |
dasd_configure 0.0.0$DEVNR 1 0 >& /dev/null |
| 148 |
udevadm settle |
| 149 |
DEVICE=$(ls /sys/bus/ccw/devices/0.0.0$DEVNR/block/) |
| 150 |
if ! test -b /dev/${DEVICE}1 ; then |
| 151 |
zvm_fatal "The device /sys/bus/ccw/devices/0.0.0$DEVNR has not been setup correctly." |
| 152 |
fi |
| 153 |
echo "${DEVICE}1" |
| 154 |
else |
| 155 |
zvm_fatal "Not enough arguments given to volume_link_local." |
| 156 |
fi |
| 157 |
} |
| 158 |
|
| 159 |
zvm_volume_detach_local() { |
| 160 |
# we need |
| 161 |
# 1. worker device number |
| 162 |
# 2. worker number |
| 163 |
DEVNR=$(zvm_get_local_devnr $1 $2) |
| 164 |
zvm_prevent_detach $DEVNR |
| 165 |
sync |
| 166 |
dasd_configure 0.0.0$DEVNR 0 0 |
| 167 |
udevadm settle |
| 168 |
if ! vmcp detach $DEVNR >& /dev/null ; then |
| 169 |
zvm_fatal "Could not locally detach disk number $1 from worker $2" |
| 170 |
fi |
| 171 |
} |
| 172 |
|
| 173 |
zvm_volume_attach() { |
| 174 |
# link the local disk of the worker |
| 175 |
# $1: user name |
| 176 |
# $2: disk device number |
| 177 |
# send link * nr nr |
| 178 |
# guest might not yet be ready to receive commands |
| 179 |
sleep 1 |
| 180 |
if ! vmcp send $1 cp link \* $2 $2 ; then |
| 181 |
zvm_fatal "Could not link worker disk number $2 for user $1" |
| 182 |
fi |
| 183 |
} |
| 184 |
|
| 185 |
zvm_volume_detach() { |
| 186 |
# send machine detach nr |
| 187 |
# $1: user name |
| 188 |
# $2: disk |
| 189 |
if ! vmcp send $1 cp detach $2 ; then |
| 190 |
zvm_fatal "Could not detach disk $2 on worker $1" |
| 191 |
fi |
| 192 |
} |
| 193 |
|
| 194 |
zvm_worker_init() { |
| 195 |
# 1. Worker user name |
| 196 |
# 2. Worker root device number |
| 197 |
# 3. Worker swap device number |
| 198 |
# 4. Worker number to generate a uniq local device number |
| 199 |
# Check for: |
| 200 |
# - still mounted dasd |
| 201 |
# - configured dasd |
| 202 |
# - linked dasd |
| 203 |
# - reset worker with force and autolog |
| 204 |
DEVNR_ROOT=$(zvm_get_local_devnr $2 $4) |
| 205 |
DEVNR_SWAP=$(zvm_get_local_devnr $3 $4) |
| 206 |
# First, check for mounts: |
| 207 |
for DEVNR in $DEVNR_ROOT $DEVNR_SWAP ; do |
| 208 |
if test -d /sys/bus/ccw/devices/0.0.0$DEVNR/block ; then |
| 209 |
DEV=$(ls /sys/bus/ccw/devices/0.0.0$DEVNR/block/) |
| 210 |
echo "Found device of worker $1 available at $DEVNR, device is /dev/$DEV." |
| 211 |
grep "/dev/$DEV" /proc/mounts >& /dev/null && umount /dev/${DEV}1 |
| 212 |
fi |
| 213 |
done |
| 214 |
# Second, check if devices are online |
| 215 |
for DEVNR in $DEVNR_ROOT $DEVNR_SWAP ; do |
| 216 |
lsdasd $DEVNR | grep $DEVNR && dasd_configure 0.0.0$DEVNR 0 0 |
| 217 |
done |
| 218 |
# Third, remove stale links |
| 219 |
for DEVNR in $DEVNR_ROOT $DEVNR_SWAP ; do |
| 220 |
zvm_prevent_detach $DEVNR |
| 221 |
if vmcp q v $DEVNR 2> /dev/null ; then |
| 222 |
vmcp detach $DEVNR |
| 223 |
fi |
| 224 |
done |
| 225 |
# Fourth, reset worker |
| 226 |
zvm_logon $1 |
| 227 |
} |
| 228 |
|
| 229 |
zvm_cp() { |
| 230 |
if test -n "$1" ; then |
| 231 |
case "$1" in |
| 232 |
start) shift ; zvm_logon "$@" ;; |
| 233 |
ipl) shift ; zvm_ipl "$@" ;; |
| 234 |
destroy) shift ; zvm_destroy "$@" ;; |
| 235 |
volume_attach) shift ; zvm_volume_attach "$@" ;; |
| 236 |
volume_detach) shift ; zvm_volume_detach "$@" ;; |
| 237 |
volume_link_local) shift ; zvm_volume_link_local "$@" ;; |
| 238 |
volume_detach_local) shift ; zvm_volume_detach_local "$@" ;; |
| 239 |
memset) shift ; zvm_memset "$@" ;; |
| 240 |
worker_init) shift ; zvm_worker_init "$@" ;; |
| 241 |
esac |
| 242 |
fi |
| 243 |
} |
| 244 |
|
| 245 |
####################################################################################### |
| 246 |
|
| 247 |
vm_verify_options_zvm() { |
| 248 |
# find the real device in the VM, includeing partition number |
| 249 |
VM_SWAPDEV="$(ls /sys/bus/ccw/devices/0.0.${ZVM_VOLUME_SWAP}/block/)1" |
| 250 |
|
| 251 |
VM_ROOT=${ZVM_VOLUME_ROOT} |
| 252 |
VM_SWAP=${ZVM_VOLUME_SWAP} |
| 253 |
if test -z "$VM_ROOT" ; then |
| 254 |
if test -n "$BUILD_ROOT" -a ${#BUILD_ROOT} -le 4 ; then |
| 255 |
VM_ROOT="$BUILD_ROOT" |
| 256 |
else |
| 257 |
VM_ROOT="0150" |
| 258 |
fi |
| 259 |
fi |
| 260 |
VM_ROOT_TYPE=unattached |
| 261 |
VM_SWAP_TYPE=unattached |
| 262 |
|
| 263 |
if test -n "$KILL" -o -n "$DO_WIPE" ; then |
| 264 |
return |
| 265 |
fi |
| 266 |
|
| 267 |
# z/VM guest name that is already defined in z/VM |
| 268 |
if test -z "$VM_WORKER" ; then |
| 269 |
cleanup_and_exit 3 "ERROR: No z/VM worker id specified" |
| 270 |
fi |
| 271 |
if test -z "$VM_WORKER_NO" ; then |
| 272 |
cleanup_and_exit 3 "ERROR: No z/VM worker number specified" |
| 273 |
fi |
| 274 |
# need the name for a kernel in zvm |
| 275 |
if test -n "$VM_KERNEL" ; then |
| 276 |
vm_kernel="$VM_KERNEL" |
| 277 |
elif test -e "/boot/vmlinux.gz" ; then |
| 278 |
vm_kernel="/boot/vmlinux.gz" |
| 279 |
else |
| 280 |
cleanup_and_exit 3 "ERROR: No z/VM kernel specified" |
| 281 |
fi |
| 282 |
# need the name for an initrd in zvm |
| 283 |
# this normally will not be the local initrd |
| 284 |
if test -n "$VM_INITRD" ; then |
| 285 |
vm_initrd="$VM_INITRD" |
| 286 |
else |
| 287 |
cleanup_and_exit 3 "ERROR: No z/VM initrd specified" |
| 288 |
fi |
| 289 |
zvm_cp worker_init $VM_WORKER $ZVM_VOLUME_ROOT $ZVM_VOLUME_SWAP $VM_WORKER_NO |
| 290 |
zvm_cp volume_detach $VM_WORKER $ZVM_VOLUME_ROOT |
| 291 |
zvm_cp volume_detach $VM_WORKER $ZVM_VOLUME_SWAP |
| 292 |
} |
| 293 |
|
| 294 |
vm_startup_zvm() { |
| 295 |
# link root/swap to the worker |
| 296 |
zvm_cp volume_attach $VM_WORKER $ZVM_VOLUME_ROOT |
| 297 |
zvm_cp volume_attach $VM_WORKER $ZVM_VOLUME_SWAP |
| 298 |
zvm_cp ipl $VM_WORKER $ZVM_VOLUME_ROOT |
| 299 |
# start IUCV Console |
| 300 |
# IPL needs some time until IPL really starts... |
| 301 |
sleep 5 |
| 302 |
# start iucv console. This blocks until build process is finished. |
| 303 |
iucvconn $VM_WORKER lnxhvc0 |
| 304 |
# sleep some time before taking root and swap devices from worker |
| 305 |
# This might be critical regarding timing (IUCV_CONSOLE down, but machine still running) |
| 306 |
sleep 5 |
| 307 |
# Build jobs might change the shutdown behavior, so just return if machine is powered off. |
| 308 |
if ! (vmcp q $VM_WORKER >& /dev/null); then |
| 309 |
return |
| 310 |
fi |
| 311 |
zvm_cp volume_detach $VM_WORKER $ZVM_VOLUME_ROOT |
| 312 |
zvm_cp volume_detach $VM_WORKER $ZVM_VOLUME_SWAP |
| 313 |
} |
| 314 |
|
| 315 |
vm_kill_zvm() { |
| 316 |
if vmcp q "$VM_WORKER" > /dev/null 2>&1 ; then |
| 317 |
if ! zvm_cp destroy $VM_WORKER ; then |
| 318 |
cleanup_and_exit 1 "could not kill zvm worker $VM_WORKER" |
| 319 |
fi |
| 320 |
fi |
| 321 |
} |
| 322 |
|
| 323 |
vm_initrd_obs_modules_zvm() { |
| 324 |
# This function is needed to add the required kernel modules that are |
| 325 |
# installed to the build system to the initrd that starts the worker. |
| 326 |
# Note, that calling mkinitrd directly for the initrd within the worker |
| 327 |
# currently causes segfaults, which might be a result from not having |
| 328 |
# all needed binaries within the build system. |
| 329 |
# In order to keep the initrd small, only those kernel modules are |
| 330 |
# copied that already exist in the initrd created by obsworker. |
| 331 |
# 1. build root mounted to local system |
| 332 |
# 2. initrd created on administrative worker |
| 333 |
# 3. kernel_version to add modules |
| 334 |
# initrd is to be created at $2-$3 |
| 335 |
# first, test if initrd has already been created |
| 336 |
test -f "${2}-${3}" && return |
| 337 |
TEMPDIR=$(mktemp -d /tmp/initrd.XXX) |
| 338 |
pushd $TEMPDIR |
| 339 |
# unpack initrd to add the kernel modules |
| 340 |
xzcat "$2" | cpio -i |
| 341 |
# detect currently installed kernel version |
| 342 |
current_kernel_version=$(ls lib/modules) |
| 343 |
# copy modules from kernel that is installed to the buildsystem: |
| 344 |
mkdir -p "$TEMPDIR/lib/modules/$3" |
| 345 |
assert_dir_path "lib/modules/$3" |
| 346 |
while read module; do |
| 347 |
(cd ${BUILD_ROOT}/lib/modules/${3}; \ |
| 348 |
assert_dir_path "lib/modules/$3/${module%/*.xz}" |
| 349 |
rsync -a --relative "$module" "${TEMPDIR}/lib/modules/$3") |
| 350 |
done < <(cd $TEMPDIR/lib/modules/${current_kernel_version}; \ |
| 351 |
find . -name "*.xz") |
| 352 |
# remove old kernel modules |
| 353 |
if [ "${current_kernel_version}" != "$3" ]; then |
| 354 |
rm -rf "$TEMPDIR/lib/modules/${current_kernel_version}" |
| 355 |
fi |
| 356 |
# create module dependencies: |
| 357 |
depmod -b "$TEMPDIR" "$3" |
| 358 |
# create new initrd: |
| 359 |
find . | cpio -H newc -o | xz -9 --format=lzma > "${2}-${3}" |
| 360 |
sync |
| 361 |
popd |
| 362 |
rm -rf $TEMPDIR |
| 363 |
} |
| 364 |
|
| 365 |
vm_fixup_zvm() { |
| 366 |
# initrd is created in obsstoragesetup. |
| 367 |
# If it is desired to use a project dependent kernel, use make_guestinitrd from zvm_functions. |
| 368 |
# have to copy kernel/initrd and run zipl to be able to IPL |
| 369 |
# have to set init_script before unmounting, thus doing it statically for now. |
| 370 |
zvm_init_script="/.build/build" |
| 371 |
assert_dir_path boot/zipl |
| 372 |
mkdir -p $BUILD_ROOT/boot/zipl |
| 373 |
vm_kernel="/.build.kernel.kvm" |
| 374 |
test -e "${BUILD_ROOT}${vm_kernel}" -a ! -L "${BUILD_ROOT}${vm_kernel}" || cleanup_and_exit 1 "${vm_kernel} does not exist" |
| 375 |
kernel_version=$(get_kernel_version "${BUILD_ROOT}${vm_kernel}") |
| 376 |
# add kernel modules to existing initrd |
| 377 |
vm_initrd_obs_modules_zvm "${BUILD_ROOT}" "${vm_initrd}" "${kernel_version}" |
| 378 |
vm_initrd="${vm_initrd}-${kernel_version}" |
| 379 |
# copy initrd to build worker: |
| 380 |
echo "copy $vm_initrd to $BUILD_ROOT" |
| 381 |
rm -rf "${BUILD_ROOT}/boot/${vm_initrd}" |
| 382 |
cp -a "${vm_initrd}" "${BUILD_ROOT}/boot" |
| 383 |
# finally, install bootloader to the worker disk |
| 384 |
# XXX/TODO: does zipl read or write ${BUILD_ROOT}${vm_initrd}? Either |
| 385 |
# rm -rf the file or check for a symlink |
| 386 |
zipl -t "$BUILD_ROOT/boot/zipl" -i "${BUILD_ROOT}${vm_kernel}" \ |
| 387 |
-r "${BUILD_ROOT}${vm_initrd}" \ |
| 388 |
--parameters "${zvm_param} init=$zvm_init_script rootfsopts=noatime" |
| 389 |
sync |
| 390 |
udevadm settle |
| 391 |
} |
| 392 |
|
| 393 |
vm_attach_root_zvm() { |
| 394 |
VM_ROOT=$(ZVM_CLEANUP=1 zvm_cp volume_link_local $VM_WORKER $ZVM_VOLUME_ROOT $zvm_mult_pass $VM_WORKER_NO ) |
| 395 |
if test "${VM_ROOT}" = "${VM_ROOT#dasd}" ; then |
| 396 |
cleanup_and_exit 3 "did not get a real device for VM_ROOT: $VM_ROOT" |
| 397 |
fi |
| 398 |
VM_ROOT="/dev/$VM_ROOT" |
| 399 |
VM_ROOT_TYPE=device |
| 400 |
} |
| 401 |
|
| 402 |
vm_attach_swap_zvm() { |
| 403 |
VM_SWAPDEV=$(ZVM_CLEANUP=1 zvm_cp volume_link_local $VM_WORKER $ZVM_VOLUME_SWAP $zvm_mult_pass $VM_WORKER_NO ) |
| 404 |
if test "${VM_SWAPDEV}" = "${VM_SWAP#dasd}" ; then |
| 405 |
cleanup_and_exit 3 "did not get a real device for VM_SWAPDEV: $VM_SWAPDEV" |
| 406 |
fi |
| 407 |
VM_SWAP="/dev/$VM_SWAPDEV" |
| 408 |
VM_SWAPDEV="/dev/$VM_SWAPDEV" |
| 409 |
VM_SWAP_TYPE=device |
| 410 |
} |
| 411 |
|
| 412 |
vm_detach_root_zvm () { |
| 413 |
sync |
| 414 |
zvm_cp volume_detach_local $ZVM_VOLUME_ROOT $VM_WORKER_NO |
| 415 |
VM_ROOT_TYPE=unattached |
| 416 |
} |
| 417 |
|
| 418 |
vm_detach_swap_zvm() { |
| 419 |
zvm_cp volume_detach_local $ZVM_VOLUME_SWAP $VM_WORKER_NO |
| 420 |
VM_SWAP_TYPE=unattached |
| 421 |
} |
| 422 |
|
| 423 |
vm_cleanup_zvm() { |
| 424 |
if test -n "$VM_WORKER" -a -n "$VM_WORKER_NO" -a -n "$ZVM_VOLUME_ROOT" -a -n "$ZVM_VOLUME_SWAP" ; then |
| 425 |
ZVM_CLEANUP=1 |
| 426 |
(zvm_cp volume_detach $VM_WORKER $ZVM_VOLUME_ROOT >/dev/null 2>&1) |
| 427 |
(zvm_cp volume_detach $VM_WORKER $ZVM_VOLUME_SWAP >/dev/null 2>&1) |
| 428 |
(zvm_cp volume_detach_local $ZVM_VOLUME_ROOT $VM_WORKER_NO >/dev/null 2>&1) |
| 429 |
(zvm_cp volume_detach_local $ZVM_VOLUME_SWAP $VM_WORKER_NO >/dev/null 2>&1) |
| 430 |
fi |
| 431 |
} |
| 432 |
|
| 433 |
vm_sysrq_zvm() { |
| 434 |
: |
| 435 |
} |
| 436 |
|
| 437 |
vm_wipe_zvm() { |
| 438 |
: |
| 439 |
} |