automount script

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

/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?

Hi,

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

well I hacked around last night and came up with this:

#!/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 :slight_smile:

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

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.

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.

This should give you what you want:

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

Ok, I got it working :slight_smile: Thanks a lot for your help!

#!/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 :frowning: