winiso-valhalla/deploy.sh

127 lines
3.3 KiB
Bash
Executable file

#!/bin/bash
WIN_DISK="${WIN_DISK}";
workingDir="$(pwd)";
overlayDir="$(mktemp -d)";
pushd "${BASH_SOURCE%/*}" > /dev/null;
. "./.env" > /dev/null 2>&1;
. "./lib/choose-disk.sh";
if [ ! -z "$WIN11_IMAGE_PATH" ]
then
WIN11_IMAGE_PATH="$(bash -c "realpath $WIN11_IMAGE_PATH")";
fi;
if [ ! -f "$WIN11_IMAGE_PATH" ]
then
if [ ! -f "./.env" ]
then
cp .env.template .env;
fi;
echo "Please specify the path to the Windows 11 ISO image in your .env file located at:";
realpath --relative-to "$workingDir" "$(realpath .env)";
else
buildDir="build";
mountPath="/media/wininstall";
bootPath="/media/boot";
dataPath="/media/data";
setupLabel="winiso";
gitArchive="$(realpath "./$buildDir/git.msi")";
pwshArchive="./$buildDir/pwsh.zip";
editionField="Edition ID";
mkdir -p "$buildDir";
cp -r winfs/* "$overlayDir";
if [ ! -f "$gitArchive" ]
then
curl -L "https://github.com/git-for-windows/git/releases/download/v2.41.0.windows.1/PortableGit-2.41.0-64-bit.7z.exe" -o "$gitArchive";
fi;
if [ ! -f "$pwshArchive" ]
then
curl -L "https://github.com/PowerShell/PowerShell/releases/download/v7.3.4/PowerShell-7.3.4-win-x64.zip" -o "$pwshArchive";
fi;
mkdir -p "$overlayDir/git";
pushd "$overlayDir/git" > /dev/null;
7z x "$gitArchive";
popd > /dev/null;
unzip "$pwshArchive" -d "$overlayDir/PowerShell";
sudo mount --mkdir "$WIN11_IMAGE_PATH" "$mountPath";
isoFile="./$buildDir/win.iso";
mkdir -p "$(dirname "$isoFile")";
mkwinpeimg --iso --arch amd64 --overlay "$overlayDir" --windows-dir "$mountPath" "$isoFile";
sudo umount "$mountPath";
if [ ! -b "$WIN_DISK" ]
then
chooseDisk WIN_DISK;
fi;
{
echo "o";
echo "n";
echo "";
echo "";
echo "";
echo "+2G";
echo "y";
echo "t";
echo "c";
echo "a";
echo "";
echo "n";
echo "";
echo "";
echo "";
echo "";
echo "y";
echo "t";
echo "";
echo "7";
echo "w";
} | sudo fdisk "$WIN_DISK";
while true
do
disks=($(bash -c "echo $WIN_DISK*"));
[ "${#disks[@]}" -ge 3 ] && break;
done;
bootDisk="${disks[1]}";
dataDisk="${disks[2]}";
sudo mkfs.fat -F 32 -n "BOOT" "$bootDisk";
sudo mkfs.ntfs -fFL "$setupLabel" "$dataDisk";
sudo mount --mkdir "$bootDisk" "$bootPath";
sudo mount --mkdir "$dataDisk" "$dataPath";
sudo mount --mkdir "$isoFile" "$mountPath";
sudo cp -r "$mountPath"/* "$bootPath";
sudo umount "$mountPath";
sudo mount "$WIN11_IMAGE_PATH" "$mountPath";
sudo cp -r "$mountPath"/* "$dataPath";
sudo cp Autounattend.template.xml "$dataPath";
sudo cp -r "$mountPath/efi" "$bootPath";
wimFile="$dataPath/sources/install.wim";
while [ ! "$(echo $(wiminfo "$wimFile" 1 | grep "^$editionField" | cut -d ":" -f2))" == "Professional" ]
do
sudo wimdelete --soft "$wimFile" 1;
done;
while wiminfo "$wimFile" 2 2>&1 > /dev/null
do
sudo wimdelete --soft "$wimFile" 2;
done;
sudo bash -c "umount -vf '$mountPath'; umount -vf '$bootPath'; umount -vf '$dataPath'; rm -rf '$mountPath' '$bootPath' '$dataPath';";
fi;
popd > /dev/null;