21

I have a pen drive and one partition:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 931.5G  0 disk 
└─sda1   8:1    0 931.5G  0 part /
sdb      8:16   1   7.5G  0 disk 
└─sdb1   8:17   1   7.5G  0 part

and I have formatted with command:

   # mkfs.fat -n A /dev/sdb

and it works fine.

But after then, I skimmed though the man page for mkfs:

   mkfs is used to build a Linux filesystem on a device,  usually  a  hard
   disk  partition.   The  device argument is either the device name (e.g.
   /dev/hda1, /dev/sdb2),  or  a  regular  file  that  shall  contain  the
   filesystem.   The  size argument is the number of blocks to be used for
   the filesystem.

It says mkfs should work with partition number. And my problem is why my operation works without error prompt?

Alaneuler
  • 411
  • 1
    there is no problem using the whole device instead of a single partition. this would more make sense if you want 3.5 G fat and 4G ext3/luckfs/whatever, then you would have to part and format each part. – Archemar Feb 22 '17 at 13:37
  • 1
    If you put it in a device that expects a partition table to exist, it might ask you to format... it's usually better to have a partition table even if it's not strictly required. – frostschutz Feb 22 '17 at 13:45
  • 1
    The man page says the the device is *usually a hard disk partition*. An entire hard disk is also a suitable block device. – Toby Speight Feb 22 '17 at 16:32

2 Answers2

24

Creating a filesystem on a whole disk rather than a partition is possible, but unusual. The documentation only explicitly mentions the partition because that's the most usual case (it does say usually). You can create a filesystem on anything that acts sufficiently like a fixed-size file, i.e. something where if you write data at a certain location and read back from the same location then you get back the same data. This includes whole disks, disk partitions, and other kinds of block devices, as well as regular files (disk images).

After doing mkfs.fat -n A /dev/sdb, you no longer have a partition on that disk. Beware that the kernel still thinks that the disk has a partition, because it keeps the partition table cached in memory. But you shouldn't try to use /dev/sdb1 anymore, since it no longer exists; writing to it would corrupt the filesystem you created on /dev/sdb since /dev/sdb1 is a part of /dev/sdb (everything except a few hundred bytes at the beginning). Run the command partprobe as root to tell the kernel to re-read the partition table.

While creating a filesystem on a whole disk is possible, I don't recommend it. Some operating systems may have problems with it (I think Windows would cope but some devices such as cameras might not), and you lose the possibility of creating other partitions. See also The merits of a partitionless filesystem

11

It is generally fine to put a filesystem on a whole device, rather than partitioning and formatting the partitions, if you do not intend to have more than one filesystem on your device. You will simply have to be consistent; since your put the filesystem on sda rather than sda1, you will have to mount sda as well, since sda1 will not exist at all.

The device in your question appears to be a FAT-formatted removable drive: if it is going to be used with embedded devices that may have a more or less detailed notion of how filesystems can be mounted, it may be worth testing that they do in fact support a whole-device filesystem without a partition table. (For example, cameras tend to create a single partition and format that; it's entirely possible that they would declare a partitionless memory card unusable.)

dhag
  • 15,736
  • 4
  • 55
  • 65