Skip to content

Instantly share code, notes, and snippets.

@davydany
Last active October 5, 2018 21:23
Show Gist options
  • Save davydany/c5aea9cce6d0ba6dfbf9ac45ad7becf4 to your computer and use it in GitHub Desktop.
Save davydany/c5aea9cce6d0ba6dfbf9ac45ad7becf4 to your computer and use it in GitHub Desktop.
All Things Disks

All Things Disks

This is my personal documentation for dealing with Disks on a Linux Systems. This assumes that we're on CentOS (6.8).

NOTE: This tutorial does not talk about how to attach a disk to your Linux Box, or removing a disk. This is because in a physical machine, you will literally put the disk in or pull it out. On a VM, you will

NOTE: All the commands you run are to be run as root, and so, it will require superuser permissions when running sudo ....

What is Attached to my Machine?

If you want to know, at any time, what disk is attached to your machine, run this:

$ sudo fdisk -l | grep '^Disk'

And you'll get something like this:

Disk /dev/sda: 42.9 GB, 42949672960 bytes
Disk identifier: 0x00068eb8
Disk /dev/sdb: 1073 MB, 1073741824 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_root: 38.2 GB, 38193332224 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_swap: 4227 MB, 4227858432 bytes
Disk identifier: 0x00000000

The /dev/sda and /dev/sdb are physical disks that are mounted on your system. The others are Volume Groups.

Adding a New Disk to Your System

The process of adding a new disk to your system is called mounting.

Requirements:

Before you mount a new device to your system, you will need the following:

  1. The Device you want to mount (i.e.: /dev/sdb)
    • SATA and SCSI drives will be mounted as /dev/sd[a-z]
    • IDE drives will be mounted as /dev/hd[a-z]
  2. The Directory where you will be mounting to (i.e.: /usr/local/share/foobar)
  3. How you'd like to format your disk?

Solution:

Create a Partition

You will need to first use fdisk and create a partition on your disk.

$ sudo fdisk /dev/sdb

Once inside fdisk, create a primary partition with n.

$ sudo fdisk /dev/sdb
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): 
Using default value 130

Now, let's write these changes to disk and quit.

Command (m for help): w

Now, let's format this partition.

Formatting Your Partition

In our case, we're going to format this partition with ext4. We created 1 partition in the previous step, which took up the entire disk (1st cylinder to the last cylinder).

We refer to a disk's partition like this: /dev/sdb[1-4].

We format the 1st partition with this command:

$ sudo mkfs -t ext4 /dev/sdb1

which outputs:

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65280 inodes, 261048 blocks
13052 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Mounting the Disk

We're ready to mount the disk. We create the directory we want to mount the disk's partition to and then mount it.

We're going to mount /dev/sdb1 to /usr/local/share/foobar.

$ sudo mkdir -p /usr/local/share/foobar
$ sudo mount -t ext4 /dev/sdb1 /usr/local/share/foobar

And you're done.

Verify Everything is Working Properly

Once we've mounted the disk, we'll want to make sure that the drive was mounted properly. To do this, run:

root@development-centos-68_[~]# df -h

and you will see:

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       35G  4.0G   30G  12% /
tmpfs                 1.9G  4.0K  1.9G   1% /dev/shm
/dev/sda1             477M   35M  417M   8% /boot
/dev/sdb1             988M  1.3M  936M   1% /usr/local/share/foobar

Removing a Disk to Your System

Removing means a lot of things. We're going to go thru each scenario and see what to do.

Unmount and Leave the Device Attached

As we already saw, mounting a drive to a path was simple with the mount command, and it makes a device's partition available as a directory. We will go ahead and use the umount to unmount the device's partition from the system.

Requirements:

  1. What is the directory you want to unmount? (i.e.: /usr/local/share/foobar)

Solution:

umount /usr/local/share/foobar

Mount Disk on Boot

Requirements:

(0) Have authorization to edit /etc/fstab. (1) Device Name + Partition (i.e.: /dev/sdb1) (2) Mount Point (i.e.: /usr/local/share/foobar) (3) File System Type (i.e.: ext4) (4) Options (i.e.: default, noexec) (5) Dump (i.e.: 0) (6) FSCK (i.e.: 1)

Solution:

As an Admin User, edit /etc/fstab and add the following entry:

#device        mountpoint             fstype    options  dump   fsck
...
/dev/sdb1    /usr/local/share/foobar    ext4    defaults    0    1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment