Category Archives: freebsd

Adding a hard drive to FreeBSD with a larger than 512-byte sector size

Introduction

I recently tried to add another USB HDD drive to my FreeBSD system and forgot the steps I had taken previously to add a USB drive with a larger than 512-byte sector size and larger than 2TB capacity.

The sysinstall utility uses fdisk to try and add disks. This is the same utility that was present when I first started using FreeBSD way back in 1996 (2.1.5-RELEASE). Things have changed dramatically since. The first Gigabyte-sized hard drives had just hit the market a year or two prior. If you were lucky enough to own a massive 3.2GB HDD, you were the envy of all your friends. 56.6k was already pretty bitchin’ too. So, needless to say, as technology has evolved, the built-in FreeBSD fdisk tool has become antiquated. However, there is a replacement, and it’s so much better. Its name is gpart.

Forgetting that I had used gpart previously (I won’t forget again, since I am documenting this for my own benefit as well), I tried the sysinstall method first. This failed for a couple reasons. The first is that fdisk assumes the 512-byte sector size and I had a 4096-byte sector size. Because of this, the geometry was showing my 3TB drive as a 350GB drive. Because it never partitioned properly, the labeling tool didn’t work either. After reading a few mailing lists and blog posts about failed fdisk attempts, I remembered that I used gpart the last time around as well. Here’s step-by-step directions on how I was able to very quickly and easily get my USD HDD partitioned, labeled and mounted.

Assumptions

  • You’re installing a brand new hard drive that has no data on it, or
  • You understand that any data currently on the hard drive will be erased using this method
  • You’re using the entire disk as a single partition
  • You understand that I am not liable for any data you may lose following these directions. 🙂

Finding your HDD

I cannot give exact detailed instructions here as your drive name will depend on several factors: is your hard drive internal hard drive or an external portable USB hard drive; how many drives are currently installed; and perhaps even by which USB port you have plugged your drive into.

In my case, it was an external USB HDD. To easily find it when I plugged it in, I ran the following command:

tail -f /var/log/messages

The following was appended to the file and onto my screen:

May  3 16:50:32 gateway kernel: da2 at umass-sim2 bus 2 scbus2 target 0 lun 0
May  3 16:50:32 gateway kernel: da2:  Fixed Direct Access SCSI-6 device 
May  3 16:50:32 gateway kernel: da2: 40.000MB/s transfers
May  3 16:50:32 gateway kernel: da2: 2861556MB (732558336 4096 byte sectors: 255H 63S/T 45599C)

Easy peasy. My HDD was da2.

Learning more about your HDD

To find out what size your sectors are, the diskinfo utility is great:

diskinfo -c da2

You should see something like this:

da2
        4096            # sectorsize
        3000558944256   # mediasize in bytes (2.7T)
        732558336       # mediasize in sectors
        0               # stripesize
        0               # stripeoffset
        45599           # Cylinders according to firmware.
        255             # Heads according to firmware.
        63              # Sectors according to firmware.
        574343344530383939393633        # Disk ident.

Checking contents

Once your drive is installed and you’ve located it, it’s time to see what’s on the drive. Since most USB drives come from the store formatted to work with one operating system or another, it’s likely not entirely blank, but it needs to be for gpart to work. To see what’s on your drive, use the following command:

gpart show da2

In my case, I can see the partition that fdisk failed to create properly:

[root@gateway ~]# gpart show da2
=>       63  732558273  da2  MBR  (2.7T)
         63  732547872    1  freebsd  [active]  (2.7T)
  732547935      10401       - free -  (40M)

Deleting existing slices and partitions

Above we saw a single slice with the index of 1. To delete that slice, use the following command:

gpart delete -i 1 da2

The -i flag is used to specify which index to use. This was the result of running that command:

[root@gateway ~]# gpart delete -i 1 da2
da2s1 deleted

[root@gateway ~]# gpart show da2
=>       63  732558273  da2  MBR  (2.7T)
         63  732558273       - free -  (2.7T)

Now it’s time to destroy:

gpart destroy da2

The output looks as such:

[root@gateway ~]# gpart destroy da2
da2 destroyed

Now that we’ve scorched the earth, it’s time to rebuild.

Create and add

First, we need to create a new partition scheme. In this case, I’m using GPT.

gpart create -s GPT da2

The resulting output should look like this:

[root@gateway ~]# gpart create -s GPT da2
da2 created

Next we need to add the partition:

gpart add -t freebsd-ufs da2

If all goes well, you should see something similar to this:

[root@gateway ~]# gpart add -t freebsd-ufs da2
da2p1 added

At this point, we’re done with gpart. Time to revel in its simplicity and give a mental shout out to the fantastic developers that create such wonderful tools, with little to no fanfare. Thank you, wonderful and kind developers.

The “F” word

Now we must talk about a word that strikes fear into the heart of geriatrics everywhere. That’s right, we must talk about format. If you’re technical, you know there’s not much to it. If you’re not, the word format probably evokes feelings of anxiety and have you reaching for Xanax. I mean, seriously, who doesn’t know a grandparent, senior, etc, who hasn’t accidentally formatted their hard drive instead of a floppy? Anyway, luckily, it’s pretty foolproof using newfs, and it’s used like so:

newfs -U /dev/da2p1

The output is long, so it has been truncated:

[root@gateway ~]# newfs -U /dev/da2p1 
/dev/da2p1: 2861556.0MB (5860466600 sectors) block size 16384, fragment size 4096
        using 8492 cylinder groups of 336.98MB, 21567 blks, 21568 inodes.
        with soft updates
super-block backups (for fsck -b #) at:
 160, 690304, 1380448, 2070592, 2760736, 3450880, 4141024, 4831168, 5521312,
 6211456, 6901600, 7591744, 8281888, 8972032, 9662176, 10352320, 11042464,
 11732608, 12422752, 13112896, 13803040, 14493184, 15183328, 15873472,
 16563616, 17253760, 17943904, 18634048, 19324192, 20014336, 20704480,
[...]
 5846209984, 5846900128, 5847590272, 5848280416, 5848970560, 5849660704,
 5850350848, 5851040992, 5851731136, 5852421280, 5853111424, 5853801568,
 5854491712, 5855181856, 5855872000, 5856562144, 5857252288, 5857942432,
 5858632576, 5859322720, 5860012864
[root@gateway ~]# 

That’s it. Simple right?

Mounting

Time to mount your drive. Nothing special about this. Just as straightforward as any other drive:

mount -t ufs /dev/da2p1 /storage/usbdisk2

Time to verify:

[root@gateway ~]# df -h
Filesystem                Size    Used   Avail Capacity  Mounted on
/dev/ad0s1a               3.9G    383M    3.2G    11%    /
devfs                     1.0k    1.0k      0B   100%    /dev
/dev/ad0s1e               347G    158G    161G    50%    /usr
/dev/ad0s1d               7.8G    813M    6.3G    11%    /var
10.0.253.11:/nfs/Media    1.8T    1.6T    237G    87%    /storage/netdisk1
10.0.253.13:/nfs/Media    1.8T    1.6T    204G    89%    /storage/netdisk3
10.0.253.14:/nfs/Media    1.8T    1.6T    207G    89%    /storage/netdisk4
/dev/da0p1                3.6T    2.8T    476G    86%    /storage/usbdisk3
devfs                     1.0k    1.0k      0B   100%    /var/named/dev
/dev/da1s1d               1.3T    1.3T   -108G   109%    /storage/usbdisk1
/dev/da2p1                2.7T    8.0k    2.5T     0%    /storage/usbdisk2

Adding your /etc/fstab entry

Your fstab will likely be different, but here’s what I added:

/dev/da2p1   /storage/usbdisk2   ufs   rw,noauto   0   2

Errors

Here are a couple errors I ran into while preparing this post and becoming familiar with gpart a second time:

fdisk: could not detect sector size

Resolution: Determine your sector size with diskinfo and use gpart if your sector size is larger than 512 bytes.

gpart: Device busy

Resolution: I ran into this error when trying to gpart destroy my drive while it still had slices on it. gpart delete your slices and then try your gpart destroy again.

Conclusion

Congratulations! If you followed this to the end, you very likely have a new, quite large, HDD installed and ready for data. For me, it’s time to offload some data from usbdisk1 to usbdisk2, as apparently, usbdisk1 is 108GB over capacity!