How to increase the capacity of the root file system used in ASCII.jp WSL2?
WSL2 (Windows Subsystem for Linux 2) realizes a Linux environment using a virtual machine.The native file system is on a virtual hard disk file."EXT4.This file called "VHDX" is created in a variable capacity format, and the maximum capacity is specified to 256GB.Since this value is a fixed value, it may be insufficient in some cases.This time, let's explain how to increase this capacity
WSL2 creates an EXT4 file system in a virtual hard disk file with a variable capacity when installing the distribution.The capacity at this time is 256GB fixed
Expand the maximum capacity of the virtual hard disk file
In general, there are two types of virtual hard disk files, "variable capacity" and "fixing capacity", but in many cases, the capacity variable is used to consume storage capacity as much as used.The maximum capacity is specified in the virtual hard disk file with a variable capacity, and the software side seems to be a drive with its capacity.
A variable virtual hard disk file can increase the maximum capacity by using the DiskPart command.However, if the target is EXT4 format, the function of the DiskPart command is limited.
For example, reducing the maximum capacity is only possible in NTFS format, and does not support virtual hard disk files formatted in EXT4 format.However, the maximum capacity is a nominal capacity, which does not mean that the size of this is large does not immediately increase the file size.In that sense, it tends to expand casually, but be careful that it will not be possible.
If you really want to make it smaller, WSL.It seems that you will have to import the distribution as a file in EXE (use the --export option) to import the newly created distribution (--yport option).Also, after changing the maximum capacity, it is necessary to re -recognize the capacity on the WSL distribution side.
EXT4 of WSL distribution.Find VHDX
EXT4 of the first processed WSL distribution.You need to find a VHDX file.To get a virtual hard disk file with a DiskPart command, you need a full pass.
ext4.VHDX is included in the LocalState folder of the WSL distribution package folder in the "AppData \ Packages" below the user folder.If you have only one WSL distribution you have installed, it's not so difficult to find.However, if there are multiple WSL distributions, use the following method.In addition, Windows PowerShell is used for command function.In addition, the DiskPart command requires administrator privileges.
First, make sure that the processing WSL distribution is stopped.If you have an open console, you can get out of EXIT and "WSL.Execute EXE -shutdown.Next, use the following command to find the package name for the distribution.The table is the name and package name of the main WSL distribution that can be obtained.
Get-AppXpackage "*Part of the WSL distribution name*" | Select PackageFullname
Get-appxpackage argument is, for example, Ubuntu-18.If you want to find 04, specify "get-appXpackage"*ubuntu*".Once you know the package name, follow the package name under the user folder AppData \ local \ package.With PowerShell
Get-Childitem $ ENV: LOCALAPPDATA \ Packages \
Enter, enter the character of the top of the package (for example, canonical for Ubuntu system, etc.), complement it with tab key to create a package path, and then "\ localstate \ ext4..Continue with VHDX and get a full path.Copy this to the clipboard.
Then actually launch DiskPart
Start the DiskPart command.
Virtual hard disk file (.To increase the maximum capacity of VHDX), use the DiskPart command.Since the path is long and the complement function cannot be used, it is necessary to copy the full pass of the virtual hard disk file to the clipboard in advance.
If the booted console is not administrator privileges, another window will open.In order to select VDisk (virtual disk) at the prompt of the DiskPart command, insert the following subcommand.
Select VDisk File = "EXT4.VHDX full pass "
After that, the information is displayed with the "Detail VDisk" command for confirmation.If the default remains, it should be displayed as 256GB in "virtual size".
Then, the maximum size is expanded to 300GB.To do that
Expand VDisk Maximum = 307200
And
The maximum capacity of the virtual hard disk (VDISK) with the EXPAND command of DiskPart
The 307200 is 300 x 1024.In Maximum, the size is specified in MB, so it is necessary to apply 1024 to make GB.When the command is completed, end the DiskPart with the exit command.
Next, start the WSL distribution that has changed the maximum capacity of the virtual hard disk file.Be careful not to make a mistake in the target distribution.Starting is WSL.From EXE, it is better to give the distribution name properly as follows.
WSL.EXE -D Ubuntu -18.04
When it starts, check the current size of the current file system.To do that, use the DF command.
Check the device name and capacity corresponding to the root file system with the DF command or LSBLK command in advance.
DF -H
Here, check the root file system.The root file system is the device in which "/" is "/" in the output of the DF command.In the above example, it was "/dev/sdc".However, depending on the execution environment, it may be the name of another device.Once the device name can be judged, use the following command.
sudo resize2fs /dev /sdc 300g
This command changes the size of the file system in the partition.The capacity of/dev/sdc should now increase.Finally, pass the WSL with the EXIT command and shut down once.
Compared to the previous screen, the capacity of/dev/sdc is increasing.DF has a different capacity calculation method, so it does not match the display of Resize2fs or DiskPart.The LSBLK command is 300GB
Changing the maximum capacity of the virtual hard disk can be done by NTFS and can be mounted from the GUI ("computer management" → "disk management"), but since Windows tools do not support EXT4, the file system is changed.(Functions equivalent to the above Resize2fs) cannot be performed.
On the other hand, the DiskPart command is quite old in terms of the current sensation and has no complementing the path, so it is necessary to check the full pass in advance.However, the WSL distribution is distributed as a package of the Microsoft store, so it is troublesome to find that position.If possible, I want PowerShell to prepare a management tool.