#!/usr/bin/env python

# Copyright (C) 2014  Sangoma Technologies Corp.
# All Rights Reserved.
#
# Author(s):
# Leonardo Lang <lang@sangoma.com>
#
# This code is Sangoma Technologies Confidential Property.
# Use of and access to this code is covered by a previously executed
# non-disclosure agreement between Sangoma Technologies and the Recipient.
# This code is being supplied for evaluation purposes only and is not to be
# used for any other purpose.

GRUB_CONF = '/boot/grub/grub.conf'
GRUB_TEMP = GRUB_CONF + '.new'

SERIAL_WHITELIST = {
    'chassis_vendor': [ 'QEMU', 'Xen', 'VirtualBox' ],
    'board_name':     [ 'ROBO-8110VG2AR-ST' ],
    'product_uuid':   [ '00020003-0004-0005-0006-000700080009' ],
}

import os
import subprocess

def read_data(fn):
    try:
        rfd = open(fn)
        data = rfd.read().strip()
        rfd.close()
        return data
    except:
        return None

def enable_serial():
    # make sure we only add console=ttyS0 if the installer haven't added it already, and only to the last entry
    if subprocess.call("grep '^[^#]' %s | grep -m1 'xen_emul_unplug=' | grep -q 'console=ttyS0'" % GRUB_CONF, shell=True) <> 0:
        # enable serial console (issue #10668)
        subprocess.call(['/bin/sed', '-i', '-e', 's#ro xen_emul_unplug=#ro console=tty0 console=ttyS0,115200n8 xen_emul_unplug=#g', GRUB_CONF])

    if subprocess.call(['grep', '-q', '^serial', GRUB_CONF]) <> 0:
        outfd = open(GRUB_TEMP, 'w')
        inpfd = open(GRUB_CONF)

        outfd.write('serial --unit=0 --speed=115200 --parity=no --stop=1\n')
        outfd.write('terminal --timeout=3 serial console\n')
        while True:
            line = inpfd.readline()
            if len(line) == 0: break
            outfd.write(line)
        inpfd.close()
        outfd.close()

        subprocess.call(['mv', '-f', GRUB_TEMP, GRUB_CONF])

def main():
    if not os.access(GRUB_CONF, os.F_OK):
        subprocess.call(['/usr/bin/logger', '-t', 'sng-update-grub', 'no "%s" file, bailing out..' % GRUB_CONF])

    # if the kernel panics, make the system reboot (issue #10352)
    subprocess.call(['/bin/sed', '-i', '-e', 's#ro root=#ro panic=15 root=#g', GRUB_CONF])
    # reserve space for running kdump as the crashkernel (issue 10363)
    subprocess.call(['/bin/sed', '-i', '-e', 's#ro panic=#ro crashkernel=128M@64M panic=#g', GRUB_CONF])
    # don't unplug the xen emulated devices so the crashkernel can work on xen guests (issue #10524)
    subprocess.call(['/bin/sed', '-i', '-e', 's#ro crashkernel=#ro xen_emul_unplug=unnecessary crashkernel=#g', GRUB_CONF])

    # scan serial console whitelist
    for keyname, whitelist in SERIAL_WHITELIST.items():
        keydata = read_data('/sys/devices/virtual/dmi/id/%s' % keyname)

        if keydata not in whitelist:
            continue

        subprocess.call(['/usr/bin/logger', '-t', 'sng-update-grub', 'found "%s" in %s whitelist, enabling serial console' % (keydata, keyname)])
        enable_serial()
        break

main()
