Home > General, Operating System > Give Windows a Swap partition with no drive letter

Give Windows a Swap partition with no drive letter

March 31st, 2010 Leave a comment Go to comments

Are you a fan of how Linux (Unix) allows you to designate an actual hard drive partition to swap space? Ever wished that Windows would let you do something similar, instead of having to either store it on the same partition as Windows, or have to designate an actual drive letter to another partition just for swap?

For the longest time ever, I always wished that you could do more than just move your Windows swap file (pagefile.sys) to another drive letter. First of all, having to assign a drive letter to the partition you want purely for storing the Windows page file really sucks because then that means the drive letter shows up in explorer (sure, you can hide drive letters, but you’ve still wasted a drive letter on the partition). Second of all, if you think you’re being smart by creating a partition at the root of your drive during Windows installation, chances are you kicked yourself after going through the install only to realize that it assigned that partition the letter “C:” and now Windows is installed on “D:” (good luck getting out of that scenario – I’d recommend just redoing the install entirely; trust me it saves you a ton of headaches).

Well, since I just got my fancy copy of Windows 7 Ultimate, I figured I would see what I could do finally about this situation. Upon booting up the Windows 7 DVD, I went through the installer up to picking the partition I wanted to install Windows 7 on to. I clicked the Advanced button so I could delete all my existing partitions and set the drive up the way I wanted. With an entirely blank drive, I began by creating a single partition on the drive (entire drive). This let the installer create the special “System Reserved” partition that it wanted. I then selected the other partition it made and deleted that and then created a new partition the size I wanted for my swap. I have 4GB of ram on this system so I want at least double that, plus I may want extra space for something else eventually so I went for a 16GB partition. Next, I created the last partition with the rest of the drive space; this is my Windows partition (C: drive). I selected the Windows partition and clicked Format (do *not* do this for the Swap partition yet!).

The next step was where some of the nerd-magic came in. I backed out of the installer at this point and got back to the main window and clicked on the Recovery link near the bottom. From there I selected the top radio button and then on the next screen I selected the Recovery Console. Once in the console I ran “diskpart” to let me tinker with the partitions I had just created. Using diskpart isn’t too complicated. You can type in “help” to get a list of the commands you can use; it’s not overly complex if you come from a Linux background and you’re used to using fdisk. I selected the Swap partition (volume) I had made earlier and ran “ATTRIBUTES VOLUME SET NODEFAULTDRIVELETTER”. This prevents the system from automatically assigning drive letters to any partitions on this drive – it will still assign “C:”, however the Swap partition will not get a letter. Once that was done, I made sure to list out the volumes just to be sure it didn’t already have a drive letter (it shouldn’t since we didn’t format it yet). I then closed the console, and closed the window with the recovery choices (this brings you back to the start – you don’t have to click Shutdown or Restart!). I then went back through the installation process, selected my Windows partition I had created and let it install Windows 7 to the drive.

After finishing the Windows install and logging in, I thought about how I would actually go about using that Swap partition for the page file without giving it a drive letter. I decided to search the registry for “pagefile.sys” to see what I could come up with and I found that the only real occurrence of it is in “HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management\”. There are two keys in there, PagingFiles and ExistingPageFiles, which reference pagefile.sys and that’s about it. Obviously ExistingPageFiles is not a key we want to modify, however PagingFiles is.

So here’s where the real brains came in to help… I went in to Disk Management and selected the unformatted Swap partition I had created and formatted it as NTFS (with 32k blocks – we don’t need small blocks here… the bigger the better for performance). Then, I opened up Explorer and decided I would have this partition mounted to “C:\Windows\Pagefile” – yup, I’m mounting it to a path, not a drive letter! So, I created the directory and then went in to it’s security permissions. I removed all the inherited permissions and set the following permissions on it: TrustedInstaller (“NT Service\TrustedInstaller” is the account name you must type in to make that work) got full access, SYSTEM got full access and Administrators got Read & Execute access. I then went and changed the Owner to TrustedInstaller as well. Once you do this, you won’t be able to access the folder even as an Administrator – but that’s what we want since we don’t want anything having any access to pagefile.sys except the system itself.

I then went back in to Disk Management and again selected the Swap partition and went in to “Change Drive Letter and Paths…”, clicked Add… and set it to mount to the “C:\Windows\Pagefile” folder I had created. I made sure to confirm that the process had worked successfully (basically just went back in to make sure it was listed in the “Change Drive Letter and Paths…” dialog box). Now that I had my Swap partition mounted to the directory, I just had to change where pagefile.sys would be written (and erase the old pagefile.sys file). I opened up regedit, went to “HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management\” and double clicked the PagingFiles key to modify it. This registry entry allows for multiple pagefiles to be specified, one per line. The 3 different formats it can have are rather simple to understand after having examined it in different configurations.

  • The first mode is having Windows manage pagefile.sys for you; the drive letter is specified with “?” to denote that it is whatever drive letter Windows is installed on and then the full path to pagefile.sys is specified (eg “?:\Windows\Pagefile\pagefile.sys”).
  • The second mode is managing the location of pagefile.sys yourself, but having Windows manage it’s size; the full path to pagefile.sys is now specified including the drive letter, and then we add two zeros separated by spaces to denote a dynamic minimum and maximum size for pagefile.sys growth (eg “C:\Windows\Pagefile\pagefile.sys 0 0″).
  • The third mode is managing pagefile.sys entirely on our own; The full path to pagefile.sys is specified, and then we specify an absolute minimum and maximum size for the file in MB (eg “C:\Windows\Pagefile\pagefile.sys 6144 10240″).

I chose the third method since after some experimenting it appears as though this is the most compatible with the Virtual Memory settings in Windows. Using the first method, Windows doesn’t appear to know where pagefile.sys is or that it is supposed to be managing it for us. Using the third method, it knows that pagefile.sys is indeed located on our “C:” drive and that there are explicit minimum and maximum growth limits specified. It may be only visual, but I didn’t want to chance it with my virtual memory (since running in to the problem when I would need my virtual memory would be a very bad thing).

Finally, to make sure the existing pagefile.sys was deleted (properly), I opened up “Local Security Policy” from the “Administrative Tools” control panel. I navigated to “Local Policies\Security Options” and scrolled down near the bottom of the list to find “Shutdown: Clear virtual memory pagefile” and simply enabled the option. This would take care of deleting the page file at shutdown for me.

That was about it – next was taking a leap of faith and hoping that when I rebooted, everything would still work! So, I crossed my fingers and hit restart and watched as my hard drive light went nuts as it deleted the current pagefile.sys before rebooting. Once my system came back up I went in to Disk Management, right clicked the Swap partition and clicked Properties. I checked to make sure there was some space being used up just to confirm that pagefile.sys was now being stored there instead of my Windows partition – and it was! All I had left to do was go back in to “Local Security Policy” and disable the “Shutdown: Clear virtual memory pagefile” setting.

I had finally done it – I had created a separate partition for Windows to store its page file without having to resort to assigning a drive letter. So far, the only drawback seems to be that if you go in to your system settings to manage your Virtual Memory, it gets a bit confused and indicates that there is no paging file set on your “C:” drive – which I suppose is now technically correct… but I wonder what might happen if Windows tries to allocate more space than is available in the Swap partition for Virtual Memory… I think I may have to peek at some more registry entries to see if I can set the min/max values for Virtual Memory so that can’t happen.

If you’re also wondering about how to deal with the hiberfil.sys file in the root of your Windows drive, you can read up on that in my other post “Delete hiberfil.sys from the root of your Windows drive“.

  1. No comments yet.
  1. No trackbacks yet.