Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Created June 17, 2014 14:23
Show Gist options
  • Save Akkiesoft/fd33d0c0c0e6da73a34e to your computer and use it in GitHub Desktop.
Save Akkiesoft/fd33d0c0c0e6da73a34e to your computer and use it in GitHub Desktop.
libvirtな仮想マシンのXMLのUUIDとMACアドレスを適当に書き換えるPHPスクリプト
<?php
$file = $argv[1];
$xml = simplexml_load_file($file);
# uniqidはそれほどいい感じでないので、いい感じのコマンドを活用する
$uuid = exec("uuidgen");
$xml->uuid = $uuid;
foreach($xml->devices->interface as $interface) {
# uniqidはそれほどいい感じでないので、ハッシュにしていい感じにする
$newmac = wordwrap(hash("sha256", uniqid(mt_rand())), 2, ":", true);
# Vendor ID + New random ID
$newmac = "52:54:00:" . substr($newmac, 0, 8);
$interface->mac->attributes()->address = $newmac;
}
# ディスクのパスを変えたいときは例えばこんなかんじで
# foreach($xml->devices->disk as $disk) {
# $disk->source->attributes()->file = str_replace("/var/lib/libvirt/images", "/path/to/new/data/store", $disk->source->attributes()->file);
# }
# 上書きが嫌だったら適当にprefixつけたらよさげ
$xml->asXml($file);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment