Created
January 4, 2021 21:55
-
-
Save hotsphink/64465e136ce4c5110ea99552eb725f85 to your computer and use it in GitHub Desktop.
Horrific script for creating and updating a view onto a live Windows disk for use with a VM, also for mounting said disk image onto a device for use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/zsh | |
LOOP_START=loop2 | |
LOOP_BOOT=loop3 | |
LOOP_END=loop4 | |
MD=md0 | |
if [ "$1" = "--list" ]; then | |
if [ -e /dev/$MD ]; then | |
sudo mdadm --detail /dev/$MD | perl -lane 'if (/^\s*Number/) { $active = 1 } elsif ($active) { print $F[-1]}' | while read DEV; do | |
case $DEV in /dev/loop*) losetup $DEV;; *) echo $DEV;; esac | |
done | |
else | |
echo "/dev/$MD does not exist" | |
fi | |
exit 0 | |
fi | |
set -x | |
if [ "$1" = "-d" ]; then | |
sudo mdadm --remove /dev/$MD | |
sudo mdadm --stop /dev/$MD | |
sudo losetup -d /dev/$LOOP_START | |
sudo losetup -d /dev/$LOOP_END | |
sudo losetup -d /dev/$LOOP_BOOT | |
exit 0 | |
fi | |
cd "$(dirname $0)"/'../VirtualBox VMs/work' | |
if [ "$1" = "--createmirror" ]; then | |
DISK="$2" | |
# Grab the first 3 partitions, which we expect to be (looking at types only): | |
# | |
# /dev/sda1 2048 923647 921600 450M Windows recovery environment | |
# /dev/sda2 923648 1126399 202752 99M EFI System | |
# /dev/sda3 1126400 1159167 32768 16M Microsoft reserved | |
# | |
# So we want the end of the 3rd one there (1159167) | |
set -e | |
base=$(basename "$DISK") | |
# If this fails, delete the directory and start over. | |
mkdir "$base" | |
# Verify the partition types | |
diff -u $(dirname "$0")/viewsetup.expect-first-3-partition-types.txt <(sudo fdisk -l -oType "$DISK" | perl -ne 'print unless 1../^Type/' | head -3) | |
# Grab out the end sector and add 1 | |
NUM_SECTORS=$(( 1 + $(sudo fdisk -l -oEnd "$DISK" | perl -ne 'print unless 1../^\s*End/' | head -3 | tail -1) )) | |
[ $NUM_SECTORS -gt 0 ] | |
# Read out the full data of the initial 3 partitions (and boot sector). | |
startname="$base/$base.start.$NUM_SECTORS.dat" | |
sudo dd if="$DISK" of="$startname" count="$NUM_SECTORS" | |
# Read out the tail of the disk. | |
fullsize=$(sudo fdisk -l "$DISK" | head -1 | perl -lne 'print $1 if /(\d+) sectors/') | |
lastpart=$(sudo fdisk -l -oEnd "$DISK" | tail -1) | |
tail_start=$(perl -le "print 1 + $lastpart") | |
tail_sectors=$(perl -le "print $fullsize - $lastpart") | |
tailname="$base/$base.tail.$tail_sectors.dat" | |
#echo "Skipping the tail" | |
#sudo dd if="$DISK" of="$tailname" skip="$tail_start" | |
# Construct the mapping file. | |
echo "$startname" > $base/mapping.txt | |
sudo fdisk -l -oDevice "$DISK" | perl -ne 'print unless 1../^Device/' | tail -n +4 >> $base/mapping.txt | |
#echo "$tailname" >> $base/mapping.txt | |
exit 0 | |
fi | |
if [ "$1" = "--createvmdk" ]; then | |
shift | |
out="${1:-nvme0n1}".vmdk | |
set -e | |
VBoxManage internalcommands createrawvmdk -filename "$out" -rawdisk /dev/$MD | |
echo "Created $out" | |
exit 0 | |
fi | |
sudo modprobe linear | |
DISKDIR="${1:-nvme0n1}" | |
export MD | |
perl -l <<'EOF' - "$DISKDIR"/mapping.txt $LOOP_START $LOOP_BOOT $LOOP_END | |
@loops = @ARGV; | |
@ARGV = (shift(@loops)); | |
while (<>) { | |
chomp; | |
if (m!^/dev!) { | |
push @pieces, $_; | |
} else { | |
my $loop = "/dev/" . shift(@loops); | |
system("sudo", "losetup", $loop, $_); | |
push @pieces, $loop; | |
} | |
} | |
my $md = "/dev/$ENV{MD}"; | |
system("sudo", "mdadm", | |
"--build", $md, | |
"--level=linear", | |
"--raid-devices=" . @pieces, | |
@pieces); | |
die "mdadm failed" if $?; | |
system("sudo", "chown", "sfink:sfink", $md); | |
die "chown failed" if $?; | |
sleep(2); | |
system("sudo", "chmod", "0666", $md); | |
die "chmod failed" if $?; | |
EOF | |
exit 0 | |
########### OLD ############ | |
sudo losetup /dev/$LOOP_START sda-p1.3-start-1159167.dat | |
sudo losetup /dev/$LOOP_BOOT sda5-1024000s-boot.dat | |
sudo losetup /dev/$LOOP_END sda-end.dat | |
# sudo mdadm --build /dev/$MD --level=linear --raid-devices=3 /dev/$LOOP /dev/sda1 /dev/sda2 | |
# /dev/sda2 - EFI System | |
# /dev/sda4 - Windows main data drive | |
sudo mdadm --build /dev/$MD --level=linear --raid-devices=4 /dev/$LOOP_START /dev/sda4 /dev/$LOOP_BOOT /dev/$LOOP_END | |
sudo chown sfink:sfink /dev/$MD | |
sleep 2 | |
sudo chmod 666 /dev/$MD | |
# MountedDevices | |
# \DosDevices\C: 2e 2e 2f 64 72 69 76 65 5f 63 00 | |
# \DosDevices\Z: 2f 00 00 00 | |
# => diskpart | |
# select disk 0 | |
# uniqueid disk id=642f2e2e | |
# exit | |
# DETAIL DISK (disk 0) | |
# Disk ID: 80cce5f3 | |
# Volume 2 (C=SYSTEM_DRV) | |
# Volume 3 (D=Windows7_OS) | |
# Loading C:\Boot\BCD | |
# - disk id is 80cce5f3 | |
# Loading D:\Boot\BCD | |
# - does not a disk id | |
# disk 0: 642f2e2e with C, D | |
# bcdedit: | |
# Windows Boot Manager | |
# device: partition=C: | |
# Windows Boot Loader | |
# device: partition=D: | |
# osdevice: partition=D: | |
# Original on /dev/sdb1 (from /Boot/BCD): 138d8c8c | |
# If I use diskpart to change to the above, the disk boots to startup repair, at least | |
# If I then do its autorepair thing, it... er... boots into a black screen | |
# ...that eventually boots for real! (Or maybe it required a ctrl-alt-delete) | |
srcprefix=/home/sfink/src | |
dstprefix=/home/sfink/Windows | |
for d in {mozilla,mozilla2,mozilla3}; do | |
[ -f $dstprefix/$d/CLOBBER ] || sudo mount $srcprefix/$d $dstprefix/$d -o bind | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment