pmbootstrap-meow/pmb/chroot/shutdown.py
Oliver Smith cca5c9aa30 pmb: fix test suite not running through twice
The test suite needed a `pmbootstrap shutdown` after running through,
before it could successfully run again.

Explanation:
This was caused by `test/test_pkgrel_bump.py`, which creates a
temporary work folder with every subfolder ("chroot_native",
"cache_apk_x86_64", ...) linked to the original work folder except for
the "packages" folder. At the end of the test case,
`pmbootstrap shutdown` gets executed and is expected to umount
everything as usual. But it does not umount anything because of the
symlinks, so `work/chroot_native/mnt/pmbootstrap-packages` points to
the fake packages folder of that test case, even after it is finished.

As a result, any test case that tries to access the packages folder in
the native chroot, will fail until `pmbootstrap shutdown` gets called.

Detailed Changes:
* Umount all folders inside the work folder, even if these are symlinks
* Remove obsolete reference to "disable timestamp based rebuilds" in a
  comment in `test/test_pkgrel_bump.py`
* Run `pmbootstrap work_migrate` and `pmbootstrap shutdown` at the
  beginning of `test/testcases_fast.sh`, in case the pkgrel_bump test
  case was aborted before it could properly shutdown and to make it
  more robust in general (user may have changed the mountpoints, work
  folder may need to be migrated)
2018-07-15 20:52:41 +00:00

102 lines
3.7 KiB
Python

"""
Copyright 2018 Oliver Smith
This file is part of pmbootstrap.
pmbootstrap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pmbootstrap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
"""
import logging
import glob
import os
import socket
from contextlib import closing
import pmb.chroot
import pmb.chroot.distccd
import pmb.helpers.mount
import pmb.install.losetup
import pmb.parse.arch
def kill_adb(args):
"""
Kill adb daemon if it's running.
"""
port = 5038
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
if sock.connect_ex(("127.0.0.1", port)) == 0:
pmb.chroot.root(args, ["adb", "-P", str(port), "kill-server"])
def shutdown_cryptsetup_device(args, name):
"""
:param name: cryptsetup device name, usually "pm_crypt" in pmbootstrap
"""
if not os.path.exists(args.work + "/chroot_native/dev/mapper/" + name):
return
pmb.chroot.apk.install(args, ["cryptsetup"])
status = pmb.chroot.root(args, ["cryptsetup", "status", name],
output_return=True, check=False)
if not status:
logging.warning("WARNING: Failed to run cryptsetup to get the status"
" for " + name + ", assuming it is not mounted"
" (shutdown fails later if it is)!")
return
if status.startswith("/dev/mapper/" + name + " is active."):
pmb.chroot.root(args, ["cryptsetup", "luksClose", name])
elif status.startswith("/dev/mapper/" + name + " is inactive."):
# When "cryptsetup status" fails, the device is not mounted and we
# have a left over file (#83)
pmb.chroot.root(args, ["rm", "/dev/mapper/" + name])
else:
raise RuntimeError("Failed to parse 'cryptsetup status' output!")
def shutdown(args, only_install_related=False):
pmb.chroot.distccd.stop(args)
# Stop adb server
kill_adb(args)
# Umount installation-related paths (order is important!)
pmb.helpers.mount.umount_all(args, args.work +
"/chroot_native/mnt/install")
shutdown_cryptsetup_device(args, "pm_crypt")
# Umount all losetup mounted images
chroot = args.work + "/chroot_native"
if pmb.helpers.mount.ismount(chroot + "/dev/loop-control"):
pattern = chroot + "/home/pmos/rootfs/*.img"
for path_outside in glob.glob(pattern):
path = path_outside[len(chroot):]
pmb.install.losetup.umount(args, path)
# Umount device rootfs chroot
chroot_rootfs = args.work + "/chroot_rootfs_" + args.device
if os.path.exists(chroot_rootfs):
pmb.helpers.mount.umount_all(args, chroot_rootfs)
if not only_install_related:
# Umount all folders inside args.work
# The folders are explicitly iterated over, so folders symlinked inside
# args.work get umounted as well (used in test_pkgrel_bump.py, #1595)
for path in glob.glob(args.work + "/*"):
pmb.helpers.mount.umount_all(args, path)
# Clean up the rest
for arch in pmb.config.build_device_architectures:
if pmb.parse.arch.cpu_emulation_required(args, arch):
pmb.chroot.binfmt.unregister(args, arch)
logging.debug("Shutdown complete")