Skip to content

Commit

Permalink
scripts: Add Windows disk clone script
Browse files Browse the repository at this point in the history
Signed-off-by: Tu Dinh <[email protected]>
  • Loading branch information
Tu Dinh committed Jan 11, 2025
1 parent 295cd81 commit 0e982f5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions scripts/guests/windows/win-diskclone.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set -eu

src=$1
dst=$2

if [ "$(blockdev --getsz $src)" != "$(blockdev --getsz $dst)" ]
then
echo "Disks are not the same size!"
exit 1
fi

if (sfdisk -d $dst > /dev/null)
then
echo "Destination contains partition table! Stopping"
exit 1
fi

echo "Cloning partition table"
sfdisk -d $src | sfdisk $dst

echo "Cloning non-data partitions"
for srcpart in $(sfdisk -d $src |
grep start= |
grep -v type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 |
cut -d ':' -f 1)
do
dstpart=${srcpart/"$src"/"$dst"}
echo "Cloning $srcpart to $dstpart"
pv $srcpart > $dstpart
done

echo "Cloning data partitions"
for srcpart in $(sfdisk -d $src |
grep type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 |
cut -d ':' -f 1)
do
dstpart=${srcpart/"$src"/"$dst"}
echo "Deleting pagefiles"
mkdir -p /mnt/$srcpart && mount $srcpart /mnt/$srcpart && find /mnt/$srcpart -maxdepth 1 -iname pagefile.sys -or -iname hiberfil.sys -or -iname swapfile.sys -delete
umount /mnt/$srcpart
echo "Cloning $srcpart to $dstpart"
ntfsclone -O $dstpart $srcpart
done

0 comments on commit 0e982f5

Please sign in to comment.