Install Windows 7 From Within Debian Sid

Did you know that we can install Windows 7 (or 8, or 10) from within Linux?
Nope, not by install it on virtual machines or use virtual machine to write Windows installer into raw disk, but by using a Linux tool dubbed wimlib to directly install the Windows installer into a dedicated Windows partition.

So, here's a simple and short "how to" to install Windows 7 (or 8, or 10) directly from within Linux machine. No need a dedicated Linux installation, a live environment will do.

These steps below tested on ThinkPad T520 with BIOS and Debian Sid installed.

  • Get a Windows 7 image (ISO) file. Theoritically, Windows 7, 8, and 10 could uses this install method as they have the same WIM format and structure.
  • Spare a partition on your disk (e.g /dev/sda2) and format it to NTFS. Don't forget to set its boot flag.
  • Install wimtools.
    sudo aptitude install wimtools
    
  • Create temporary folders to mount the Windows image and the NTFS partition.
    sudo mkdir /tmp/{iso,ntfs}
    
  • Mount the Windows 7 image.
    sudo mount -o loop PATH_TO_WINDOWS_ISO /tmp/iso
    
  • Generally, the Windows image may contains alots of Windows edition. List the index using following command:
    wimlib-imagex info /tmp/iso/sources/install.wim
    
  • For example, we will be installing Windows 7 Ultimate which located on 4th index.
    sudo wimlib-imagex apply /tmp/iso/sources/install.wim 4 /dev/sda2
    
  • Mount /dev/sda2 and then copy the boot files.
    mount /dev/sda2 /tmp/ntfs
    mkdir /tmp/ntfs/sources
    cp -r /tmp/ntfs/Windows/Boot/PCAT /tmp/ntfs/Boot
    cp /tmp/ntfs/Boot/bootmgr /ntfs/hdd
    
  • Unfortunately, I've not yet found Linux tools to manipulate Windows bootloader (BCD). So, we'll do a "hack"; mount the Windows partition and copy Windows image bootmanager to this partition.
    cp /tmp/ntfs/Windows/Boot/DVD/PCAT/{boot.sdi,BCD} /tmp/ntfs/Boot
    cp /tmp/ntfs/Windows/System32/Recovery/winRE.wim /tmp/ntfs/sources/boot.wim
  • Extra. You can skip this unattended install step.
    We can automate the Windows installation by create an unttended install file. See technet.microsoft.com or windowsafg.no-ip.org.
    Copy the unattended answer file (e.g Unattend.xml) to Windows installation.
    sudo cp Unattend.xml /tmp/ntfs/Windows/System32/sysprep
    

    Unattend.xml possibly contains some sensitive data, so we better remove it after installation is finished.
    It’s easier if we just create a script (e.g SetupComplete.cmd) to automate those steps. The script could also be expanded to enable some registry tweaks, and more.

    mkdir /tmp/ntfs/Windows/Setup/Scripts
    cp SetupComplete.cmd /tmp/ntfs/Windows/Setup/Scripts
    
  • Update GRUB (or any bootloader) so it can recognize the new installation.
    sudo update-grub
    

That's it!
Because of the bootloader is ISO/image bootloader, it has no information about partition on disk so upon the first boot we have to enter Windows Recovery Environment and do BCD repair.
First stage installation will start on second boot.
Second (latest) stage installation will start on third boot.

Note
Linux do not write to disk in contiguous. It's better to defrag this new Windows installation if it's installed on hard disk drive.

Attachment