1

I currently have a single logical drive and would like to mount another drive to a single folder on a centos 7 system. The current disk configuration looks like this.

NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  1.3T  0 disk
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0  1.3T  0 part
  ├─centos-root 253:0    0  1.2T  0 lvm  /
  ├─centos-swap 253:1    0    4G  0 lvm  [SWAP]
  └─centos-home 253:2    0  160G  0 lvm  /home
sr0              11:0    1 1024M  0 rom

I want to mount an empty 2TB hard drive to /var/www/mysite.com/public_html/content where content is an empty subdirectory so that I can start serving content from that folder.

After reading here and here and this StackExchange thread I'm still a bit nervous that I will break something. It's not a mission critical commercial application, so it wouldn't be the end the world. Even still I may jump from a short ladder if it messes everything up too bad.

The filesystem type for root and home are xfs, which I think is something I'll need to specify in the mount command.

What is the correct commandline syntax for mounting the drive in the way described above? Aside from popping the drive in and typing a mount command, what else am I missing, like will the folder inherit the ownership and permissions of the parent directory?

1 Answers1

2

You need to partition the drive, format it, mount it, and then change the ownership & perms.

Strictly speaking, partitioning is not absolutely required (you can format and mount the entire unpartitioned disk) - but partitioning IS a good way of indicating that that drive is in use and has data on it, and also protecting it from over-eager GUI utilities that may "helpfully" offer to format it for you. It is very strongly recommended that you do not use a bare, unpartitioned drive.

Anyway, assuming that when you install the drive it appears as /dev/sdb (it almost certainly will, but if it doesn't adjust the following to suit the actual device name):

sudo fdisk /dev/sdb

create one partition spanning the entire disk. It will probably be the default, but if isn't make sure the partition starts from sector 2048 - this will ensure that the partition is correctly aligned whether it has 512-byte or 4096-byte sectors.

Then format it as XFS:

sudo mkfs.xfs /dev/sdb1

Next you need to add an entry to /etc/fstab for your newly formatted partition. The following will extract the UUID from the newly formatted block device and append it to /etc/fstab:

blkid -s UUID /dev/sdb1 | 
 awk -F': ' '{print $2, "/var/www/mysite.com/public_html/content xfs defaults 0 1"}' | 
 sudo tee -a /etc/fstab

You probably should have defaults,relatime,nodiratime rather than just defaults there. Use the mount options that you need.

If you're doing this as root (rather than as a non-root user with sudo), then you can skip the pipe to sudo tee ..., and just use an append redirection >> /etc/fstab.

NOTE: blkid is part of the util-linux collection. Make sure you have that installed before running it as described above.

Next, mount it and set the correct ownership and permissions. Setting owner and perms only has to be done once, after the first mount - every time it is mounted after that, they will be correct (i.e. they are stored with the mounted drive, not with the mount-point they are mounted on).

sudo mount /var/www/mysite.com/public_html/content
sudo chown user:group /var/www/mysite.com/public_html/content
sudo chmod permissions /var/www/mysite.com/public_html/content
cas
  • 78,579
  • If there is any chance you might want to use some of the 2TB space for other things, then either create /dev/sdb1 to be the maximum size you think you'll need for it, and leave the rest unpartitioned for later use, or create a new LVM PV with the entire drive and create an LV to use instead of /dev/sdb1. The advantage of an LV is that it's easily extended later if you need more space on it. – cas Aug 13 '17 at 09:19
  • Wow. Thank you for such a thorough answer. I undestand all the steps but the middle one. What does adding the UUID to the fstab file do? What's the risk factor for breaking that file and subsequently my system? – Aunt Jemima Aug 14 '17 at 18:53
  • It adds more than just the UUID, it creates a complete, working entry for /etc/fstab that the system will use to automount the fs on every reboot. Try running it without the final pipe to sudo tee to see what it will actually add to /etc/fstab. – cas Aug 15 '17 at 01:31