Example #1
0
def pack_vps_fs_tarball (img_path, tarball_dir_or_path):
    """ if tarball_dir_or_path is a directory, will generate filename like XXX_fs_FSTYPE.tar.gz  """
    tarball_dir = None
    tarball_path = None
    if os.path.isdir (tarball_dir_or_path):
        tarball_dir = tarball_dir_or_path
    else:
        if os.path.exists (tarball_dir_or_path):
            raise Exception ("file %s exists" % (tarball_dir_or_path))
        tarball_path = tarball_dir_or_path
        tarball_dir = os.path.dirname (tarball_path)
        if not os.path.isdir (tarball_dir):
            raise Exception ("directory %s not exists" % (tarball_dir))

    if img_path.find ("/dev") == 0:
        mount_point = mount_partition_tmp (img_path, readonly=True)
    else:
        mount_point = mount_loop_tmp (img_path, readonly=True)
    if not tarball_path and tarball_dir:
        fs_type = get_partition_fs_type (mount_point=mount_point)
        tarball_name = "%s_fs_%s.tar.gz" % (os.path.basename (img_path), fs_type)
        tarball_path = os.path.join (tarball_dir, tarball_name)
        if os.path.exists (tarball_path):
            raise Exception ("file %s already exists" % (tarball_path))
        
    cwd = os.getcwd ()
    os.chdir (mount_point)
    try:
        call_cmd ("tar zcf %s ." % (tarball_path))
    finally:
        os.chdir (cwd)
        umount_tmp (mount_point)
    return tarball_path
Example #2
0
def create_raw_image (path, size_g, sparse=False):
    assert size_g > 0
    size_m = int (size_g * 1024)
    if sparse:
        call_cmd ("dd if=/dev/zero of=%s bs=1M count=1 seek=%d" % (path, size_m - 1))
    else:
        call_cmd ("dd if=/dev/zero of=%s bs=1M count=%d" % (path, size_m))
Example #3
0
 def create_limit(self):
     if not self.cgroup_limit or len(self.cgroup_limit) != 4:
         return
     assert conf.CGROUP_SCRIPT_DIR
     if not os.path.isdir(conf.CGROUP_SCRIPT_DIR):
         os.makedirs(conf.CGROUP_SCRIPT_DIR)
     major, minor = vps_common.get_dev_no(self.dev)
     script_file = os.path.join(conf.CGROUP_SCRIPT_DIR,
                                os.path.basename(self.dev))
     content = ""
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_iops_device\n" % (
         major, minor, self.cgroup_limit[0])
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_iops_device\n" % (
         major, minor, self.cgroup_limit[1])
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device\n" % (
         major, minor, self.cgroup_limit[2])
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_bps_device\n" % (
         major, minor, self.cgroup_limit[3])
     if content:
         content = "#!/bin/sh\n" + content
         f = open(script_file, "w")
         try:
             f.write(content)
         finally:
             f.close()
         call_cmd("sh %s" % script_file)
         return True
Example #4
0
 def create_limit(self):
     if not self.cgroup_limit or len(self.cgroup_limit) != 4:
         return
     assert conf.CGROUP_SCRIPT_DIR
     if not os.path.isdir(conf.CGROUP_SCRIPT_DIR):
         os.makedirs(conf.CGROUP_SCRIPT_DIR)
     major, minor = vps_common.get_dev_no(self.dev)
     script_file = os.path.join(
         conf.CGROUP_SCRIPT_DIR, os.path.basename(self.dev))
     content = ""
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_iops_device\n" % (
         major, minor, self.cgroup_limit[0])
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_iops_device\n" % (
         major, minor, self.cgroup_limit[1])
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device\n" % (
         major, minor, self.cgroup_limit[2])
     content += "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_bps_device\n" % (
         major, minor, self.cgroup_limit[3])
     if content:
         content = "#!/bin/sh\n" + content
         f = open(script_file, "w")
         try:
             f.write(content)
         finally:
             f.close()
         call_cmd("sh %s" % script_file)
         return True
Example #5
0
def lv_create (vg_name, lv_name, size_g):
    assert size_g > 0
    size_m = int (size_g * 1024)
    call_cmd ("lvcreate --name %s --size %dM /dev/%s" % (lv_name, size_m, vg_name))
    lv_dev = "/dev/%s/%s" % (vg_name, lv_name)
    if not os.path.exists (lv_dev):
        raise Exception ("lv %s not exists after creating" % (lv_dev))
    return lv_dev
Example #6
0
def add_rule(net):
    assert net
    ovsops = OVSOps()
    ofport = ovsops.find_ofport_by_name(conf.EXT_INF)
    assert ofport > 0
    call_cmd(
        "ovs-ofctl add-flow %s 'ip,in_port=%s,nw_dst=%s,priority=2000,action=normal'"
        % (conf.XEN_BRIDGE, ofport, net))
    call_cmd("ovs-ofctl add-flow %s 'ip,in_port=%s,priority=10,action=drop'" %
             (conf.XEN_BRIDGE, ofport))
Example #7
0
def sync_img(vpsmountpoint, template_img_path):
    if vpsmountpoint[-1] != '/':
        vpsmountpoint += "/"
    template_mount = mount_loop_tmp(template_img_path, readonly=True)
    if template_mount[-1] != '/':
        template_mount += "/"
    try:
        call_cmd("rsync -a '%s/' '%s/'" % (template_mount, vpsmountpoint))
    finally:
        umount_tmp(template_mount)
Example #8
0
def unpack_tarball(vpsmountpoint, tarball_path):
    pwd = os.getcwd()
    os.chdir(vpsmountpoint)
    try:
        if re.match("^.*\.(tar\.gz|tgz)$", tarball_path):
            call_cmd("tar zxf '%s'" % (tarball_path))
        elif re.match('^.*\.(tar\.bz2|tbz2)$', tarball_path):
            call_cmd("tar jxf '%s'" % (tarball_path))
    finally:
        os.chdir(pwd)
Example #9
0
def vg_space(vg_name):
    out = call_cmd("vgs --noheadings -o vg_free --units g --nosuffix /dev/%s" %
                   (vg_name))
    out = out.strip()
    free_space = int(float(out))
    out = call_cmd("vgs --noheadings -o vg_size --units g --nosuffix /dev/%s" %
                   (vg_name))
    out = out.strip()
    total_space = int(float(out))
    return free_space, total_space
Example #10
0
def lv_snapshot(dev, snapshot_name, vg_name):
    snapshot_dev = "/dev/%s/%s" % (vg_name, snapshot_name)
    if not os.path.exists(dev):
        raise Exception("%s not exists" % (dev))
    if os.path.exists(snapshot_dev):
        raise Exception("%s already exists" % (snapshot_dev))
    size = lv_getsize(dev)
    call_cmd("lvcreate --name %s --size %dG -s %s" %
             (snapshot_name, size, dev))
    return snapshot_dev
Example #11
0
def mount_partition_tmp (dev_path, readonly=False):
    tmp_mount = tempfile.mkdtemp (prefix='mountpoint')
    try:
        if readonly:
            call_cmd ("mount %s %s -o ro" % (dev_path, tmp_mount))
        else:
            call_cmd ("mount %s %s" % (dev_path, tmp_mount))
    except Exception, e:
        os.rmdir (tmp_mount)
        raise e
Example #12
0
def add_rule(net):
    assert net
    ovsops = OVSOps()
    ofport = ovsops.find_ofport_by_name(conf.EXT_INF)
    assert ofport > 0
    call_cmd(
        "ovs-ofctl add-flow %s 'ip,in_port=%s,nw_dst=%s,priority=2000,action=normal'" %
        (conf.XEN_BRIDGE, ofport, net))
    call_cmd("ovs-ofctl add-flow %s 'ip,in_port=%s,priority=10,action=drop'" %
             (conf.XEN_BRIDGE, ofport))
Example #13
0
def mount_loop_tmp(img_path, readonly=False, temp_dir=None):
    """ create temporary mount point and mount loop file """
    tmp_mount = mkdtemp("mpl", temp_dir=temp_dir)
    try:
        if readonly:
            call_cmd("mount %s %s -o loop,ro,noatime" % (img_path, tmp_mount))
        else:
            call_cmd("mount %s %s -o loop,noatime" % (img_path, tmp_mount))
    except Exception, e:
        os.rmdir(tmp_mount)
        raise e
Example #14
0
def mount_loop_tmp (img_path, readonly=False):
    """ create temporary mount point and mount loop file """
    tmp_mount = tempfile.mkdtemp (prefix='mountpoint')
    try:
        if readonly:
            call_cmd ("mount %s %s -o loop,ro" % (img_path, tmp_mount))
        else:
            call_cmd ("mount %s %s -o loop" % (img_path, tmp_mount))
    except Exception, e:
        os.rmdir (tmp_mount)
        raise e
Example #15
0
def mount_partition_tmp(dev_path, readonly=False, temp_dir=None):
    prefix = "mp" + os.path.basename(dev_path)
    tmp_mount = mkdtemp(prefix, temp_dir=temp_dir)
    try:
        if readonly:
            call_cmd("mount %s %s -o ro,noatime" % (dev_path, tmp_mount))
        else:
            call_cmd("mount %s %s -o noatime" % (dev_path, tmp_mount))
    except Exception, e:
        os.rmdir(tmp_mount)
        raise e
Example #16
0
def format_fs(fs_type, target):
    if fs_type in ['ext4', 'ext3', 'ext2']:
        mkfs_cmd = "mkfs.%s -F" % (fs_type)
    elif fs_type == 'reiserfs':
        mkfs_cmd = "mkfs.reiserfs -f"
    elif fs_type in ['swap']:
        mkfs_cmd = "mkswap"
    elif fs_type == 'raw':
        pass
    else:
        raise Exception("not supported fs_type %s" % (fs_type))
    call_cmd("%s %s" % (mkfs_cmd, target))
Example #17
0
 def unset_traffic_limit(self, if_name):
     qos_rows = self.ovsdb.find(['qos'], 'Port', {'name': if_name})
     qos = None
     if qos_rows and qos_rows[0].has_key('qos'):
         qos = qos_rows[0]['qos']
         if qos:
             #qos_uuid = qos.uuid
             call_cmd("ovs-vsctl -- destroy qos %s -- clear port %s qos" %
                      (if_name, if_name))
             queues = qos.queues.values()
             if queues:
                 for q in queues:
                     call_cmd("ovs-vsctl destroy queue %s" % (q.uuid))
Example #18
0
def main():
    if len(sys.argv) <= 1:
        usage()
        os._exit(0)

    bridge = sys.argv[1]
    cmd = "ovs-ofctl dump-flows %s" % (bridge)
    out = call_cmd(cmd)
    lines = out.split("\n")
    regx = re.compile(r'^.*in_port=(\d+).*$')
    ovsops = OVSOps()
    unused_ofport = dict()
    using_ofport = dict()
    print "of_port that are in used:"
    for line in lines:
        om = re.match(regx, line)
        if not om:
            continue
        of_port = int(om.group(1))
        if of_port < 0:
            continue
        if using_ofport.has_key(of_port) or unused_ofport.has_key(of_port):
            continue
        try:
            if_name = ovsops.ovsdb.find_one(
                'name', 'Interface', {'ofport': of_port})
            print if_name, of_port
            using_ofport[of_port] = 1
        except LookupError, e:
            unused_ofport[of_port] = 1
Example #19
0
 def set_mac_filter(self, bridge, ofport, ips):
     call_cmd("ovs-ofctl del-flows %s in_port=%s" % (bridge, ofport))
     if isinstance(ips, basestring):
         ips = [ips]
     for ip in ips:
         call_cmd(
             "ovs-ofctl add-flow %s in_port=%s,arp,nw_proto=2,nw_src=%s,priority=2,action=normal" %
             (bridge, ofport, ip))
         call_cmd(
             "ovs-ofctl add-flow %s in_port=%s,ip,nw_src=%s,priority=2,action=normal" %
             (bridge, ofport, ip))
     call_cmd(
         "ovs-ofctl add-flow %s in_port=%s,arp,nw_proto=1,priority=2,action=normal" %
         (bridge, ofport))
     call_cmd("ovs-ofctl add-flow %s in_port=%s,priority=1,action=drop" %
              (bridge, ofport))
Example #20
0
def main():
    if len(sys.argv) <= 1:
        usage()
        os._exit(0)

    bridge = sys.argv[1]
    cmd = "ovs-ofctl dump-flows %s" % (bridge)
    out = call_cmd(cmd)
    lines = out.split("\n")
    regx = re.compile(r'^.*in_port=(\d+).*$')
    ovsops = OVSOps()
    unused_ofport = dict()
    using_ofport = dict()
    print "of_port that are in used:"
    for line in lines:
        om = re.match(regx, line)
        if not om:
            continue
        of_port = int(om.group(1))
        if of_port < 0:
            continue
        if using_ofport.has_key(of_port) or unused_ofport.has_key(of_port):
            continue
        try:
            if_name = ovsops.ovsdb.find_one('name', 'Interface',
                                            {'ofport': of_port})
            print if_name, of_port
            using_ofport[of_port] = 1
        except LookupError, e:
            unused_ofport[of_port] = 1
Example #21
0
def set_root_passwd (vps, vps_mountpoint):
    if not vps.root_pw:
        print "orz, root passwd is empty, skip"
        return
    sh_script = """
echo 'root:%s' | /usr/sbin/chpasswd
""" % (vps.root_pw)

    user_data = os.path.join (vps_mountpoint, "root/user_data")
    f = open (user_data, "w")
    try:
        try:
            f.write (sh_script)
        finally:
            f.close ()
        call_cmd ("chroot %s /bin/sh /root/user_data" % (vps_mountpoint)) # chroot's path varies among linux distribution
    finally:
        if os.path.exists (user_data):
            os.remove (user_data)
Example #22
0
def set_root_passwd(xv, vps_mountpoint):
    if not xv.root_pw:
        print "orz, root passwd is empty, skip"
        return
    sh_script = """
echo 'root:%s' | /usr/sbin/chpasswd
""" % (xv.root_pw)

    user_data = os.path.join(vps_mountpoint, "root/user_data")
    f = open(user_data, "w")
    try:
        try:
            f.write(sh_script)
        finally:
            f.close()
        # chroot's path varies among linux distribution
        call_cmd("chroot %s /bin/sh /root/user_data" % (vps_mountpoint))
    finally:
        if os.path.exists(user_data):
            os.remove(user_data)
Example #23
0
 def destroy_limit(self):
     major, minor = vps_common.get_dev_no(self.dev)
     script_file = os.path.join(conf.CGROUP_SCRIPT_DIR,
                                os.path.basename(self.dev))
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_iops_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_iops_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_bps_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     if os.path.isfile(script_file):
         os.remove(script_file)
Example #24
0
 def destroy_limit(self):
     major, minor = vps_common.get_dev_no(self.dev)
     script_file = os.path.join(
         conf.CGROUP_SCRIPT_DIR, os.path.basename(self.dev))
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_iops_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_iops_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.read_bps_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     _cmd = "echo '%s:%s %s' > /sys/fs/cgroup/blkio/blkio.throttle.write_bps_device\n" % (
         major, minor, 0)
     call_cmd(_cmd)
     if os.path.isfile(script_file):
         os.remove(script_file)
Example #25
0
    def _get_tree(path):
        """ return a dict , parsed from xenstore-ls """
        cmd = "xenstore-ls %s" % (path)
        out = call_cmd(cmd)
        lines = out.split("\n")
        _static = {
            'node': {},
            'last_key': None,
            'last_value': None,
            'last_space': 0,
        }
        stack = []

        def __process(space, key, value):
            # print _static['last_space'], space, _static['last_key'],
            # _static['last_value'], key, value
            if _static['last_key'] is None:
                _static['last_space'] = 0
            elif _static[
                    'last_value'] == "" and _static['last_space'] + 1 == space:
                _static['node'][_static['last_key']] = {}
                stack.append(_static['node'])
                _static['node'] = _static['node'][_static['last_key']]
            elif _static['last_space'] > space:
                _static['node'][_static['last_key']] = _static['last_value']
                for i in xrange(0, _static['last_space'] - space):
                    _static['node'] = stack.pop()
            else:
                assert _static['last_space'] == space
                _static['node'][_static['last_key']] = _static['last_value']
            _static['last_key'] = key
            _static['last_value'] = value
            _static['last_space'] = space
            return

        reg_key_value = re.compile(r"^(\s*)([\w\-]+)\s*=\s*\"(.*)\".*$")
        for line in lines:
            om = re.match(reg_key_value, line)
            if not om:
                raise Exception("unexpect format: %s" % (line))
            space = len(om.group(1))
            key = om.group(2)
            value = om.group(3)
            __process(space, key, value)
        __process(0, None, None)
        return _static['node']
Example #26
0
File: xen.py Project: wwq0327/vps
    def _get_tree(path):
        """ return a dict , parsed from xenstore-ls """
        cmd = "xenstore-ls %s" % (path)
        out = call_cmd(cmd)
        lines = out.split("\n")
        _static = {
            'node': {},
            'last_key': None,
            'last_value': None,
            'last_space': 0,
        }
        stack = []

        def __process(space, key, value):
            #print _static['last_space'], space, _static['last_key'], _static['last_value'], key, value
            if _static['last_key'] is None:
                _static['last_space'] = 0
            elif _static[
                    'last_value'] == "" and _static['last_space'] + 1 == space:
                _static['node'][_static['last_key']] = {}
                stack.append(_static['node'])
                _static['node'] = _static['node'][_static['last_key']]
            elif _static['last_space'] > space:
                _static['node'][_static['last_key']] = _static['last_value']
                for i in xrange(0, _static['last_space'] - space):
                    _static['node'] = stack.pop()
            else:
                assert _static['last_space'] == space
                _static['node'][_static['last_key']] = _static['last_value']
            _static['last_key'] = key
            _static['last_value'] = value
            _static['last_space'] = space
            return

        reg_key_value = re.compile(r"^(\s*)([\w\-]+)\s*=\s*\"(.*)\".*$")
        for line in lines:
            om = re.match(reg_key_value, line)
            if not om:
                raise Exception("unexpect format: %s" % (line))
            space = len(om.group(1))
            key = om.group(2)
            value = om.group(3)
            __process(space, key, value)
        __process(0, None, None)
        return _static['node']
Example #27
0
 def set_traffic_limit(self, if_name, bandwidth):
     """ bandwidth in kbps """
     assert isinstance(bandwidth, int)
     call_cmd("ovs-vsctl set interface %s ingress_policing_rate=%d" %
              (if_name, bandwidth))
     if bandwidth > 0:
         call_cmd("ovs-vsctl set interface %s ingress_policing_burst=%d" %
                  (if_name, 100))
         call_cmd(
             ["ovs-vsctl", "--", "set", "Port", if_name, "qos=@newqos", "--",
              "--id=@newqos", "create", "qos", "type=linux-htb", "other-config:max-rate=%d" % (
                  bandwidth * 1000), "queues=0=@q0",
              "--", "--id=@q0", "create", "queue", "other-config:max-rate=%d" % (
                  bandwidth * 1000)
              ])
Example #28
0
def get_fs_type(dev_path):
    dev_path = os.path.realpath(dev_path)
    assert os.path.exists(dev_path)
    out = call_cmd("file -s %s" % (dev_path))
    if re.match(r"^.*ext4.*?$", out, re.I):
        return "ext4"
    elif re.match(r"^.*ext3.*?$", out, re.I):
        return "ext3"
    elif re.match(r"^.*ext2.*?", out, re.I):
        return "ext2"
    elif re.match(r"^.*swap.*?", out, re.I):
        return "swap"
    elif re.match(r"^.*ReiserFS.*?", out, re.I):
        return "reiserfs"
    elif re.match(r"^.*xfs.*?", out, re.I):
        return "xfs"
    elif re.match(r"^.*FAT.*?", out, re.I):
        return "vfat"
    elif re.match(r"^.*NTFS.*?", out, re.I):
        return "ntfs-3g"
    else:
        raise Exception("unknown fs: %s" % (out.strip("\r\n")))
Example #29
0
def lv_resizefs(dev, new_size_g):
    fs_type = get_fs_type(dev)
    if fs_type in ['ext2', 'ext3', 'ext4']:
        _lv_resize(dev, new_size_g)
        call_cmd("resize2fs -f %s" % (dev))
    elif fs_type == 'reiserfs':
        _lv_resize(dev, new_size_g)
        call_cmd("resize_reiserfs -f %s" % (dev))
    elif fs_type == 'xfs':
        _lv_resize(dev, new_size_g)
        mp = mount_partition_tmp(dev)
        try:
            call_cmd("xfs_growfs %s" % (dev))
        finally:
            umount_tmp(mp)
    else:
        raise Exception("unsupported fs_type=%s on %s" % (fs_type, dev))
Example #30
0
 def add_port_to_bridge(self, bridge, if_name):
     call_cmd("ovs-vsctl add-port %s %s" % (bridge, if_name))
Example #31
0
 def del_port_from_bridge(self, bridge, if_name):
     call_cmd("ovs-vsctl del-port %s %s" % (bridge, if_name))
Example #32
0
def get_blk_size(dev):
    out = call_cmd("blockdev --getsize64 %s" % (dev))
    out = out.strip()
    return int(out)
Example #33
0
 def unset_mac_filter(self, bridge, ofport):
     call_cmd("ovs-ofctl del-flows %s in_port=%s" % (bridge, ofport))
Example #34
0
def umount_tmp(tmp_mount):
    """ umount loop file and delete temporary mount point """
    call_cmd("umount -f %s" % (tmp_mount))
    os.rmdir(tmp_mount)
Example #35
0
def gen_mac():
    cmd = """(date; cat /proc/interrupts) | md5sum | sed -r 's/^(.{6}).*$/\\1/; s/([0-9a-f]{2})/\\1:/g; s/:$//;'"""
    s = "00:16:3e:"
    out = call_cmd(cmd)
    return s + out
Example #36
0
def xm_network_detach(domain, mac):
    call_cmd("xm network-detach %s %s" % (domain, mac))
Example #37
0
def _lv_resize(dev, new_size_g):
    assert isinstance(new_size_g, int)
    call_cmd("lvresize --size %dG %s" % (new_size_g, dev))
Example #38
0
File: xen.py Project: wwq0327/vps
 def mem_free():
     """ return mem free (MB) in Xen """
     out = call_cmd("xm info | grep free_memory")
     return int(out.strip("\r\n").split(":")[1].strip())
Example #39
0
File: vps_ops.py Project: 42qu/vps
    def _upgrade(self, xv_new, xv_old):
        # TODO: check hard disk downgrade size
        _vps_image, os_type, os_version = os_image.find_os_image(xv_new.os_id)
        fs_type = vps_common.get_fs_from_tarball_name(_vps_image)
        if not fs_type:
            fs_type = conf.DEFAULT_FS_TYPE

        for xen_dev, new_disk in xv_new.data_disks.iteritems():
            old_disk = xv_old.data_disks.get(xen_dev)
            if not new_disk.exists():
                new_disk.create(fs_type)
                self.loginfo(xv_new, "create %s" % (str(new_disk)))
            else:
                assert new_disk.size_g
                if old_disk:
                    old_size = old_disk.get_size()
                    new_size = new_disk.size_g
                    if old_size == new_size:
                        pass
                    elif old_size == new_size:
                        pass
                    elif old_size < new_size and new_disk.can_resize():
                        new_disk.destroy_limit()
                        new_disk.resize(new_disk.size_g)
                        new_disk.create_limit()
                        self.loginfo(xv_new, "resized %s from %s to %s" %
                                     (str(new_disk), old_size, new_size))
                    else:
                        old_disk, new_disk = xv_new.renew_storage(
                            xen_dev, new_size=new_disk.size_g)
                        vps_mountpoint_bak = old_disk.mount_trash_temp()
                        self.loginfo(xv_new, "mounted %s" %
                                     (old_disk.trash_str()))
                        try:
                            fs_type = vps_common.get_mounted_fs_type(
                                mount_point=vps_mountpoint_bak)
                            new_disk.create(fs_type)
                            new_disk.destroy_limit()
                            self.loginfo(xv_new, "create new %s" %
                                         (str(new_disk)))
                            vps_mountpoint = new_disk.mount_tmp()
                            self.loginfo(xv_new, "mounted %s" %
                                         (str(new_disk)))
                            try:
                                call_cmd("rsync -a '%s/' '%s/'" %
                                         (vps_mountpoint_bak, vps_mountpoint))
                                self.loginfo(
                                    xv_new, "synced old %s to new one" %
                                    (str(new_disk)))
                            finally:
                                vps_common.umount_tmp(vps_mountpoint)
                            new_disk.create_limit()
                        finally:
                            vps_common.umount_tmp(vps_mountpoint_bak)

        for xen_dev, old_disk in xv_old.data_disks.iteritems():
            if not xv_new.data_disks.has_key(xen_dev):
                xv_new.data_disks[xen_dev] = old_disk
                xv_new.dump_storage_to_trash(old_disk)
                self.loginfo(xv_new, "%s dump to trash" % (str(old_disk)))
        self.create_xen_config(xv_new)
        self.loginfo(xv_new, "begin to init os")
        vps_mountpoint = xv_new.root_store.mount_tmp()
        try:
            os_init.os_init(xv_new, vps_mountpoint, os_type, os_version,
                            is_new=False, to_init_passwd=False, to_init_fstab=True)
            self.loginfo(xv_new, "done init os")
        finally:
            vps_common.umount_tmp(vps_mountpoint)
Example #40
0
def lv_getsize(dev):
    out = call_cmd("lvs --noheadings -o lv_size --units g %s" % (dev))
    out = out.strip()
    out = out.strip("gG")
    return float(out)
Example #41
0
def lv_rename(src_dev, dest_dev):
    call_cmd("lvrename %s %s " % (src_dev, dest_dev))
Example #42
0
def lv_delete(lv_dev):
    call_cmd("lvremove -f %s" % (lv_dev))
Example #43
0
File: vps_ops.py Project: 42qu/vps
    def reinstall_os(self, vps_id, _xv=None, os_id=None, vps_image=None):
        meta_path = self._meta_path(vps_id, is_trash=False)
        xv = None
        if os.path.exists(meta_path):
            xv = self._load_vps_meta(meta_path)
            if os_id:
                xv.os_id = os_id
            elif _xv:
                xv.os_id = _xv.os_id
                self._update_vif_setting(xv, _xv)
            else:
                raise Exception("missing os_id")
        elif _xv:
            xv = _xv
            if os_id:
                xv.os_id = os_id
            raise Exception("missing vps metadata")
        _vps_image, os_type, os_version = os_image.find_os_image(xv.os_id)
        if not vps_image:
            vps_image = _vps_image
        if not vps_image:
            raise Exception(
                "no template image configured for os_type=%s, os_id=%s" %
                (os_type, xv.os_id))
        if not os.path.exists(vps_image):
            raise Exception("image %s not exists" % (vps_image))
        # fs_type is tied to the image
        fs_type = vps_common.get_fs_from_tarball_name(vps_image)
        if not fs_type:
            fs_type = conf.DEFAULT_FS_TYPE

        assert xv.has_all_attr
        if xv.stop():
            self.loginfo(xv, "stopped")
        else:
            xv.destroy()
            self.loginfo(xv, "force destroy")
        time.sleep(3)
        root_store_trash, root_store = xv.renew_storage(
            xv.root_store.xen_dev, 5)
        xv.root_store.create(fs_type)
        self.loginfo(xv, "create new root")

        # we have to know fs_type for fstab generation
        for xen_dev, disk in xv.data_disks.iteritems():
            if xen_dev == xv.root_store.xen_dev:
                continue
            if disk.exists():
                pass
            else:
                disk.create(fs_type)

        vps_mountpoint_bak = root_store_trash.mount_trash_temp()
        try:
            vps_mountpoint = xv.root_store.mount_tmp()
            self.loginfo(xv, "mounted vps image %s" % (str(xv.root_store)))

            try:
                xv.root_store.destroy_limit()
                if re.match(r'.*\.img$', vps_image):
                    vps_common.sync_img(vps_mountpoint, vps_image)
                else:
                    vps_common.unpack_tarball(vps_mountpoint, vps_image)
                self.loginfo(xv, "synced vps os to %s" % (str(xv.root_store)))

                for sync_dir in ['home', 'root']:
                    dir_org = os.path.join(vps_mountpoint_bak, sync_dir)
                    dir_now = os.path.join(vps_mountpoint, sync_dir)
                    if os.path.exists(dir_org):
                        call_cmd("rsync -a --exclude='.bash*'  '%s/' '%s/'" %
                                 (dir_org, dir_now))
                        self.loginfo(xv, "sync dir /%s to new os" % (sync_dir))

                self.loginfo(xv, "begin to init os")
                if _xv:
                    xv.root_pw = _xv.root_pw
                    os_init.os_init(xv, vps_mountpoint, os_type, os_version,
                                    is_new=True, to_init_passwd=True, to_init_fstab=True)
                else:  # if no user data provided from backend
                    os_init.os_init(xv, vps_mountpoint, os_type, os_version,
                                    is_new=True, to_init_passwd=False, to_init_fstab=True,)
                    os_init.migrate_users(
                        xv, vps_mountpoint, vps_mountpoint_bak)
                self.loginfo(xv, "done init os")
                xv.root_store.create_limit()
            finally:
                vps_common.umount_tmp(vps_mountpoint)
        finally:
            vps_common.umount_tmp(vps_mountpoint_bak)

        self.create_xen_config(xv)
        self._boot_and_test(xv, is_new=False)
        self.loginfo(xv, "done vps reinstall")
Example #44
0
def xm_network_attach(domain, vifname, mac, ip, bridge):
    call_cmd("xm network-attach %s mac=%s ip=%s vifname=%s bridge=%s" %
             (domain, mac, ip, vifname, bridge))