13 February 2008

Fun with /etc/fstab

So, since I'm using Fluxbox, I don't have GNOME's nice little thing where it talks to HAL and flashdrives automount and all that stuff. After having to mount as root then sudo cp stuff back and forth on my flash drive to copy everything from my broken laptop to this borrowed one, I decided it was time to learn a bit about /etc/fstab. Here's what I came up with for making mounting my flash drive more straightforward:

# flash drive - noexec so things must be copied before running to avoid [censored] with executables
/dev/sdb1 /media/disk vfat user,noauto,uid=1000,rw,noexec,sync,noatime 0 0

The first column is the device. If you look in your /etc/fstab, you probably see a bunch of UUIDs in there. Ubuntu does that to protect against things like when libata was upgraded and suddenly everyone's /dev/hda1 was /dev/sda1. I know my flash drive will always be seen as SATA, and I don't know my flash drive's UUID, so I'm just going with the label, /dev/sdb1. Next, I have the column that says where to mount it. Make this directory before you try actually mounting it. Third is the file system. Flash drives are commonly FAT32, so that's referenced with vfat. After that I have a whole big pile of options. They are as follows:

  • user: users can mount the disk without sudo
  • noauto: disk does not auto-mount from "mount -a" or when computer is booting up. If this isn't there, you'll get a little error message about it on boot, but nothing will be harmed
  • uid=1000: User number 1000 (the first user created...ie, *me*) owns the drive
  • rw: Read/Write. Just to avoid those times when you're sitting there going "I'm even using sudo and it won't copy to the flash drive!!!!"
  • noexec:Files on the drive cannot be executed, period. It at least keeps jerks from using auto-run scripts or anything like that.
  • sync: Write changes syncrhonously, ie, as they happen. If you like to pull out the flash drive without unmounting it first, this will at least protect against your 5 page paper still being in the buffer, not on the disk, when you pull it out
  • noatime: Don't write the access time to the disk. Flash media has limited writes. Minimize how many you use up

After the mount options, there are two columns left. The next one 1 if you want the disk to be backed up when the dump command is used for backups and 0 if you don't. It's probably 0. The last one is for fsck. If you don't want fsck to do a check on that disk (you probably don't), make it 0. Otherwise, make it 1.

Now, before you can actually use this, you've got to do one more thing. You've got to give your user access to that mount point. You did create the directory like I said earlier, right? If not, go do that. Since your user was the first user made, it is a member of the admin group. We're going to let /media/disk/ continue to be owned by root. That's fine. Let's just change the group to admin, so all of the admins have access. sudo chown root:admin /media/disk/ Let's also make it so that those admins can write to the partition (otherwise we can't mount to it) sudo chmod 775 /media/disk/

Assuming everything went well, you should be able to mount the drive as yourself instead of root and without specifying a whole lot of information. If you wanted to specify a lot of information on each mount, you wouldn't need to touch /etc/fstab, now would you? You should be able to mount it with just the device or just the mount point. Using both does not seem to work for me, so don't bother with the extra typing. Just type mount /media/disk/ or mount /dev/sdb1 and it should be all set to go.


Kevin Mark said...

Hi Mac,
ls -l /dev/disk/by-uuid/
should provide some enlightnement.
I just overhauled to 'the heron' and I survived ;-) modulo a few things like my taskbar sound control.

Mackenzie said...

Ah, but one of the nice things about using /dev/sdb1 is that regardless of which of my 3...no wait, 2 now (one died)...flash drives I use, I can just mount them in the same place :D If I use UUID, then each would mount in a different place.

Kevix said...

But you can make rules to make thumbdrive#1 be mounted as /dev/thumb-1 (with udev rules) and mount it as /mount/thumb-1 (with fstab). So you can make each drive have its own device and mount point which means you can do certain action for each drive. Maybe one has photos, one has source code, etc.

Brad said...

Just thought I'd mention that the uid= and gid= options for mount can take actual user/group names instead of the numbers as well... it can make things more readable at times.

Mackenzie said...

Jeez, I did RTFM, but TFM didn't say that! Silly, leaving something like that out of the manpage.