Skip to content

Instantly share code, notes, and snippets.

@Bharat-B
Created March 24, 2021 14:19
Show Gist options
  • Save Bharat-B/1098eedaeeeca48b6bdf087bf94c540a to your computer and use it in GitHub Desktop.
Save Bharat-B/1098eedaeeeca48b6bdf087bf94c540a to your computer and use it in GitHub Desktop.
A virtualizor dependent php script to regenerate proxmox vm configs for kvm.
<?php
# Execution /usr/local/emps/bin/php /usr/local/virtualizor/prox_gen.php "server_id"
include_once 'scripts/inc.php';
$res = makequery("
SELECT
vps_name as conf,
disks.bus_driver as diskdriver,
CONCAT(disks.bus_driver,0) as bootdisk,
hostname,
boot,
cpu as cpuunits,
cores,
cores as cpulimit,
ram as memory,
CONCAT(nic_type,'=',mac,',bridge=vmbr0') as net0,
numa,
CONCAT(disks.bus_driver,'0:',disks.path,',','size=',vps.space,'G') as vmdisk,
disks.type as vmdisk_type,
storage.name, storage.path
FROM vps
JOIN disks ON vps.uuid = disks.vps_uuid
JOIN storage on disks.st_uuid = `storage`.st_uuid
WHERE serid = '{$argv[1]}'
");
for ($i = 0; $i < vsql_num_rows($res); $i++) {
$_res = vsql_fetch_assoc($res);
if ($_res['vmdisk_type'] == "zfs block") {
$p_arr = explode("/", $_res['path']);
$zfs_block = explode("/", $_res['vmdisk']);
$zfs_block = end($zfs_block);
$vmdisk = "{$_res['name']}:{$zfs_block}";
if ($_res['diskdriver'] == 'scsi') {
$vmdisk .= ',discard=on';
$vmdisk .= "\nscsihw: virtio-scsi-pci";
}
} else {
$p_arr = explode("/", $_res['vmdisk']);
$vmdisk = $_res['name'] . ':' . "{$p_arr[5]}" . '/' . end($p_arr);
}
$config = "
boot: {$_res['boot']}\n
bootdisk: {$_res['bootdisk']}\n
cores: {$_res['cores']}\n
cpu: kvm64\n
cpulimit: {$_res['cores']}\n
memory: {$_res['memory']}\n
name: {$_res['hostname']}\n
net0: {$_res['net0']}\n
numa: {$_res['numa']}\n
onboot: 1\n
{$_res['bootdisk']}: {$vmdisk}";
file_put_contents("bharat/{$_res['conf']}.conf", $config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment