#!/bin/sh
#uuid='14278884-9156-498f-b2b0-9bf43b4ae2c8'
#system_type='windows'
#echo $1
#echo $2
uuid=$1
system_type=$2
#get root lists
function get_rbd_root()
{
root_path=`guestfish <<_EOF_
set-attach-method appliance
add-drive /dev/null
config -set drive.hd0.file=rbd:$rbd_path
run
inspect-os
_EOF_`
}
function get_file_root()
{
root_path=`guestfish <<_EOF_
add $disk_path
run 
inspect-os
_EOF_`
}
#file windows
function file_windows()
{
guestfish <<_EOF_
add $disk_path
run 
list-filesystems
mount $root_path /
write win:\Windows\Data.ini ''
_EOF_
}
#file linux
function file_linux()
{
guestfish <<_EOF_
add $disk_path
run 
list-filesystems
mount $root_path /
write /etc/drive/drive.conf 'passwd=0'
_EOF_
}
#rbd windows
function rbd_windows()
{
guestfish  <<_EOF_
set-attach-method appliance
add-drive /dev/null
config -set drive.hd0.file=rbd:$rbd_path
run
list-filesystems
mount $root_path /
write win:\Windows\Data.ini ''
_EOF_
}
#rbd linux
function rbd_linux()
{
guestfish  <<_EOF_
set-attach-method appliance
add-drive /dev/null
config -set drive.hd0.file=rbd:$rbd_path
run
list-filesystems
mount $root_path /
write /etc/drive/drive.conf 'passwd=0'
_EOF_
}
#virsh dumpxml $uuid |grep disk 
#get first disk type
type=`virsh dumpxml $uuid | grep 'disk' |head -1 | cut -d"'" -f2`
if [ $type = "network" ];then
echo "netwok"
#get first disk path
rbd_path=`virsh dumpxml $uuid | grep 'source protocol' |head -1 |awk 'NR=1 {print $3}' |cut -d"'" -f2`
if [ $system_type = "windows" ];then
echo "rbd_windows"
#rbd_windows
get_rbd_root
#echo $root_path
rbd_windows
elif [ $system_type = "linux" ];then
echo "rbd_linux"
get_rbd_root
#echo $root_path
rbd_linux
fi
elif [ $type = "file" ];then
#get first disk disk
echo "file"
disk_path=`virsh dumpxml $uuid | grep 'source file' | head -1 |cut -d"'" -f2` 
if [ $system_type = "windows" ];then
echo "file_windows"
get_file_root
file_windows
elif [ $system_type = "linux" ];then
echo "file_linux"
get_file_root
file_linux
fi
else
echo "null"
fi

