USB Drive with Multiple Partitions Windows

  1. last year

    Hello,

    I am writing you all today in response to zenithtwc post about creating a USB drive to image directly from. There are several different ways to do this but due to the limitations of "fat" file systems, mainly file size, the process that I will be describing in this post includes setting up a small "fat 16" partition and a larger NTFS partition for saving the images on.

    The problem

    This process is very easy for linux machines has they do not have limitations on the amount of partitions on removable devices, so the creation of a USB stick in this manner is trivial and can be done with ntfsprogs and fdisk. For Windows machines, on the other hand, this is a very difficult process since Windows does currently have hard limitations on the amount of partitions on a removable disk, which is only a single partition. This proved to be a challenge in setting up the USB drives and required the use of third party tools to automate the process.

    Plan

    To overcome this problem, a tool is needed that will allow the creation of multiple partitions on a USB from the command line so that the entire set up could be scripted and thus automated. After much research, the most viable tool for completing this process was determined to be AOMEI Part Assist because it has a free version that allows for scripted partition creation and formatting of USB drives. Using this tool and windows powershell, a script was developed that would allow for the creation of the desired USB format.

    Solution

    To enact this plan, the use of powershell, powershell ISE, cmd, and AOMEI Part Assist were all implemented to create a script that will determine the disk to format, the lay out of the partitions and the creation of the file structure within the partitioned USB disk. This solution does expect that the reader has the files needed to create a simple single partition Clone Deploy USB disk ready and that AOMEI Part Assist is at least available on the readers computer. This script uses the, "get-disk", "copy-item", "clear-disk", and "start-process" cmdlets so ensure that your version of powershell has those available.

    The first thing that needs to be done it determing a valid disk for formatting, this can be done relatively trivially with the "get-disk" cmdlet. To do this, the get-disk.bustype field and a foreach loop will be used to determine the disk(s) that connected to the computer via USB.

    Ex:

    $usbdisks = 0
    
    foreach ($disk in Get-Disk) {
        if ($disk.bustype -eq 'USB'){
            $WorkingDisk = $disk.number
            $usbdisk += 1
        }
    }
    
    if (($usbdisks -gt 1) -or ($usbdisks -eq 0)){
        Write-Warning -Message "Unable to determine valid USB disk"
        get-disk
        $WorkingDisk = Read-Host -Prompt "Please enter in the disk number for the desired USB disk"
    }

    This will determine if the disk is a USB disk and then set it has the working disk or the disk that will modified. If no disks are found, the program will prompt the user to enter in a valid disk number.

    An valid unused drive letter is needed, to do this a simple line of code to used:

    Ex:

    $Wletter = (ls function:[d-z]: -n | ?{ ! (Test-Path $_) } | random)

    Next, the drive needs to be cleaned or clear of all existing data, the clear-disk cmdlet is used to accomplish this.

    Ex:

    Clear-Disk -Number $WorkingDisk -RemoveData -RemoveOEM -erroraction SilentlyContinue -Confirm:$false

    With a valid disk found, the script moves onto partitioning the disk. This is done using the AOMEI Part Assist tool, and thus will use the start-process cmdlet. The first partition will the boot partition that will contain the Clone Deploy boot environment and be bootable from bios and UEFI.

    Ex:

    $AOMEIInstall = "C:\Program Files (x86)\AOMEI"
    
    start-process -FilePath cmd -ArgumentList ("/c", "start", "/wait", "$AOMEIInstall\PartAssist.exe", "/hd:$WorkingDisk", "/cre", "/end", "/fs:fat16", "/letter:$Wletter", "/label:System", "/act", "/size:55", "/pri") -Wait -WindowStyle Hidden

    This partition must be created at the end of the disk because Windows loads what ever partition it finds first for USB's with multiple partitions, and thus if it was at the front, the NTFS partition would not be accessible.

    With the boot partition created, the Clone Deploy environment files are copied to the USB disk.

    Ex:

    $clonedeployloc = "c:\clonedeploy\deploymentfiles"
    
    Copy-Item -Path "$clonedeployloc\*" -Destination "$WLetter\" -Recurse

    Next, the syslinux boot files will be created for the targeted USB stick.

    Ex:

    Start-Process -FilePath "$WLetter\utils\win64\syslinux64.exe" -ArgumentList ("-d", "syslinux", "-mafi", "$WLetter")

    With the boot partition created, the partition can now be hidden to ensure that is not loaded by Windows, this will once again use the AOMEI Part Assist tool.

    Ex:

    Start-Process -FilePath cmd -ArgumentList ("/c", "start", "/wait", "$AOMEIInstall\PartAssist.exe", "/hide:$WLetter") -Wait -WindowStyle Hidden

    Finally, the NTFS partition will be created to the full remaining size of the disk, this will once again use the AOMEI Part Assist Tool.

    Ex:

    Start-process -FilePath cmd -ArgumentList ("/c", "start", "/wait", "$AOMEIInstall\PartAssist.exe", "/hd:$WorkingDisk", "/cre", "/fs:NTFS", "/letter:$Wletter", "/label:Images", "/size:auto", "/pri") -Wait -WindowStyle Hidden

    With is command, the disk is now set up to boot from BIOS and UEFI and be able to handle large images.

    The full script:

    $AOMEIInstall = "C:\Program Files (x86)\AOMEI"
    $clonedeployloc = "c:\clonedeploy\deploymentfiles"
    $usbdisks = 0
    
    foreach ($disk in Get-Disk) {
        if ($disk.bustype -eq 'USB'){
            $WorkingDisk = $disk.number
            $usbdisk += 1
        }
    }
    
    if (($usbdisks -gt 1) -or ($usbdisks -eq 0)){
        Write-Warning -Message "Unable to determine valid USB disk"
        get-disk
        $WorkingDisk = Read-Host -Prompt "Please enter in the disk number for the desired USB disk"
    }
    
    $Wletter = (ls function:[d-z]: -n | ?{ ! (Test-Path $_) } | random)
    
    
    Clear-Disk -Number $WorkingDisk -RemoveData -RemoveOEM -erroraction SilentlyContinue -Confirm:$false
    
    start-process -FilePath cmd -ArgumentList ("/c", "start", "/wait", "$AOMEIInstall\PartAssist.exe", "/hd:$WorkingDisk", "/cre", "/end", "/fs:fat16", "/letter:$Wletter", "/label:System", "/act", "/size:55", "/pri") -Wait -WindowStyle Hidden
    
    Copy-Item -Path "$clonedeployloc\*" -Destination "$WLetter\" -Recurse
    
    Start-Process -FilePath "$WLetter\utils\win64\syslinux64.exe" -ArgumentList ("-d", "syslinux", "-mafi", "$WLetter")
    
    Start-Process -FilePath cmd -ArgumentList ("/c", "start", "/wait", "$AOMEIInstall\PartAssist.exe", "/hide:$WLetter") -Wait -WindowStyle Hidden
    
    Start-process -FilePath cmd -ArgumentList ("/c", "start", "/wait", "$AOMEIInstall\PartAssist.exe", "/hd:$WorkingDisk", "/cre", "/fs:NTFS", "/letter:$Wletter", "/label:Images", "/size:auto", "/pri") -Wait -WindowStyle Hidden

    Conclusion

    This script will create a USB with multiple partitions but it does have its draw backs and is very limited in its application. The script doesn't check for changes to the system between steps, and if the user enters in an invalid value for the disk number from the prompt that script doesn't check that either. Otherwise this process does allow a USB to created in the way that zenithtwc asked for.

  2. clonedeploy

    7 Apr 2017 Administrator

    This is exciting news. Thank you for your effort and research into this matter. It's definitely something I want to try and get into the 1.3.0 release, but it's also getting more complicated now that there are 3 imaging environments. Hopefully I can at least get it into the linux environment. Once this is all worked out and put into a release I'll be sure to add your script to the documentation. Thanks again.

 

or Sign Up to reply!