Skip to content

Instantly share code, notes, and snippets.

@kumbasar
Created April 25, 2019 19:29
Show Gist options
  • Save kumbasar/49906cb704ce9213c972a3e008c74c0c to your computer and use it in GitHub Desktop.
Save kumbasar/49906cb704ce9213c972a3e008c74c0c to your computer and use it in GitHub Desktop.
How To Create a NTFS Image File in Linux
#!/bin/bash
set -x
image="test.img"
label="test"
mntdir=`mktemp -d`
sudo dd status=progress if=/dev/zero of=$image bs=6M count=1000 && sync
echo 'type=7' | sudo sfdisk $image
LOOPMOUNT=`sudo losetup --partscan --show --find ${image}`
echo $LOOPMOUNT
sudo mkfs.ntfs -Q -v -F -L ${label} ${LOOPMOUNT}p1
sudo mount ${LOOPMOUNT}p1 ${mntdir}
# Now you can put some files to ${mntdir}
sudo umount ${mntdir}
sudo losetup -d ${LOOPMOUNT}
@kumbasar
Copy link
Author

/home/kumbasar$ ./test1.bash
+ image=test.img
+ label=test
++ mktemp -d
+ mntdir=/tmp/tmp.8iWf5jvbqm
+ sudo dd status=progress if=/dev/zero of=test.img bs=6M count=1000
4951375872 bytes (5.0 GB, 4.6 GiB) copied, 3.00505 s, 1.6 GB/s
1000+0 records in
1000+0 records out
6291456000 bytes (6.3 GB, 5.9 GiB) copied, 3.95805 s, 1.6 GB/s
+ sync
+ echo type=7
+ sudo sfdisk test.img
Checking that no-one is using this disk right now ... OK

Disk test.img: 5.9 GiB, 6291456000 bytes, 12288000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

>>> Created a new DOS disklabel with disk identifier 0x492cf99d.
test.img1: Created a new partition 1 of type 'HPFS/NTFS/exFAT' and of size 5.9 GiB.
test.img2: Done.

New situation:

Device     Boot Start      End  Sectors  Size Id Type
test.img1        2048 12287999 12285952  5.9G  7 HPFS/NTFS/exFAT

The partition table has been altered.
Syncing disks.
++ sudo losetup --partscan --show --find test.img
+ LOOPMOUNT=/dev/loop2
+ echo /dev/loop2
/dev/loop2
+ sudo mkfs.ntfs -Q -v -F -L test /dev/loop2p1
The partition start sector was not specified for /dev/loop2p1 and it could not be obtained automatically.  It has been set to 0.
The number of sectors per track was not specified for /dev/loop2p1 and it could not be obtained automatically.  It has been set to 0.
The number of heads was not specified for /dev/loop2p1 and it could not be obtained automatically.  It has been set to 0.
Cluster size has been automatically set to 4096 bytes.
To boot from a device, Windows needs the 'partition start sector', the 'sectors per track' and the 'number of heads' to be set.
Windows will not be able to boot from this device.
Creating NTFS volume structures.
Creating root directory (mft record 5)
Creating $MFT (mft record 0)
Creating $MFTMirr (mft record 1)
Creating $LogFile (mft record 2)
Creating $AttrDef (mft record 4)
Creating $Bitmap (mft record 6)
Creating $Boot (mft record 7)
Creating backup boot sector.
Creating $Volume (mft record 3)
Creating $BadClus (mft record 8)
Creating $Secure (mft record 9)
Creating $UpCase (mft record 0xa)
Creating $Extend (mft record 11)
Creating system file (mft record 0xc)
Creating system file (mft record 0xd)
Creating system file (mft record 0xe)
Creating system file (mft record 0xf)
Creating $Quota (mft record 24)
Creating $ObjId (mft record 25)
Creating $Reparse (mft record 26)
Syncing root directory index record.
Syncing $Bitmap.
Syncing $MFT.
Updating $MFTMirr.
Syncing device.
mkntfs completed successfully. Have a nice day.
+ sudo mount /dev/loop2p1 /tmp/tmp.8iWf5jvbqm
+ sudo umount /tmp/tmp.8iWf5jvbqm
+ sudo losetup -d /dev/loop2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment