How do I format an NTFS drive on CentOS 7 so that I can also use it in Windows?

2

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?

Blackwood

Posted 2018-10-15T15:16:03.693

Reputation: 171

Few thoughts; I cannot test them now, so just a comment: (1) The partition type may be relevant, 0x07 for NTFS. (2) I think one of my Windows OS-es (old one) was picky and required the first partition to start at the sector 63 (decimal). I couldn't use this start sector directly, I created a new partition with a greater number, then moved its beginning in the expert menu (x, b). I don't remeber if the partition had to be set as active. – Kamil Maciorowski – 2018-10-15T16:56:32.593

I recalled this answer, it may be relevant.

– Kamil Maciorowski – 2018-10-15T21:01:39.663

Answers

1

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.

Blackwood

Posted 2018-10-15T15:16:03.693

Reputation: 171

Well, you didn't specify which Windows you had in mind. :) DOS partition table is a safe choice, legacy standard. I wouldn't say fdisk under Linux is obsolete; it's just a tool designed to work with the old partitioning scheme. GPT has its advantages but for a single data partition on a 480 GB HDD a DOS partition table is also OK, I think. You specified "Microsoft basic data" in GPT. Have you tried DOS partition table with 0x07 partition type if only for curiosity? I wouldn't be surprised if it was the only thing you had missed. – Kamil Maciorowski – 2018-10-15T20:40:26.763

After reading more I think you may be right. I'll test it when I get a chance and edit my answer. If that's all it is then my answer is technically wrong as is. – Blackwood – 2018-10-16T13:31:41.463

I tried using fdisk again with a DOS partition table, partition type of 0x07, and starting sectors at both default and at 63. It still doesn't work though. I'm not saying it's impossible with fdisk. I just couldn't get it to work in the 15 minutes of playing with it that I did. By the way, I was hoping that the version of Windows would be ambiguous for this task. Knowing Microsoft though I'm sure it's not. I'm currently on Windows 7 but in the future I will also need it to work for 10. Thanks for the help. You helped point me in the right direction and I learned some things. – Blackwood – 2018-10-16T13:52:29.073

OK then. Q&A upvoted for your research effort. – Kamil Maciorowski – 2018-10-16T15:13:48.860