Home Using WSL 2 to create a Full image of a given drive with on the fly compression
Post
Cancel

Using WSL 2 to create a Full image of a given drive with on the fly compression

If you want to access a Linux disk format that isn’t supported by Windows, you can use WSL 2 to mount your disk and access its content.

This tutorial will cover the steps to identify the disk and partition to attach to WSL2, how to mount them, and how to access them.

Administrator access is required to attach a disk to WSL 2.

Identify the disk

To list the available disks in Windows, run:

1
wmic diskdrive list brief

Example output:

1
2
3
4
5
PS C:\WINDOWS\system32> wmic diskdrive list brief
Caption                                DeviceID            Model                                  Partitions  Size
Intel Raid 0 Volume                    \\.\PHYSICALDRIVE0  Intel Raid 0 Volume                    1           1500307522560
NVMe Samsung SSD 960 SCSI Disk Device  \\.\PHYSICALDRIVE1  NVMe Samsung SSD 960 SCSI Disk Device  4           500105249280
OCZ-VERT EX4 SCSI Disk Device          \\.\PHYSICALDRIVE2  OCZ-VERT EX4 SCSI Disk Device          2           128034708480

The disks paths are available under the ‘DeviceID’ columns. Usually under the \\.\PHYSICALDRIVE* format.

List and select the partitions to mount in WSL 2

Once the disk is identified, run:

1
wsl --mount <DiskPath> --bare

This will make the disk available in WSL 2.

Once attached, the partition can be listed by running the following command inside WSL 2:

1
lsblk

This will display the available block devices and their partitions.

Inside Linux, a block device is identified as /dev/<Device><Partition>. For example, /dev/sdb3, is the partition number 3 of disk sdb.

Example output:

1
2
3
4
5
6
7
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   0    1G  0 disk
├─sdb2   8:18   0   50M  0 part
├─sdb3   8:19   0  873M  0 part
└─sdb1   8:17   0  100M  0 part
sdc      8:32   0  256G  0 disk /
sda      8:0    0  256G  0 disk

Mount the device

If you want to create an image of this drive, than do not do this step. This step is only used for accessing Files on the drive inside of WSL.

  1. Create a mountpoint at your desired destination (normally in /mnt/ )
1
sudo mkdir /mnt/drive1p1
  1. List all available partitions for the given drive (if you already know the drives identifier you can pass it to lsblk, otherwise just run lsblk without any parameters)
1
2
3
4
5
fabian@Workstation:/mnt/d$ lsblk /dev/sdc
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdc      8:48   0 223.6G  0 disk
├─sdc1   8:49   0   256M  0 part
└─sdc2   8:50   0 223.3G  0 part
  1. Mount a partition
1
sudo mount /dev/sdc1 /mnt/drive1p1
  1. After you are done, you can unmout the drive
1
sudo umount /mnt/drive1p1

Create on the fly compressed Images of a Disk with WSL

We will use PLZIP for compression.

PLZIP - Lzip is a lossless data compressor based on the LZMA algorithm, with very safe integrity checking and a user interface similar to the one of gzip or bzip2. Lzip decompresses almost as fast as gzip and compresses better than bzip2, which makes it well suited for software distribution and data archiving.

Plzip is a massively parallel (multi-threaded) version of lzip using the lzip file format; the files produced by plzip are fully compatible with lzip.

Plzip is intended for faster compression/decompression of big files on multiprocessor machines, which makes it specially well suited for distribution of big software files and large scale data archiving. On files big enough, plzip can use hundreds of processors.

1
sudo apt-get install plzip

This creates a image-file of a disk

1
sudo dd if=/dev/sda status=progress | plzip -9 -v > ./DiskImage.img.lz

or with p7zip

1
sudo dd if=/dev/sdd status=progress | 7z a -si ./DiskImage.img.7z

With this command you can write the image-file to a disk

1
sudo plzip -v -d ./DiskImage.img.lz | dd of=/dev/sda status=progress

or with p7zip

1
sudo 7z x -so DiskImage.img.7z | dd of=/dev/sda status=progress

Detaching a disk from WSL

To detach a disk from WSL 2, run:

1
wsl --unmount [DiskPath]

If Diskpath is omitted, all attached disks are unmounted and detached.

If one disk fails to unmount, WSL 2 can be forced to exit by running wsl --shutdown, which will detach the disk.

This post is licensed under CC BY 4.0 by the author.