#!/usr/local/bin/python

# Copyright C 2011 by Isilon Systems, LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# LLC.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# LLC.  Legion is a trademark of Isilon Systems, LLC.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

import re
import subprocess
import sys
import isi.app.lib.procs as procs

is_geom = True
partition = None
mount_dir = None
# IMDD vars
root_part = None
diskuid = None

status, lines = procs.get_cmd_output('/usr/bin/isi_bootdisk_status', strip=False)
if lines[0].find('imdd') != -1:
     is_geom = False

if is_geom is True:
     #Get active from gpart and use that to figure out partition to boot
     status, lines = procs.get_cmd_output('/sbin/gpart list | grep -A 4 "active: 1" | tail -n 1', strip=False)
     partition = lines[0][lines[0].find('root'):].strip()
     #partition = 'root0'

else:
# IMDD
    status, lines = procs.get_cmd_output('/usr/bin/isi_imdd_status| grep -B 1 "#0x1 " | head -n 1', strip=False)
    lines_split = lines[0].split()
    partition = lines_split[0] + 'a'
    disk = lines_split[1]
    root_part = disk[disk.find('da'):]
    root_part = root_part[len(root_part)-1]

    part_c = disk[:disk.find('s1')] + 's1'
    status, lines = procs.get_cmd_output('/sbin/disklabel -Ar ' + part_c, strip=False)
    diskuidre = re.compile(r"^imdd_diskuid:\s+(\d+)")
    if lines is not None:
        for l in lines:
            m = diskuidre.match(l)
            if (m != None):
                diskuid, = m.groups()

# Check if our root is mounted, if not mount it
status, lines = procs.get_cmd_output('/sbin/mount | grep %s' % partition, strip=False)
if len(lines) == 0:
    subprocess.call(['/bin/mkdir','-p','/mnt/kload'])
    subprocess.call(['/sbin/mount','/dev/mirror/%s' % partition,'/mnt/kload'])
    mount_dir = '/mnt/kload'
else:
    mount_dir_start = lines[0].find('on') + 3
    mount_dir_end = lines[0].find('(ufs')
    mount_dir = lines[0][mount_dir_start:mount_dir_end]

if is_geom is True:
    print 'executing kload -h %s'%mount_dir
    status, lines = procs.get_cmd_output('/usr/sbin/kload -h %s' % mount_dir)
    for l in lines:
        print l
else:
    print 'executing kload -h %s -k disk_id=%s -k rootpart=%s' % \
          (mount_dir, diskuid, root_part)
    status, lines = procs.get_cmd_output \
                    ('/usr/sbin/kload -h %s -k disk_id=%s -k rootpart=%s' % \
                     (mount_dir, diskuid, root_part))
    for l in lines:
        print l
