Let's assume that we already have a primary physical drive in our system (HDD or SDD), and have added a second drive to our box, which we need to partition and mount automatically on boot.
Here's how to that:
Let's see what physical drives we have installed in our server:
Since this is our 2nd drive, you should see /dev/sdb.
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb
Let see the currently mounted drives and partition layout:
lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
Here's a full list of all available lsblk options:
NAME device name KNAME internal kernel device name MAJ:MIN major:minor device number FSTYPE filesystem type MOUNTPOINT where the device is mounted LABEL filesystem LABEL UUID filesystem UUID RO read-only device RM removable device MODEL device identifier SIZE size of the device STATE state of the device OWNER user name GROUP group name MODE device node permissions ALIGNMENT alignment offset MIN-IO minimum I/O size OPT-IO optimal I/O size PHY-SEC physical sector size LOG-SEC logical sector size ROTA rotational device SCHED I/O scheduler name RQ-SIZE request queue size TYPE device type DISC-ALN discard alignment offset DISC-GRAN discard granularity DISC-MAX discard max bytes DISC-ZERO discard zeroes data
(optional) If your initial attempt failed, and you want to start from scratch
We can do the following; otherwise skip this step:
In case the new drive is mounted, let's unmount it:
Note: the above command is spelled "umount" and NOT "unmount". This gets a lot of people.
Let's delete all partitions on the new drive:
Once in fdisk type the following:
d # this deletes the partition w # this saves the new partition structure and exist
How to partition a drive if it's LARGER than 2TB
If this drive is larger than 2TB, then we need to use parted and not fdisk--fdisk has a 2TB partition limit:
Now do following in parted, (confirming everything):
mklabel gpt unit TB mkpart primary 0 100% # Or to align properly see here: # http://h10025.www1.hp.com/ewfrf/wc/document?docname=c03479326&cc=us&dlc=en&lc=en # Example this is what I used for my 3TB HDD # mkpart primary 4096s 100% # Let's check to see if it is created: list # If everything looks good: quit
How to partition a drive if it's SMALLER than 2TB
Let's partition the new drive (/dev/sdb).
Now enter the following in fdisk, (confirming everything):
# Turn off DOS compatible mode switch units to sectors
c u # View the current partition (there should be none) p # Create the new partition n 1 # Now save/write the parition to disk and exit w
And that should create the new < 2TB partition all properly aligned, and ready to be formatted.
Let's create the new filesystem and format it.
(you can replace "private" with whatever you want):
mkfs.ext4 -L /private /dev/sdb1
Let's create the directory/location (mount point) where we'll mount the new drive partition to:
Now we mount it to test to see if there are no error:
mount /dev/sdb1 /private
Check that everything is cool using lsblk--plus we can now get the UUID of the new drive, which we'll need for our fstab:
lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,UUID
You should see your new drive listed (here is my 3TB, sdb1) along with it's UUID:
NAME FSTYPE SIZE MOUNTPOINT LABEL UUID sdb 2.7T └─sdb1 ext4 2.7T /private /private d5d7f5c6-70b7-42ea-b6f6-bd1372632eb9 sda 476.4G ├─sda1 ext4 1G /boot 59a3441c-a7f7-4ab4-aad6-5e06fadbd244 ├─sda2 ext4 473.4G / 562f20cb-e210-4e71-b50b-b6c5c2de39dc └─sda3 swap 2G [SWAP] e3b2e9d2-d074-4036-8e42-152753037ecd
Mount your new drive automatically on boot
Finally, we add our new partion to /etc/fstab so it mounts automatically on every reboot.
Let's double check our new drive's UUID
This will show a listing of UUIDs, select the /dev/sdb1
/dev/sdb1: LABEL="/private" UUID="d5d7f5c6-70b7-42ea-b6f6-bd1372632eb9" TYPE="ext4"
So using the above, we can now add this to our /etc/fstab:
Enter the following:
UUID=d5d7f5c6-70b7-42ea-b6f6-bd1372632eb9 /private ext4 defaults 1 2
This is what it looks like in my fstab:
# # /etc/fstab # UUID=562f20cb-e210-4e71-b50b-b6c5c2de39dc / ext4 noatime,nodiratime,discard,usrquota,grpquota,errors=remount-ro 1 1 UUID=59a3441c-a7f7-4ab4-aad6-5e06fadbd244 /boot ext4 defaults 1 2 # LABEL=/private /private ext4 defaults 1 2 UUID=d5d7f5c6-70b7-42ea-b6f6-bd1372632eb9 /private ext4 defaults 1 2 UUID=e3b2e9d2-d074-4036-8e42-152753037ecd swap swap defaults 0 0 tmpfs /tmp tmpfs noexec,nosuid,noatime,rw,mode=1777 0 0 # NOTICE: If you uncomment the below line, user crontabs will be deleted on each reboot. # tmpfs /var/spool tmpfs defaults,noatime,mode=1777 0 0 tmpfs /var/tmp tmpfs noexec,nosuid,noatime,rw,mode=1777 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0
And that should do it...