# Copyright (c) 2014, Craig Rodrigues <[email protected]>1# All rights reserved.2#3# Redistribution and use in source and binary forms, with or without4# modification, are permitted provided that the following conditions5# are met:6# 1. Redistributions of source code must retain the above copyright7# notice unmodified, this list of conditions, and the following8# disclaimer.9# 2. Redistributions in binary form must reproduce the above copyright10# notice, this list of conditions and the following disclaimer in the11# documentation and/or other materials provided with the distribution.12#13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,17# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2324# EXTRACT TEST LOGS FROM BHYVE VM25#26# The following program::27# (1) takes a UFS image28# (2) mounts it29# (3) extracts test logs from /usr/tests directory30#3132from __future__ import print_function33from optparse import OptionParser34import atexit35import getopt36import json37import subprocess38import sys39import tempfile4041test_config = None42test_config_file = None43sentinel_file = None44temp_dir = None45md = ""4647def usage(argv):48print("Usage:")49print(" %s -f [JSON config file]" % argv[0])505152def main(argv):5354try:55opts, args = getopt.getopt(sys.argv[1:], "f:")56except getopt.GetoptError as err:57print(err)58sys.exit(2)5960global test_config61global test_config_file62global sentinel_file63global temp_dir64global md6566for o, a in opts:67if o == "-f":68test_config_file = a69else:70assert False, "unhandled option"7172if test_config_file is None:73usage(argv)74sys.exit(1)7576config_file = open(test_config_file, "r")77test_config = json.load(config_file)78config_file.close()7980md = subprocess.check_output(["mdconfig", "-a", "-f", test_config['disks'][0]])81md = md.strip()82temp_dir = tempfile.mkdtemp()83cmd = ["mount", "/dev/%s" % (md), temp_dir]84print(" ".join(cmd))85ret = subprocess.call(cmd)86if ret != 0:87sys.exit(ret)8889cmd = "cp %s/usr/tests/*.xml %s/usr/tests/*.txt ." \90% (temp_dir, temp_dir)91subprocess.call(cmd, shell=True)9293def cleanup():94global md95subprocess.call("rm -f %s" % (sentinel_file), shell=True)96if temp_dir is not None:97cmd = ["umount", temp_dir]98print(" ".join(cmd))99subprocess.call(cmd)100101cmd = ["mdconfig", "-d", "-u", md]102print(" ".join(cmd))103subprocess.call(cmd)104105if __name__ == "__main__":106atexit.register(cleanup)107main(sys.argv)108109110