Linux – How to format an NTFS drive on CentOS 7 so that I can also use it in Windows

centosfdisklinuxntfspartitioning

We have a LIDAR system that collects data in the field. The computers that process the raw data are Windows computers so we use NTFS. However, the LIDAR system itself runs CentOS 7. Normally this is no problem. We format the collection drives to NTFS on Windows or use GParted (CentOS or Ubuntu). CentOS mounts and writes to them just fine and Windows can read them.

Our customers would like to be able to format the collection drives in the LIDAR system using CentOS though. I thought this would be a trivial task and wrote a script. The script works to format the drives and CentOS will write data to them. However, after using the script Windows will no longer recognize the drives.

If GParted can make this work then there must be something wrong with they way I am formatting them. My procedure is as follows:

umount <mountPoint>

This ensures the drive is not mounted.

(echo o; echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk /dev/<driveLetter>

This uses fdisk to write a new partition table by taking the following actions:

  • o: Create a new empty DOS partition table
  • n: Add a new partition
    • p: Create a primary partition
    • 1: Partition 1
    • default first sector (showing as 65535 on a 480GB drive)
    • default last sector (showing as 937703087 on a 480GB drive)
  • w: Write the table to disk

Then…

mkfs.ntfs -f -L <driveLabel> /dev/<driveLetter>1

This formats the drive partition 1 to NTFS.

mount -a

This remounts the storage drives.

Windows cannot see the drives formatted this way. However, if I use AOMEI in Windows it will see the drives even though I cannot use them. The only clue that it gives me is that it adds a *: to the drive label. So instead of <driveLabel> it shows *:<driveLabel>.

Can anyone tell me if there is a flaw in my script or if I am missing something?

Best Answer

The underlying problem seems to be with the partition type that Microsoft (at least in Windows 7) is looking for. This may still be possible using fdisk but I didn't have time to test any further.

I ended up using gdisk instead of fdisk and I was able to get it to work.

In case anyone else is interested, here is my modified script:

umount <mountPoint>

(echo o; echo y; echo n; echo 1; echo ; echo ; echo 0700; echo w; echo y) | gdisk /dev/<driveLetter>

partprobe

umount <mountPoint>

mkfs.ntfs -f -L <driveLabel> /dev/<driveLetter>1

mount -a

The gdisk options used are as follows:

  • o: Create a new empty GUID partition table (GPT)
  • y: Verify
  • n: Add a new partition table
  • 1: Partition number
  • default first sector (shown as 2048)
  • default last sector
  • 0700: GUID for Microsoft basic data
  • w: Write table to disk and exit
  • y: Verify

After multiple tests it seems to work great.

Related Question