Removable EXTx drive for non-root Linux user

Today, we will cover setting up an external removable drive using the EXT3 file system, and to allow us to mount the drive as a normal (non-root) user. This allows us to keep existing Linux file permissions when we write to the drive, and features journaling which ensures that what we write to the removable drive, is completely written to disk.

This system also allows us to easily create separate mount points for distinct removable drives, which facilitates the use of multiple drives in a scripted backup strategy.

So let’s get started!

First thing we do is determine how the drive is recognized by the kernel:

From the command prompt:

sudo tail -f /var/log/messages
or
su -c tail -f /var/log/messages

and look for something on the order of “[sdb] Attached SCSI disk” after we plug in our removeable device.

The kernel output will tell us if there are any existing partitions (look for “sdb1″, “sdc1″ etc.)

Ctrl-C to exit tail

Next we need to create a partition table, if there are existing partitions you’ll need to ensure thay do not contain important data before you remove or overwrite them.

Again from the command prompt:

sudo fdisk /dev/sdb
or
su -c fdisk /dev/sdb

Choose “n” for “new partition”

Then “p” for “primary partition”

Then “1” for the first partition.

Then the defaults for the sectors.

Then “w” to write changes and quit.

Our next step is to create a filesystem

Again from the command prompt:

sudo mkfs.ext3 /dev/sdb1
or
su -c mkfs.ext3 /dev/sdb1

This might take a while depending on the drive size

While we wait, open another terminal window and continue working.

We are going to name our disk “BU_Linux” but feel free to change the name, but avoid spaces and wildcard/reserved characters in the name (No: “.”, “\”, “?”, “!”, etc.). We will mount the disk using the same name under the “/media” directory.

Again from the command prompt:

sudo mkdir -p /media/BU_Linux
or
su -c mkdir -p /media/BU_Linux

Next we need to backup “/etc/fstab”

sudo cp /etc/fstab /etc/fstab.org
or
su -c cp /etc/fstab /etc/fstab.org

Then we can edit “etc/fstab” using vi:

sudo vi /etc/fstab
or
su -c /etc/fstab

and add a line as below:

"LABEL=BU_Linux  /media/BU_Linux  ext3  defaults,user,noauto  0 0"

We then will go back to our prior console window, hopefully mkfs.ext3 has finished.

We now create the disk label using “e2label” which can display or change the filesystem label on the ext2, ext3, or ext4 filesystems.

Again from the command prompt:

sudo e2label /dev/sdb1 BU_Linux
or
su -c e2label /dev/sdb1 BU_Linux

Now let’s mount the drive!

Again from the command prompt:

mount -L BU_Linux

Let’s check if the drive is mounted.

Again from the command prompt:

mount |grep BU_Linux

Which should return:

“/dev/sdb1 on /media/BU_Linux type ext3 (rw,noexec,nosuid,nodev,user=MyUserName)”

Now let’s examine the drive.

Again from the command prompt:

df -h /media/BU_Linux

Which tells us how much free space is available

and

ls -al /media/BU_Linux

which should show us the “lost+found” directory

Now if we try to write a file ie.:

echo "This is a test" > /media/BU_Linux/test.txt

You’ll see that you cannot write to the disk.

So let’s fix this!

Again from the command prompt:

sudo chmod 777 /media/BU_Linux
or
su -c chmod 777 /media/BU_Linux

And try again!

echo "This is a new test" > /media/BU_Linux/new_test.txt

And we should have success.

This change will survive remounts because the permission you just changed is resident on the volume’s filesystem.

When you are finished you’ll need to unmount the file system before removing the drive.

Again from the command prompt:

umount /dev/sdb1