General Notes on System Tuning. Tuning a system requires a carefully planned proceeding. Learn which steps are necessary to successfully improve your system. Automatically mount external drives to /media/LABEL on boot without a user logged in? Note for Ubuntu Server 1. This script fails on Ubuntu Server 1. To fix the script, replace "vol_id" by "blkid - o udev" in the udev- auto- mount. I've been banging my head around this for a while now, and I think I've found a working solution. This is developed and tested on a Debian- based system, so it should work on Ubuntu. I'll point out the assumptions it makes so it can be adapted to other systems as well. It will automatically mount USB drives on plugin, and shouldn't take much to adapt for Firewire. It uses UDEV, so no monkeying with HAL/Device. Kit/GNOME- Anything. It automagically creates a /media/LABEL directory to mount the device to. However, it may interfere with other automounters; I can't test for that. I expect that, with Gnome- VFS active, both may try to do the mount .. Gnome- VFS fails the mount, it might not configure a desktop icon. Unmounting from Gnome should be possible, but might require gksudo or similar. I have not tested this on system boot, but the only reason I can see that it might not work is if it tries to mount the USB drive before the system is ready for mounts. If that's the case, you'll probably need one additional tweak to the mount script. I'm checking with Server. Fault to see if there's any advice, but not much interest in it over there.)On to it, then.
UDEV references: Background (UDEV? Whuzzat?)UDEV is the kernel's hotplug system. It's what automagically configures the proper devices and device symlinks (eg /dev/disk/by- label/< LABEL> ), both at boot time and for devices added while the system is running. D- Bus and HAL are used for sending hardware events to listeners like Desktop Environments. So when you log into GNOME and insert a CD or plug in a USB drive, that event follows this chain: kernel - > udev - > dbus - > hal - > gnome- vfs/nautilus (mount). And presto, your drive gets mounted. But in a headless system, we don't want to have to log in to get the benefits of automounting. Udev Rules. Since UDEV lets us write rules and run programs on device insertion, this is an ideal choice. We're going to take advantage of Debian/Ubuntu's existing rules, let them setup the /dev/disk/by- label/< LABEL> symlink for us, and add another rule that will mount the device for us. UDEV's rules are kept in /etc/udev/rules. Karmic), and are processed in numerical order. Any file not starting with a number gets processed after the numbered files. On my system, HAL rules are in a file called 9. I put my rules in 8. HAL. Primarily, you need to make sure these rules happen after the 6. Put this in your new rules file: # /etc/udev/rules. ADD rule: if we have a valid ID_FS_LABEL_ENC, and it's USB, mkdir and mount. ENV{ID_FS_LABEL_ENC}=="?*", ACTION=="add", SUBSYSTEMS=="usb", \. RUN+="/usr/local/sbin/udev- automounter. Make sure there's no spaces after the \, just a newline (\n). Change SUBSYSTEMS=="usb" to SUBSYSTEMS=="usb|ieee. Firewire support. If you want the device to always be owned by a particular user, add an OWNER="username" clause. If you just need the files owned by a particular user, tweak the mount script instead. Reading the Rule. This adds a program to run to the device's list of programs to run. It identifies USB partition devices by < LABEL> , then passes this information to a script that performs the mount. Specifically, this rule is matching: ENV{ID_FS_LABEL_ENC}=="?*" - - an environment variable set by an earlier system rule. Doesn't exist for non- filesystems, so that's why we check for it. We actually want to use ID_FS_LABEL for the mount point, but I haven't convinced UDEV to escape it for me, so we'll let the mount script handle that. This and other environment variables are obtained by udev using the vol_id command (deprecated). It's a handy tool to see nice quick details on a partition: $ sudo vol_id /dev/sdc. ID_FS_TYPE=ext. 2. ID_FS_UUID=a. 40d. ID_FS_LABEL=Travel Dawgs. ID_FS_LABEL_ENC=Travel\x. Dawgs. ID_FS_LABEL_SAFE=Travel_Dawgs. ACTION=="add" - - only match add events.. SUBSYSTEMS=="usb" - - only match devices that are on the USB bus. We use SUBSYSTEMS here because this matches against our device's parents; the device we're interested in will actually be SUBSYSTEM=="scsi". Matching against a parent USB device avoids adding our program to the internal drives. RUN+=".." - - not a match, but an action: add this program to the list of programs to run. In the program's arguments, %k gets expanded to the device name (eg sdc. FOO} gets the contents of environment variable FOO. Testing the Rule. The first reference link (above) is an excellent UDEV tutorial, but it's slightly out of date. The programs it runs for testing your rules (udevtest in particular) have been replaced by the catch- all udevadm utility. After you've added the rule, plug in your device. Give it a few seconds, then check to see what device it's been assigned to with: $ ls - l /dev/disk/by- label/*. Foo - > ././sda. Bar - > ././sdb. Baz - > ././sdc. If your removeable drive contains label_Baz, it's on device sdc. Run this and look at the output towards the end: $ sudo udevadm test /sys/block/sdc/sdc. LABEL_BAZ'. update_link: found '/block/sdc/sdc. LABEL_BAZ'. update_link: compare (our own) priority of '/block/sdc/sdc. LABEL_BAZ' with target 'sdc. LABEL_BAZ'. udevtest: run: 'socket: /org/freedesktop/hal/udev_event'. Look for the script name from our RUN+= rule in the last few lines (3rd from the bottom in this example). You can see the arguments that would be used for this device. You can run that command now to check that the arguments are sound; if it works on your commandline, it should work automatically when a device is inserted. You can also monitor UDEV events in realtime: run sudo udevadm monitor (see man udevadm for details on the switches). Then just plug in a new device and watch events scroll by. Probably overkill unless you're into really low- level details..). Reloading the Rules. Once you've verified the rule is getting read properly, you need to tell UDEV to reload its rules so the new one takes effect. Use any of these methods (if the first doesn't work, the second should.. Script! Actually, 2 Scripts.. Here's the first script. Since the program we run needs to complete quickly, this just spins the second script off in the background. Put this in /usr/local/sbin/udev- automounter. USAGE: usb- automounter. DEVICE. # DEVICE is the actual device node at /dev/DEVICE. Here's the second script. This does a bit more input checking. Put this in /usr/local/sbin/udev- auto- mount. You may want to tweak the mount options below. This script now handles finding the partition LABEL on its own; UDEV only sends the DEVICE name. If there's a problem mounting drives at boot- time, you can put a nice long sleep 6. I've given a suggestion in the comments for how to check (run ps to see if a webserver is running), but you'll want to tweak that for your system. I think most any network servers you might be using would suffice for this purpose - - nfsd, smbd, apache, etc. The risk, of course, is that the mount script will fail if the service isn't running, so maybe testing a particular file's existence would be a better solution.#!/bin/sh. USAGE: udev- auto- mount. DEVICE. # DEVICE is the actual device node at /dev/DEVICE. This script takes a device name, looks up the partition label and. LABEL and mounts the partition. Mount options. # are hard- coded below. DEVICE" ]; then. # test that this device isn't already mounted. DEVICE} /etc/mtab`. DEVICE} is already mounted". If there's a problem at boot- time, this is where we'd put. An example to experiment with. Assume the system is "booted enough" if the HTTPD server is running. If it isn't, sleep for half a minute before checking again. The risk: if the server fails for some reason, this mount script. A better solution would. HTTPD_UP=`ps - ax | grep httpd | grep - v grep`. HTTPD_UP" ]; do. # HTTPD_UP=`ps - ax | grep httpd | grep - v grep`. Just In Case. eval `/sbin/vol_id /dev/${DEVICE} | sed 's/^/export /; s/=/="/; s/$/"/'`. ID_FS_LABEL" ] || [ - z "$ID_FS_TYPE" ]; then. ID_FS_LABEL is empty! DEVICE}". # test mountpoint - it shouldn't exist. ID_FS_LABEL}" ]; then. ID_FS_LABEL}". # mount the device. If expecting thumbdrives, you probably want. If drive is VFAT/NFTS, this mounts the filesystem such that all files. Change to your user's UID. You may also want "gid=1. ID_FS_TYPE" in. vfat) mount - t vfat - o sync,noatime,uid=1. DEVICE} "/media/${ID_FS_LABEL}".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |