Problem creating GPT VHD automatically

Page 1 of 4 123 ... LastLast

  1. Posts : 13,561
    Windows10
       #1

    Problem creating GPT VHD automatically


    I have a script which produces a diskpart script to create a virtual hard drive.

    Code:
    create vdisk file="F:\hyper-V\virtual hard disks\2004dn.vhdx" maximum=60000 type=expandable 
    select vdisk file="F:\hyper-V\virtual hard disks\2004dn.vhdx" 
    attach vdisk
    create partition primary 
    select partition 1 
    assign letter=T 
    format fs=ntfs quick label="Windows" 
    


    This works perfectly but when I try and produce a script for a GPT VHD (I want more than 4 primary partitions, I alsi have t select a volume.

    Code:
    create vdisk file="F:\hyper-V\virtual hard disks\2004dn.vhdx" maximum=60000 type=expandable 
    select vdisk file="F:\hyper-V\virtual hard disks\2004dn.vhdx" 
    attach vdisk
    convert gpt 
    create partition primary 
    select partition 1 
    select volume #
    assign letter=T 
    format fs=ntfs quick label="Windows" 
    


    Problem is a GPT drive also requires the volume number. I have to manually enter the Volume number # into my script that creates diskpart.

    Only way I can think of doing it is to run diskpart list volume command outputting list of existing drive volumes to a text file, parse text file to get last volume number, then add 1 (or 2 maybe), then running diskpart script in a separate batch file, passing volume number as an argument.

    Can anybody think of a simpler way?

      My Computer


  2. Posts : 2,768
    Windows 11 Pro, 21H2
       #2

    It's possible that if you are using a script, it may work differently and you have to select the vdisk after you create it, and again, you may need to select the volume as well. I simply dispense with the use of a script altogether and pipe the commands to diskpart at which time it operates just like it would if I were manually issuing the commands one by one.

    See the summary below for details...

    I wrote a little program for myself that includes almost exactly what you are doing, but I'm finding that I am not selecting the volume number. I also just ran though this manually and did not have to select a volume number. In fact, you can also omit selection of the vdisk since it will already be selected after creation.

    After you create the partition, if you issue a "list vol" you will find that the unformatted volume is already selected by default (shown with an asterisk), so there is no need to select it again.

    As noted above, the same is true after creating the vdisk. It will already be selected so you can omit the line where you select it after creation.

    I simply skip the script file altogether as in the batch file example below:

    Code:
    @echo off
    (create vdisk file="F:\hyper-V\virtual hard disks\2004dn.vhdx" maximum=60000 type=expandable 
    REM Note that I have left off the vdisk selection since it is already selected
    attach vdisk
    convert gpt 
    create partition primary 
    select partition 1 
    REM Note that I am not selecting a volume since it is already selected
    assign letter=T 
    format fs=ntfs quick label="Windows"
    exit
    ) | diskpart
    Everything between the parens will get piped to diskpart as if you were typing the commands manually. If you want totally silent operation, change the last line to ") | diskpart > NUL".

    I hope that helps!
      My Computers


  3. Posts : 2,768
    Windows 11 Pro, 21H2
       #3

    Just a follow up, here's how I setup a VHDX for Windows deployment:

    Code:
    @echo off
    (create vdisk file="D:\WinDeploy.VHDX" maximum=60000 type=expandable
    attach vdisk
    convert gpt
    create partition efi size=260
    format quick fs=fat32 label="System"
    create partition msr size=128
    create partition primary
    shrink minimum=500
    format quick fs=ntfs label="Windows"
    assign letter=T
    create partition primary
    format quick fs=ntfs label="WinRE"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    exit
    ) | diskpart > NUL
      My Computers


  4. Posts : 17,246
    Windows 11 Pro
       #4

    I've never had to select the volume to assign a drive letter after formatting a partition in diskpart. It's always been:
    Code:
    cre par pri
    for fs=ntfs quick
    ass letter=t
      My Computer


  5. Posts : 17,636
    Windows 10 Pro
       #5

    The simplest method is of course to create a new Generation 2 (UEFI / GPT) VM in Hyper-V, then add the VHD of that VM to Windows boot menu as a native boot VHD.

    I usually create native boot VHDs with a batch file and a DISKPART script. The only change I need to do is to add CONVERT GPT as third line in the DISKPART script in following video:

    Kari said:
    New video: Dual Boot - The Easy Way



    The easiest possible method to dual boot. No partitioning, no virtualization required. Deploy Windows 10 on a virtual hard disk VHD or VHDX file, add it to boot menu. When done, when you no longer want to dual boot, just delete the VHD file.
    Note   Note
    When creating VHD file to be used in native boot, always use MBR partitioning! To upgrade Windows on native boot VHD, it must be temporarily attached to a virtual machine. An MBR partitioned VHD is easy to attach to VM, it only needs Windows partition to be marked active, whereas a GPT partitioned VHD with only a single partition for Windows requires manually creating system partitions before it can be used on VM.

    If you for any reason want to use a GPT partitioned VHD in native boot (can't think any valid reason!), it is better and recommended that you first create a Generation 2 VM in Hyper-V, installing Windows 10 on it. This takes care of the partitioning, doing it correctly. The VHD can then be used as native boot VHD, or on a VM.

    See this tutorial for more information: Native boot Virtual Hard Disk - How to upgrade Windows

    The DISKPART script and deployment batch from video:

    DISKPART scripts are normal text files with extension .txt. To run a DISKPART script, enter following command:

    diskpart /s DRIVE:\FOLDER\SCRIPT.TXT

    Here's the script used in video:
    Code:
    create vdisk file=F:\W10PRO.vhdx maximum=51200 type=expandable
    attach vdisk
    create part primary
    format quick label="Windows"
    assign letter=W
    exit
    Save script as VHDConfig.txt

    Above script creates a dynamically expanding MBR partitioned 50 GB (51,200 MB) VHDX file (you can use .vhd or .vhdx) named W10PRO.vhdx in root of drive F:, labels it Windows and assigns it a temporary drive letter W:. Edit script to meet your needs, be sure the drive you create VHD has double the free storage than the size of VHD; if you will create a 100 GB VHD file to be used in dual boot, the drive where it will be created needs 200 GB free.

    Also, be sure to assign an unused drive letter! I always use W: which I have reserved for this purpose, never assigning it to anything else. In any case the drive letter will only be needed for deployment, it will be freed when VHD will be unmounted.

    The deployment batch is really simple, too. It just runs DISKPART script to set up VHD, deploys Windows on it with DISM, adds it to host boot menu and finally changes the boot menu entry to something more descriptive:
    Code:
    start /wait diskpart /s E:\Users\Kari\Scripts\VHDConfig.txt
    start /wait dism /apply-image /imagefile:I:\sources\install.wim /index:8 /applydir:W:\
    start /wait bcdboot W:\Windows
    start /wait bcdedit /set {default} description "W10 PRO EN-GB (VHD)"
    cls
    @echo off
    echo. 
    echo Windows deployed to VHD file
    echo and added to host boot menu.
    echo.
    pause
    exit
    Save batch file as VHDBoot.bat

    Again, edit the batch to meet your needs. Be sure path to DISKPART script is correct, and that path to install.wim (or in case of MCT ISO install.esd) is correct in DISM command. Select correct index value. Check that BCDBOOT will add Windows to boot menu from correct disk, the drive letter here must be the same DISKPART script assigned to VHD.

     Windows 10 ISO edition index values


    Table 1: Official Windows 10 ISO images

    Edition index values for ISO images downloaded from Download Windows 10 Disc Image (ISO File)

    • ESD = ISO image created with Windows Media Creation Tool (install.esd file instead of install.wim)
    • WIM = WIM based ISO image (requires browser user agent change)


    Notice that values in WIM column also apply for official Insider ISO images downloaded from Download Windows 10 Insider Preview Advanced

    Edition ESD WIM
    Home 1 1
    Home N 2 2
    Home SL 3 3
    Pro 6 6
    Pro N 7 7
    Pro Education - 8
    Pro Education N - 9
    Pro for Workstation - 10
    Pro for Workstation N - 11
    Education 4 4
    Education N 5 5


    Table 2: Visual Studio (former MSDN) subscriber ISO images

    Edition index values for ISO images downloaded from Sign in to your account
    • VSB= Business editions ISO image
    • VSC= Consumer editions ISO image


    Edition VSB VSC
    Home - 1
    Home N - 2
    Home SL - 3
    Pro 5 6
    Pro N 6 7
    Pro Education 7 8
    Pro Education N 8 9
    Pro for Workstation 9 10
    Pro for Workstation N 10 11
    Education 1 4
    Education N 2 5
    Enterprise 3 -
    Enterprise N 4 -
    Enterprise for Virtual Desktops 11 -


    All single edition ISO images have index value 1 for edition in question regardless of edition, for instance whatever edition you are running, its index = 1 if you made the ISO by yourself with UUPtoISO.
      My Computer


  6. Posts : 13,561
    Windows10
    Thread Starter
       #6

    hsehestedt said:
    Just a follow up, here's how I setup a VHDX for Windows deployment:

    Code:
    @echo off
    (create vdisk file="D:\WinDeploy.VHDX" maximum=60000 type=expandable
    attach vdisk
    convert gpt
    create partition efi size=260
    format quick fs=fat32 label="System"
    create partition msr size=128
    create partition primary
    shrink minimum=500
    format quick fs=ntfs label="Windows"
    assign letter=T
    create partition primary
    format quick fs=ntfs label="WinRE"
    set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac"
    exit
    ) | diskpart > NUL
    DO I have to set a specific disk id or will your number just work?
      My Computer


  7. Posts : 13,561
    Windows10
    Thread Starter
       #7

    Kari said:
    The simplest method is of course to create a new Generation 2 (UEFI / GPT) VM in Hyper-V, then add the VHD of that VM to Windows boot menu as a native boot VHD.

    I usually create native boot VHDs with a batch file and a DISKPART script. The only change I need to do is to add CONVERT GPT as third line in the DISKPART script in following video:
    Thanks @Kari - sure I can do this but my reasons are for a script I have that mounts iso, injects drivers, creates iso with injected drivers, and also optionally creates a dual boot vhd all in one go. The script is setup in a general way and can be transferred to different pcs.

    The only bit I could not generalise was as in my original post. Not a big deal as I can simply enter volume number manually.

    However, I may have a solution from @hsehestedt.
      My Computer


  8. Posts : 13,561
    Windows10
    Thread Starter
       #8

    cereberus said:
    Thanks @Kari - sure I can do this but my reasons are for a script I have that mounts iso, injects drivers, creates iso with injected drivers, and also optionally creates a dual boot vhd all in one go. The script is setup in a general way and can be transferred to different pcs.

    The only bit I could not generalise was as in my original post. Not a big deal as I can simply enter volume number manually.

    However, I may have a solution from @hsehestedt.
    Hi @hsehestedt, @Kari
    I tried the above and it did not work. I even manually just entered the commands in an admin prompt. I could not format the file WITHOUT selecting volume. Im MBR, the volume seems to be selected automatically, and in UEFI it is not.
    However, the solution was so simple - I just had an idea and just entered "SELECT VOL" with no volume number and it selected the volume.


    Edit - I have done a bit more testing. If I do not enter "SELECT PARTITION 1" after creating the primary partition, I can format with out needing to even use "SELECT VOL" as above.


    So following does not work as I need to select volume

    Code:
    create partition primary
    select partition 1
    format fs=ntfs quick


    The two following do work.

    Code:
    create partition primary
    select partition 1
    select volume
    format fs=ntfs quick

    or, simply
    Code:
    create partition primary
    format fs=ntfs quick

    This is really odd but hey - I now have a solution.
    So many thanks for your input. It led me in the right direction.

    Edit - just deleted "select partition 1" line from my script and now it works fine.
    Last edited by cereberus; 18 Jun 2020 at 11:19.
      My Computer


  9. Posts : 2,768
    Windows 11 Pro, 21H2
       #9

    Glad to hear you got it working. Also, my apologies. I assumed GPT, I should have also provided my code for MBR.
      My Computers


  10. Posts : 13,561
    Windows10
    Thread Starter
       #10

    hsehestedt said:
    Glad to hear you got it working. Also, my apologies. I assumed GPT, I should have also provided my code for MBR.
    I had no issue with mbr.

    Thanks for help.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 08:20.
Find Us




Windows 10 Forums