#!/usr/bin/env python
# Copyright (c) 2008 Citrix Systems, Inc. All use and distribution of this
# copyrighted material is governed by and subject to terms and conditions
# as licensed by Citrix Systems, Inc. All other rights reserved.
# Xen, XenSource and XenEnterprise are either registered trademarks or
# trademarks of Citrix Systems, Inc. in the United States and/or other 
# countries.
#
#
# LVM upgrade script for Dom0, inserts the use_vhd flag into the sm_config
# database
#

import os
import sys
import re
sys.path.insert(0, "/opt/xensource/sm")
import util
import XenAPI


def help():
    print "Usage: xe-lvm-upgrade <SR UUID>"
    sys.exit(-1)

def main():
    if len(sys.argv) != 2:
        help()
    uuid = sys.argv[1]
    if not util.exactmatch_uuid(uuid):
        print "Not a UUID"
        help()
    session = util.get_localAPI_session()
    sr_ref = session.xenapi.SR.get_by_uuid(uuid)
    type = session.xenapi.SR.get_type(sr_ref)
    if not re.search("lvm", type):
        print "Incorrect SR type, must be an LVM SR"
    sm_config = session.xenapi.SR.get_sm_config(sr_ref)
    sm_config['use_vhd'] = 'true'
    session.xenapi.SR.set_sm_config(sr_ref, sm_config)
    session.xenapi.SR.scan(sr_ref)
    print "SR %s successfully upgraded" % uuid

if __name__ == '__main__':
    main()
