Skip to main content

vhd

Windows can mount vhd file as a virtual drive. We can use it for application install space or document storage area.

When we create a vhd file on a flash memory, the memory should be formatted with ntfs format.

parted /dev/sdb print
parted /dev/sdb rm 1
parted /dev/sdb mkpart primary ntfs 0% 100%
mkfs.ntfs -Q /dev/sdb1
mount /dev/sdb1 /mnt

At first a raw image file, make label, and make partition.

dd if=/dev/zero of=file.img bs=1M count=0 seek=59000
losetup -f
losetup /dev/loop1 file.img
losetup -a
parted /dev/loop1 print
parted /dev/loop1 mklabel msdos
parted /dev/loop1 mkpart primary ntfs 0% 100%
losetup -d /dev/loop1

Then format the partition in ntfs format.

kpartx -a -v file.img
mkfs.ntfs -Q /dev/loop1p1
kpartx -d -v file.img

The vhd file must not be sparse file. So after we convert it to vhd format, then we should convert it to normal file.

cp --sparse=never file.img file_new.img
qemu-img convert -O vpc -o subformat=fixed,force_size=on file.img output.vhd
cp -pi --sparse=never output.vhd.sparse file.vhd

diskpart

In windows os, we can handle partition and filesystem with sub commands of diskpart command.

format usb drive

list volume
select volume=2
format fs=ntfs quick

create a vhd file and attach it

list volume
create vdisk file="d:\image.vhd" maximum=1024
select vdisk file="d:\image.vhd"
attach vdisk
list vdisk

create partition in the vhd file

select disk 2
list disk
create partition primary

format in ntfs and assign drive letter

list volume
select volume=3
format fs=ntfs quick
assign letter=e:

detach the vhd file

select vdisk file="d:\image.vhd"
detach vdisk
list vdisk

next time, just attach it

select vdisk file="d:\image.vhd"
attach vdisk
list vdisk