示例#1
0
/**
 * duplicate an existing vm to a given lab id
 *
 * @param   int $from_vm_id vm id
 * @param   int $to_lab_id  lab id
 *
 * @return  int|bool            new vm id | FALSE on error
 */
function ace_db_vm_duplicate($from_vm_id, $to_lab_id)
{
    $from_vm_info = ace_db_vm_get_info($from_vm_id);
    $to_vm_instance = $from_vm_info['instance'];
    $to_vm_name = str_pad($to_lab_id, 5, '0', STR_PAD_LEFT) . '-vm-' . str_pad($to_vm_instance, 2, '0', STR_PAD_LEFT);
    $to_vm_virt_id = $to_vm_name;
    $sql = "INSERT INTO `vm` (\n\t\t\t\t`lab_id`,\n\t\t\t\t`instance`,\n\t\t\t\t`name`,\n\t\t\t\t`virt_id`,\n\t\t\t\t`display_name`,\n\t\t\t\t`vcpu`,\n\t\t\t\t`memory`,\n\t\t\t\t`unit`,\n\t\t\t\t`arch`,\n\t\t\t\t`profile`,\n\t\t\t\t`user_visible`,\n\t\t\t\t`state`)\n\t\t\tVALUES (\n\t\t\t\t{$to_lab_id},\n\t\t\t\t{$to_vm_instance},\n\t\t\t\t'{$to_vm_name}',\n\t\t\t\t'{$to_vm_virt_id}',\n\t\t\t\t'" . $from_vm_info['display_name'] . "',\n\t\t\t\t" . $from_vm_info['vcpu'] . ",\n\t\t\t\t" . $from_vm_info['memory'] . ",\n\t\t\t\t'" . $from_vm_info['unit'] . "',\n\t\t\t\t'" . $from_vm_info['arch'] . "',\n\t\t\t\t'" . $from_vm_info['profile'] . "',\n\t\t\t\t" . $from_vm_info['user_visible'] . ",\n\t\t\t\t" . $from_vm_info['state'] . ")";
    $db_result = ace_db_query($sql);
    if ($db_result->last_insert_id != 0) {
        $to_vm_id = $db_result->last_insert_id;
        $from_vm_vnic_table = ace_db_vm_get_nic_table($from_vm_id);
        foreach ($from_vm_vnic_table as $from_vm_vnic) {
            $from_vm_vnic_network_info = ace_db_network_get_info($from_vm_vnic['network_id']);
            if ($from_vm_vnic_network_info['lab_id'] == 0) {
                $to_vm_vnic_network_id = $from_vm_vnic['network_id'];
            } else {
                $to_vm_vnic_network_id = ace_db_network_get_id_by_lab_instance($to_lab_id, $from_vm_vnic_network_info['instance']);
            }
            //$sql = "INSERT INTO vnic (
            //		`vm_id`,
            //		`instance`,
            //		`network_id`)
            //	VALUES (
            //		$to_vm_id,
            //		" . $from_vm_vnic['instance'] . ",
            //		" . $to_vm_vnic_network_id . ")";
            $sql = "INSERT INTO vnic (\n\t\t\t\t\t\t`vm_id`,\n\t\t\t\t\t\t`instance`,\n\t\t\t\t\t\t`mac_index`,\n\t\t\t\t\t\t`network_id`)\n\t\t\t\t\tVALUES (\n\t\t\t\t\t\t{$to_vm_id},\n\t\t\t\t\t\t" . $from_vm_vnic['instance'] . ",\n\t\t\t\t\t\t(SELECT t1.mac_index + 1 AS mac_index\n                            FROM `vnic` AS t1\n                            LEFT JOIN `vnic` AS t2 ON t1.mac_index + 1 = t2.mac_index\n                            WHERE t2.mac_index IS NULL\n                                AND t1.mac_index >= " . _MAC_POOL_INDEX_START_ . "\n                                AND t1.mac_index < " . _MAC_POOL_INDEX_END_ . "\n                            ORDER BY t1.mac_index\n                            LIMIT 1\n                        ),\n\t\t\t\t\t\t" . $to_vm_vnic_network_id . ")";
            ace_db_query($sql);
            //$db_result = ace_db_query($sql);
            //if ($db_result->last_insert_id != 0) {
            //	$to_vm_vnic_id = $db_result->last_insert_id;
            //} else {
            //	$to_vm_vnic_id = FALSE;
            //}
        }
        $from_vm_vcdrom_table = ace_db_vm_get_cdrom_table($from_vm_id);
        foreach ($from_vm_vcdrom_table as $from_vm_vcdrom) {
            $sql = "INSERT INTO vcdrom (\n\t\t\t\t\t\t`vm_id`,\n\t\t\t\t\t\t`instance`,\n\t\t\t\t\t\t`volume_id`)\n\t\t\t\t\tVALUES (\n\t\t\t\t\t\t{$to_vm_id},\n\t\t\t\t\t\t" . $from_vm_vcdrom['instance'] . ",\n\t\t\t\t\t\t" . ($from_vm_vcdrom['volume_id'] != NULL ? $from_vm_vcdrom['volume_id'] : 'NULL') . ")";
            ace_db_query($sql);
            //$db_result = ace_db_query($sql);
            //if ($db_result->last_insert_id != 0) {
            //	$to_vm_vcdrom_id = $db_result->last_insert_id;
            //} else {
            //	$to_vm_vcdrom_id = FALSE;
            //}
        }
        $from_vm_vdisk_table = ace_db_vm_get_disk_table($from_vm_id);
        foreach ($from_vm_vdisk_table as $from_vm_vdisk) {
            $from_vm_vdisk_volume_info = ace_db_volume_get_info($from_vm_vdisk['volume_id']);
            $from_vm_vdisk_volume_instance = $from_vm_vdisk_volume_info['instance'];
            # find volume_id by lab and instance
            $to_vm_vdisk_volume_id = ace_db_volume_get_id_by_lab_instance($to_lab_id, $from_vm_vdisk_volume_instance);
            # find new volume_id backed by from_vm_vdisk['volume_id']
            /* $sql = "SELECT id
               FROM volume
               WHERE lab_id=$to_lab_id
                   AND base_id=" .	$from_vm_vdisk['volume_id']; */
            // echo '$sql' . d($sql);
            // $db_result = ace_db_query($sql);
            // echo '$db_result' . d($db_result);
            // $to_vm_vdisk_volume_id = $db_result->table[0]['id'];
            // echo '$to_vm_vdisk_volume_id' . d($to_vm_vdisk_volume_id);
            $sql = "INSERT INTO vdisk (\n\t\t\t\t\t\t`vm_id`,\n\t\t\t\t\t\t`instance`,\n\t\t\t\t\t\t`volume_id`)\n\t\t\t\t\tVALUES (\n\t\t\t\t\t\t{$to_vm_id},\n\t\t\t\t\t\t" . $from_vm_vdisk['instance'] . ",\n\t\t\t\t\t\t{$to_vm_vdisk_volume_id})";
            // echo '$sql' . d($sql);
            ace_db_query($sql);
            //$db_result = ace_db_query($sql);
            // echo '$db_result' . d($db_result);
            //if ($db_result->last_insert_id != 0) {
            //	$to_vm_vdisk_id = $db_result->last_insert_id;
            //} else {
            //	$to_vm_vdisk_id = FALSE;
            //}
        }
    } else {
        $to_vm_id = FALSE;
    }
    return $to_vm_id;
}
示例#2
0
/**
 * returns a volume information record
 *
 * @api
 *
 * @param   int $volume_id volume id
 *
 * @return array|bool           volume information | FALSE on error
 */
function ace_volume_get_info($volume_id)
{
    return ace_db_volume_get_info($volume_id);
}