Skip to content

Instantly share code, notes, and snippets.

@xzn
Created March 14, 2020 13:32
Show Gist options
  • Save xzn/06760e0e7df6ca325d0f05979aeff3bd to your computer and use it in GitHub Desktop.
Save xzn/06760e0e7df6ca325d0f05979aeff3bd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")"
MEM=Windows_mem
IMG=Windows_img
IMG_O=Windows_img_o
IMG_P=Windows_img_p
MBR=Windows_mbr
SIZE_MBR=2048
# dd if=/dev/zero of=$MBR count=$SIZE_MBR
# LOOP=$(losetup --show -f $MBR)
# echo $LOOP
PART=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155
PART_A=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155-part1
PART_B=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155-part2
PART_C=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155-part4
PART_D=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155-part5
PART_E=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155-part6
PART_O=/dev/disk/by-id/ata-ADATA_SU800_2J3420115033
PART_P=/dev/disk/by-id/ata-ST8000DM004-2CX188_ZCT15FTG
DM=Windows_dm
# echo "0 $SIZE_MBR linear $LOOP 0
# $SIZE_MBR $(blockdev --getsz $PART_C) linear $PART_C 0" | dmsetup create $DM
#
# dmsetup remove $DM
# losetup -d $LOOP
MBR_B=Windows_mbr_b
START_A=$(blockdev --report $PART_A | tail -1 | awk '{print $5}')
SIZE_A=$(blockdev --getsz $PART_A)
START_B=$(blockdev --report $PART_B | tail -1 | awk '{print $5}')
SIZE_MBR=$START_A
SIZE_MBR_B=40
START_C=$(blockdev --report $PART_C | tail -1 | awk '{print $5}')
SIZE_C=$(blockdev --getsz $PART_C)
START_D=$(blockdev --report $PART_D | tail -1 | awk '{print $5}')
SIZE_D=$(blockdev --getsz $PART_D)
START_E=$(blockdev --report $PART_E | tail -1 | awk '{print $5}')
SIZE=$(blockdev --getsz $PART)
umount $PART_A
umount $PART_C
umount $PART_D
umount $PART_O
umount $PART_P
PATH_A=/boot
PATH_C=/run/media/same/BOOTCAMP
PATH_D=/run/media/same/C2BE4A02BE49F009
PATH_O=/run/media/same/D858788E58786CDE
PATH_P=/run/media/same/D46C89B96C899740
if mount | grep -w $MEM; then umount $MEM; fi
if mount | grep -w $PATH_A; then umount $PATH_A; fi
if mount | grep -w $PATH_C; then umount $PATH_C; fi
if mount | grep -w $PATH_D; then umount $PATH_D; fi
if mount | grep -w $PATH_O; then umount $PATH_O; fi
if mount | grep -w $PATH_P; then umount $PATH_P; fi
if dmsetup ls | grep -w $DM; then dmsetup remove $DM; fi
LOOP_B=$(losetup -l | grep -w $MBR_B | awk '{print $1}')
if [ "$LOOP_B" ]; then losetup -d $LOOP_B; fi
LOOP=$(losetup -l | grep -w $MBR | awk '{print $1}')
if [ "$LOOP" ]; then losetup -d $LOOP; fi
if [ ! -f $MBR ]; then dd if=$PART count=$SIZE_MBR of=$MBR; fi
LOOP=$(losetup --show -f $MBR)
if [ ! -f $MBR_B ]; then dd if=$PART skip=$((SIZE-SIZE_MBR_B)) count=$SIZE_MBR_B of=$MBR_B; fi
LOOP_B=$(losetup --show -f $MBR_B)
echo "0 $SIZE_MBR linear $LOOP 0
$START_A $SIZE_A linear $PART_A 0
$START_B $((START_C-START_B)) zero
$START_C $SIZE_C linear $PART_C 0
$START_D $SIZE_D linear $PART_D 0
$START_E $((SIZE-START_E-SIZE_MBR_B)) zero
$((SIZE-SIZE_MBR_B)) $SIZE_MBR_B linear $LOOP_B 0" | dmsetup create $DM
EFI=Windows_efi
PID=Windows_pid
IMG_B=Windows_imgb
# if [ ! -f $IMG_B ]; then qemu-img create -f qcow2 $IMG_B 256M; fi
# if [ ! -f $IMG ]; then qemu-img create -f qcow2 -b /dev/mapper/$DM $IMG; fi
# if [ ! -f $IMG_O ]; then qemu-img create -f qcow2 -b $PART_O $IMG_O; fi
# if [ ! -f $IMG_P ]; then qemu-img create -f qcow2 -b $PART_P $IMG_P; fi
IMG=/dev/mapper/$DM,format=raw
# IMG_O=$PART_O,format=raw
# IMG_P=$PART_P,format=raw
if [ ! -f $EFI ]; then cp /usr/share/ovmf/x64/OVMF_VARS.fd $EFI; fi
RAM=12
mkdir -p $MEM
mount -t hugetlbfs hugetlbfs $MEM
echo $RAM > /proc/sys/vm/nr_hugepages
# chrt -f 1 \
qemu-system-x86_64 -vga none -name Windows,debug-threads=on -boot menu=on \
-pidfile $PID -daemonize -m ${RAM}G -mem-path $MEM \
-enable-kvm -machine q35,kernel_irqchip=on -parallel none -serial none \
-cpu host,kvm=off,hv_relaxed,hv_spinlocks=0xffffffff,hv_vapic,hv_time,hv_vendor_id=0xDEADBEEFFF,hv-runtime,hv-vpindex,hv-synic,hv-stimer,hv-tlbflush,hv-ipi,hv-reset,hv-frequencies,hv-reenlightenment,hv-stimer-direct,migratable=no,+invtsc \
-smp 6,cores=3,threads=2 \
-rtc clock=host,base=utc,driftfix=slew \
-audiodev id=pa,driver=pa,in.buffer-length=50000,out.buffer-length=50000,in.latency=20000,out.latency=20000 \
-device ich9-intel-hda,id=snd,msi=on -device hda-micro,id=mic,bus=snd.0,cad=0 \
-netdev user,id=nic -device virtio-net-pci,netdev=nic \
-drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd \
-drive if=pflash,format=raw,file=$EFI \
-device vfio-pci,host=0a:00.0,multifunction=on,romfile=rom-parser/image.rom \
-device vfio-pci,host=0a:00.1 \
-device vfio-pci,host=0a:00.2 \
-device vfio-pci,host=0a:00.3 \
-device qemu-xhci,id=usbds -device usb-host,bus=usbds.0,vendorid=0x054c,productid=0x05c4 \
-device qemu-xhci,id=usbkb -device usb-host,bus=usbkb.0,vendorid=0x2516,productid=0x001a \
-device qemu-xhci,id=usbbt -device usb-host,bus=usbbt.0,vendorid=0x05ac,productid=0x8289 \
-object iothread,id=ioto \
-device virtio-scsi-pci,id=scsio,iothread=ioto,num_queues=6 \
-drive id=drvo,file=/dev/disk/by-id/ata-ADATA_SU800_2J3420115033,format=raw,if=none,cache=unsafe,discard=unmap,detect-zeroes=unmap,copy-on-read=on \
-device scsi-hd,drive=drvo,bus=scsio.0,rotation_rate=1 \
-object iothread,id=iotp \
-device virtio-scsi-pci,id=scsip,iothread=iotp,num_queues=6 \
-drive id=drvp,file=/dev/disk/by-id/ata-ST8000DM004-2CX188_ZCT15FTG,format=raw,if=none,cache=unsafe,discard=unmap,detect-zeroes=unmap,copy-on-read=on \
-device scsi-hd,drive=drvp,bus=scsip.0,rotation_rate=1 \
-drive file=$IMG,if=none,id=drvhd -device ich9-ahci,id=ichhd -device ide-hd,drive=drvhd,bus=ichhd.0,bootindex=1 \
# -object iothread,id=iotc \
# -device virtio-scsi-pci,id=scsic,iothread=iotc,num_queues=6 \
# -drive id=drvc,file=/dev/disk/by-id/ata-SAMSUNG_MZHPV256HDGL-000L1_S1WTNYAG701155,format=raw,if=none,cache=unsafe,discard=unmap,detect-zeroes=unmap,copy-on-read=on \
# -device scsi-hd,drive=drvc,bus=scsic.0,rotation_rate=1,bootindex=1 \
# -device vfio-pci,host=07:00.0 \
# -drive file=$IMG_O,if=none,id=drvo -device ich9-ahci,id=icho -device ide-hd,drive=drvo,bus=icho.0 \
# -drive file=$IMG_P,if=none,id=drvp -device ich9-ahci,id=ichp -device ide-hd,drive=drvp,bus=ichp.0 \
# -cdrom "/run/media/same/8A2CF4F62CF4DE5F/Downloads/Software/Windows 10 1909.iso" -boot d \
# -device ioh3420,id=rootn,chassis=0,slot=0,bus=pcie.0 \
# -device vfio-pci,host=0a:00.0,bus=rootn,addr=0x00,multifunction=on,romfile=rom-parser/image.rom \
# -device vfio-pci,host=0a:00.1,bus=rootn,addr=0x00.1 \
# -device vfio-pci,host=0a:00.2,bus=rootn,addr=0x00.2 \
# -device vfio-pci,host=0a:00.3,bus=rootn,addr=0x00.3 \
PID=$(cat $PID)
# chrt -a -f -p 99 $(pidof qemu-system-x86_64)
# chrt -a -f -p 99 $PID
qemu-affinity $PID -p 6-7 -i *:6-7 -q 6-7 -w *:6-7 -k 0 2 4 1 3 5
# qemu-affinity $PID -p 0-7 -i *:0-7 -q 0-7 -w *:0-7 -k 0 2 4 1 3 5
for i in $(sed -n -e 's/ \([0-9]\+\):.*/\1/p' /proc/interrupts); do echo '6,7' > /proc/irq/$i/smp_affinity_list; done;
# dmsetup remove $DM
# losetup -d $LOOP_B
# losetup -d $LOOP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment