automount script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting automount script
# 1  
Old 12-01-2008
automount script

I'm attempting to take an fstab that looks something like this:

Code:
/proc      /proc       proc   rw,nosuid,nodev,noexec 0 0
/sys       /sys        sysfs  rw,nosuid,nodev,noexec 0 0
/dev/shm   /dev/shm    tmpfs  rw,nosuid,nodev,noexec 0 0
/dev/pts   /dev/pts    devpts mode=0622           0 0
/dev/fd0   /media/fd0  auto   user,noauto,exec,umask=000    0 0
/dev/cdrom /media/cdrom  auto   user,noauto,exec,ro 0 0
/dev/hdc /media/hdc  auto   users,noauto,exec,ro 0 0
# Added by KNOPPIX
/dev/sda1 /media/sda1 ntfs noauto,users,exec,umask=000,uid=knoppix,gid=knoppix 0 0
# Added by KNOPPIX
/dev/sda2 /media/sda2 ext3 noauto,users,exec 0 0
# Added by KNOPPIX
/dev/sda3 none swap defaults 0 0vv

and write a script to loop through the "# Added by KNOPPIX" entries, then cut c1-9 off the next line and run mount $whatever-I-just-detected /mountpoint and also write that mountpoint to a file.

How would I write the condition to find "# Added by KNOPPIX" and then get it to look at the next line, first 9 characters, basically how can I get sed/awk/whatever to read something like this line by line and not go to the next line until it did something?
# 2  
Old 12-02-2008
Hi,

Code:
sed -n '/^#/{N;s/^#.*\n\(.........\).*/\1/p}' fstab

-n tells sed print only requested lines
/^#/ search for lines starting with # and then execute the following commands
{N; read in next line
s/^#.*\n/ substitute the comment part by nothing
\(.........\) save the first nine characters in \1
.* substitute the rest by noting
/p} print the matched and substituted line.

HTH Chris
# 3  
Old 12-02-2008
well I hacked around last night and came up with this:

Code:
#!/bin/bash
#find the "# Added by KNOPPIX" line
grep -A 1 "KNOPPIX" etc/fstab | grep dev | cut -c1-9 > drivelist
# read a line at a time
cat drivelist | while read line;
do
        # increment a variable for sequential mount points
        let "a += 1"
        # make mountpoint
        mkdir -p "/scanmount/drive"$a
        # now mount that drive
        mount ${line} /scanmount/drive$a
done

but it doesn't work, I have to give it a fs type, which is more difficult because I have to search for all the letters between the 2nd and 3rd blank spaces on the same line to populate a fs type variable, and I can't seem to figure out how to get grep/sed/awk to do that, ideas? If I can figure that out, then I have to write something that will only mount if that mountpoint is either ntfs, fat16 or fat32. I'm really starting to enjoy the power of bash scripts, though I'm still pretty new to it, it's amazing what you can do Smilie
# 4  
Old 12-02-2008
Take a look at you fstab. The option "noauto" explicitly demands
_NOT_ to automount the device. If you want it to be mounted
automatically, change "noauto" to "auto".

Further take a look at

Code:
man mount

Your script cannot work due to the way mount uses the
fstab. If you call mount with a device listed in the fstab,
mount will mount it to the mount point specified in the fstab,
-- that's the use of the fstab -- not to a recently created
folder. So if you want the device mounted according to the fstab
extract the information from the fstab if not you have to give
the information needed on the command line.
# 5  
Old 12-02-2008
The problem is I don't know how knoppix sets the drives to noauto, I've been trying to figure that out. Also, I don't want to mount non ntfs/fat16/fat32 drives, so I still need to detect the fs type and mount them with a script, unless I can figure out how knoppix does it.

My script works and mounts the drive if I specify -t ntfs, and it generates the mount point. I will try to look into the automount scripts, but until then I'm still trying to find out how to detect text on the next line beyond the comment, and between the 2nd and 3rd blank character.
# 6  
Old 12-03-2008
This should give you what you want:

Code:
sed -n '/^\s*#/{N;s/^#.*\n//p}' fstab \
| while read device mountpoint filesystem options junk  
do    
  echo sudo mount $device -t $filesystem -o $options === YOUR TEXT === done

# 7  
Old 12-03-2008
Ok, I got it working Smilie Thanks a lot for your help!

Code:
#!/bin/bash

# find the "# Added by KNOPPIX" line
# and pipe the output to a loop
sed -n '/^\s*#/{N;s/^#.*\n//p}' /etc/fstab \
| while read device mountpoint filesystem options junk
# now we have a list of our mount information
do
    while [ "$filesystem" = "ntfs" ]
        do
        # increment drive mount variable
        let "a += 1"
        # make the sequential mount location
        mkdir -p "/scanmount/drive"$a
        # now mount that drive at the dir we just made
        mount $device -t $filesystem "/scanmount/drive"$a
        # reset filesystem variable
        filesystem="1"
        done
    while [ "$filesystem" = "fat32" ]
        do
        # increment drive mount variable
        let "a += 1"
        # make the sequential mount location
        mkdir -p "/scanmount/drive"$a
        # now mount that drive at the dir we just made
        mount $device -t $filesystem "/scanmount/drive"$a
        # reset filesystem variable
        filesystem="1"    
        done
    while [ "$filesystem" = "fat16" ]
        do
        # increment drive mount variable
        let "a += 1"
        # make the sequential mount location
        mkdir -p "/scanmount/drive"$a
        # now mount that drive at the dir we just made
        mount $device -t $filesystem "/scanmount/drive"$a
        # reset filesystem variable
        filesystem="1"
        done
done

though I'm sure there's a better way to do my nested while loops Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Need help with automount.. is not working!!

When i export the directory where the data really is, i can specify which hosts can mount it. On the remote server i create a mount point directory and then mount it to the source servers directory (that has the data). I need to run my script on Server X , i would login there and type in the... (11 Replies)
Discussion started by: bkilaru
11 Replies

2. Red Hat

Automount in RHEL

Hello experts, On my RHEL box when i mount a nfs file system using autofs, the df -t shows the file system as nfs only. For which mounts does it report the filesystem as autofs. ?? I actually want to see the filesystem getting reported as autofs instead of nfs. Pls guide me I... (1 Reply)
Discussion started by: achak01
1 Replies

3. Linux

Automount problem

Hi, Please give step by step how to do automount in linux Thanks, Mani (9 Replies)
Discussion started by: Mani_apr08
9 Replies

4. AIX

Help on Unconfiguring Automount

Hi All, Please help. I need an advise on how to Unconfigure automount please. Many Thanks. (2 Replies)
Discussion started by: EngnrRG
2 Replies

5. Solaris

Automount in Solaris 10

Hi friends I'm a newbie trying to automount a nfs shared directory. Below is the configuration I'm using FreeBSD machine as NFS server. IP Address - 192.168.1.60 # cat /etc/exports /shared 192.168.1.50 Solaris 10 as NFS client. IP Address - 192.168.1.50 # cat... (1 Reply)
Discussion started by: pankajj
1 Replies

6. UNIX for Dummies Questions & Answers

Automount issue

Folks; I'm mounting a directory on a different SUSE 10 server from my SUSE server fine. using this mount command: # mount 192.168.132.11:/var/local/new /var/local/new this command above works fine but when i added a new line to my "/etc/fstab" to be mounted automatically every time i... (2 Replies)
Discussion started by: Katkota
2 Replies

7. Solaris

CD automount does not work

Hello, I have a SUN Solaris 9 machine (Sun-Fire-V490). I put a DVD in the reader to install a software. The automount procedure did not work (vold is running) : I have nothing under /cdrom When I try "eject" command I have the answer "No default media available" When I try to mount manually the... (3 Replies)
Discussion started by: aribault
3 Replies

8. UNIX for Advanced & Expert Users

AutoMount

Hi All How do I do a auto mount to a directory in a different unix server. I am using Solaris. Please advise!! TIA Jana (7 Replies)
Discussion started by: janavenki
7 Replies

9. UNIX for Advanced & Expert Users

Automount

My site has a few sun solaris server including out NIS server and NFS server on solaris machines. we also have few suse linux and redhat linux machine. All our home directory is on our NFS server(sun Solaris) and this is automounted through /etc/auto_master and /etc/auto_home this worked fine... (1 Reply)
Discussion started by: hassan2
1 Replies

10. UNIX for Advanced & Expert Users

automount

I install an external disk on my sun solaris 8 this went fine and I was able to access all filesystem on the disk. the new disk is mounted on /local then 6 hours later files under /local/files was 1 byte in size at the same time I received the following error message in... (4 Replies)
Discussion started by: hassan2
4 Replies
Login or Register to Ask a Question