#!/bin/zsh
export src=$1
export disk=$2
export type=$3
echo source image: $src
if [ -e $disk ];then
echo target disk: $disk
else
echo cannot locate target disk, mistyped device name\?
exit 2
fi
echo source image type: $type
if lsmod|grep -qw nbd;then
sleep .01
else
echo loading nbd module
modprobe nbd
fi
echo connecting $src to /dev/nbd0
qemu-nbd -r -c /dev/nbd0 $src
echo loading partition table for /dev/nbd0
partprobe /dev/nbd0
if echo $type|grep -qw w;then
export boottype=uefi
case "$boottype" in
legacy)
echo creating legacy partition table for $disk
cat > /tmp/partmap<<EOF
o
n
p


206847
t
1
ef
n
p


239615
t
2
da
n
p


-510M
t
3
07
n
p



t
4
27
a
3
w
EOF
;;
uefi)
echo creating uefi partition table for $disk
cat > /tmp/partmap<<EOF
g
n


206847
t
1
1
n


239616
t
2
10
n


-510M
t
3
11
n



t
4
14
w
EOF
;;
esac
echo writing partition table to $disk
cat /tmp/partmap|fdisk $disk > /dev/null 2>/dev/null
sleep .5
partprobe $disk
fdisk -l $disk -o type|sed "/Disk identifier/d;/Disklabel type/d;/I\/O size/d;/Sector size/d;/Units: sectors/d;/Disk \//d;s|Type|\\n|g"|tr -s \\n>/tmp/partlist
export partlen=`cat /tmp/partlist|wc -l`
cat /tmp/partlist|tail -n $(($partlen-1))>/tmp/partmap
export maxparts=`wc -l /tmp/partmap|cut -f 1 -d \  `
export counter=1
while [ $counter -le $maxparts ];do
export desc=`cat /tmp/partmap|head -n $counter|tail -n 1`
if [ -e $disk"-part"$counter ];then
export dstpart=$disk"-part"$counter
elif [ -e $disk"p"$counter ];then
export dstpart=$disk"p"$counter
else
export dstpart=$disk$counter
fi
if [ -e /dev/nbd0p$counter ];then
while true;do
echo writing $desc partition to $dstpart
if qemu-img convert -p /dev/nbd0p$counter $dstpart;then
break
else
echo disconnecting due to write error
qemu-nbd -d /dev/nbd0
echo reconnecting $src to /dev/nbd0
qemu-nbd -s -c /dev/nbd0 $src
echo loading partition table for /dev/nbd0
partprobe /dev/nbd0
continue
fi
done
if blkid $dstpart|grep -iqw ntfs;then
echo resizing ntfs partition $dstpart
echo y|ntfsresize -f $dstpart > /dev/null 2>/dev/null
fi
fi
export counter=$(($counter+1))
done
rm /tmp/partlist /tmp/partmap
else
echo replicating source partition table
sgdisk -R $disk /dev/nbd0
partprobe $disk
fdisk -l $disk -o type|sed "/Disk identifier/d;/Disklabel type/d;/I\/O size/d;/Sector size/d;/Units: sectors/d;/Disk \//d;s|Type|\\n|g"|tr -s \\n>/tmp/partlist
export partlen=`cat /tmp/partlist|wc -l`
cat /tmp/partlist|tail -n $(($partlen-1))>/tmp/partmap
export maxparts=`wc -l /tmp/partmap|cut -f 1 -d \  `
export counter=1
while [ $counter -le $maxparts ];do
export desc=`cat /tmp/partmap|head -n $counter|tail -n 1`
if [ -e $disk"-part"$counter ];then
export dstpart=$disk"-part"$counter
elif [ -e $disk"p"$counter ];then
export dstpart=$disk"p"$counter
else
export dstpart=$disk$counter
fi
if [ -e /dev/nbd0p$counter ];then
while true;do
echo writing $desc partition to $dstpart
if qemu-img convert -p /dev/nbd0p$counter $dstpart;then
break
else
echo disconnecting due to write error
qemu-nbd -d /dev/nbd0
echo reconnecting $src to /dev/nbd0
qemu-nbd -r -c /dev/nbd0 $src
echo loading partition table for /dev/nbd0
partprobe /dev/nbd0
continue
fi
done
fi
export counter=$(($counter+1))
done
rm /tmp/partlist /tmp/partmap
fi
echo disconnecting source
qemu-nbd -d /dev/nbd0
if echo $type|grep -qw w;then
if [ -e /net/.scripts/bcdbooter ];then
cd /net/.scripts
./bcdbooter $disk
else
qemu-nbd -t -f raw -x disk $disk &
while true;do
if pgrep -af qemu-nbd;then
break
else
continue
fi
done
curl -s http://192.168.120.1/bootloader > /dev/null
sleep 2
while true;do
if curl -s http://192.168.120.1/bootloaderstatus|grep -qw _done_;then
killall -9 qemu-nbd
break
else
sleep 2
continue
fi
done
fi
sync
case "$boottype" in
legacy)
echo installing windows mbr
ms-sys -7 $disk > /dev/null 2>/dev/null
;;
uefi)
echo clearing boot entries from nvram
for b in `efibootmgr|grep \*|cut -f 1 -d \  |sed "s|Boot||g;s|\*||g"`;do
efibootmgr -b $b -B > /dev/null 2>/dev/null
done
efibootmgr -O > /dev/null 2>/dev/null
;;
esac
else
echo clearing boot entries from nvram
for b in `efibootmgr|grep \*|cut -f 1 -d \  |sed "s|Boot||g;s|\*||g"`;do
efibootmgr -b $b -B > /dev/null 2>/dev/null
done
efibootmgr -O > /dev/null 2>/dev/null
echo checking for image script
curl -Lo postimage http://192.168.120.1/`basename $src`.sh
if cat postimage|head -n 1 | grep \<html;then
echo no valid script detected
rm postimage
else
chmod 700 postimage
echo post image script running
./postimage $@
fi
fi
echo syncing disks
sync
echo rebooting
reboot -f
