TripAdvisor has a fair amount of physical hardware, and one of the devops team’s responsibilities was to manage the hundreds of servers in the development environment. We provisioned new machines, retired old ones, replaced controller batteries, and constantly upgraded, added, and replaced hard drives.
Working in an AWS stack means not having to deal with most of that any more (huzzah!), but there are still times when it’s important to know the magic incantation for partitioning, formatting, and mounting a new drive. These aren’t commands you’ll use every day, but when you need to use them, it’s frustrating not to remember the exact sequence of events.
And so, the following recipe using fdisk
, mkfs.ext4
, and mount
. If you’re dealing with hard drives that are bigger that 2TB, you’ll need to use gparted
(or parted
) instead of fdisk
, but these instructions should be fine for most situations.
First, set up the partition(s)
You can see a list of all devices recognized by the system by using fdisk -l
. After you’ve determined which drive to partition (it will be the one that doesn’t have any partitions yet), you can call fdisk
on that specific device. The output will look something like the following. Note that in the following example, /dev/sdb
doesn’t have any partitions listed.
# fdisk -l Disk /dev/sda: 128.0 GB, 128035676160 bytes 255 heads, 63 sectors/track, 15566 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x7c31bd20 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 15567 124521472 8e Linux LVM Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes 255 heads, 63 sectors/track, 121601 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0x000d5eb9 Disk /dev/sdb doesn't contain a valid partition table
At this point, you’re ready to start partitioning. I typically haven’t needed to create multiple partitions, so this example will just show how to create a single partition.
NOTE: depending on your distro and version, you may have to unset the DOS compatibility flag and change display/entry units to sectors.
# fdisk /dev/sdb The device presents a logical sector size that is smaller than the physical sector size. Aligning to a physical sector (or optimal I/O) size boundary is recommended, or performance may be impacted. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): c DOS Compatibility flag is not set Command (m for help): u Changing display/entry units to sectors Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-209715199, default 2048): <enter> Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): <enter> Using default value 209715199 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
At this point, your partitions are set up.
Next, format the partition
In the following code, I create a label on the partition called /data
, which I will use later when mounting the drive. Some people prefer to use a partition’s UUID instead of a label, but I find that having the label works fine, and is more meaningful than something like f02e0e1f-6f31-4a08-8c2c-b5237e73a988
.
Note that you format the partition (/dev/sdb1
), not the drive (/dev/sdb
).
# fdisk -l ... Device Boot Start End Blocks Id System /dev/sdb1 2048 209715199 104856576 83 Linux # /sbin/mkfs.ext4 -L /data /dev/sdb1 mke2fs 1.42 (29-Nov-2011) Filesystem label=/data OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 6553600 inodes, 26214144 blocks 1310707 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 800 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
Lastly, mount the drive
We first create a mount point, specify our mount in /etc/fstab
(so that it will automatically be mounted when we boot), then mount the drive.
# mkdir /data # echo "LABEL=/data /data ext4 defaults 1 2" >> /etc/fstab # mount /data
And there you have it. Not something you’ll do every day, but as such more important to document. Enjoy your new hard drive!