Script for detecting and mounting partitions automatically

Taking care of your Linux box.
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Script for detecting and mounting partitions automatically

Post by Muhammad Saad »

Assalam-o-Alaikum,

I need a script that can detect and mount all the partitions automatically. I need it for a custom live CD. Can anyone help?
And please do not tell me to go ask Google. :evil: I have already tried that.
x2oxen
Major General
Posts: 1114
Joined: Wed Aug 22, 2007 3:17 pm
Location: Faisalabad
Contact:

Post by x2oxen »

Most of the distros like ubuntu live cd automatically detect partitions and mount it for you on 1st click. for which live cd you needed that!
Muhammad Usman
+92-321-6640501
Chemonics International
http://usmanpk.com
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Post by Muhammad Saad »

I need it for a command line based custom live CD.
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Post by Muhammad Saad »

I have found a script at http://bbs.archlinux.org/viewtopic.php?id=59884

Code: Select all

# Get all other partitions
sfdisk -d | grep "^/dev/" | sed "s|\(.*\):.*Id=\(..\).*|\1 \2|" | \
    while read dev id; do
        # Ignore if id is "Extended" or "LVM", these are not usable partitions
        if [ "${id}" = "5" -o "${id}" = "8e" ]; then continue; fi
        # See if swap
        if [ "${id}" = "82" ]; then
            printf "%-12s %-12s %-8s defaults,noatime 0     0\n" \
                ${dev} swap swap >>${DEST}
            continue
        fi
        removable=""
        part=$( basename ${dev} )
        if [ $( cat /sys/block/${part:0:3}/removable 2>/dev/null ) -ne 0 ]; then
            removable="_rmv"
        fi
        mountdir=${part}${removable}
        printf "%-12s %-12s %-8s user,noauto,noatime 0     0\n" \
                ${dev} /mnt/${mountdir} auto >>${tmpfile}
        mkdir -p ${MNT}/${mountdir}
    done
Now the problem is that I do not understand what it does and what else I have to add. Can someone help?
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear Muhammad Saad,
Salam,
http://bbs.archlinux.org/viewtopic.php?id=59884 wrote:Here's a snippet I have been using in larch for generating entries in /etc/fstab, you might be able to modify it for your purposes
Best Regards.
Farrukh Ahmed
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Re:

Post by Muhammad Saad »

LinuxFreaK wrote:Dear Muhammad Saad,
Salam,
http://bbs.archlinux.org/viewtopic.php?id=59884 wrote:Here's a snippet I have been using in larch for generating entries in /etc/fstab, you might be able to modify it for your purposes
Best Regards.
I had also read this sentence but the file /etc/fstab is not mentioned in the script. That is why I asked.
And is there anything in the script that I shall need to change?
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear Muhammad Saad,
Salam,
Muhammad Saad wrote:I had also read this sentence but the file /etc/fstab is not mentioned in the script. That is why I asked.
And is there anything in the script that I shall need to change?
It will generate fstab file it self.

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear Muhammad Saad,
Salam,

Code: Select all

DEST="/root/fstab"
MNT=/mnt

# Get all other partitions
sfdisk -d | grep "^/dev/" | sed "s|\(.*\):.*Id=\(..\).*|\1 \2|" | \
    while read dev id; do
        # Ignore if id is "Extended" or "LVM", these are not usable partitions
        if [ "${id}" = "5" -o "${id}" = "8e" ]; then continue; fi
        # See if swap
        if [ "${id}" = "82" ]; then
            printf "%-12s %-12s %-8s defaults,noatime 0     0\n" \
                ${dev} swap swap >>${DEST}
            continue
        fi
        removable=""
        part=$( basename ${dev} )
        if [ $( cat /sys/block/${part:0:3}/removable 2>/dev/null ) -ne 0 ]; then
            removable="_rmv"
        fi
        mountdir=${part}${removable}
        printf "%-12s %-12s %-8s user,noauto,noatime 0     0\n" \
                ${dev} /mnt/${mountdir} auto >> ${DEST}
        mkdir -p ${MNT}/${mountdir}
    done
Generates

Code: Select all

/dev/sda1    /mnt/sda1    auto     user,noauto,noatime 0     0
/dev/sda3    /mnt/sda3    auto     user,noauto,noatime 0     0
/dev/sda4    /mnt/sda4    auto     user,noauto,noatime 0     0
/dev/sda5    swap         swap     defaults,noatime 0     0
Best Regards.
Farrukh Ahmed
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Post by Muhammad Saad »

Thanks a lot for the help.
And I think that DEST="/root/fstab" should be DEST="/etc/fstab".
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Post by Muhammad Saad »

I have two more questions:

I have seen that there is a package named autofs listed in the package manager. Can this package be used in my case? Or the script is a better solution?

Second question is that if a partition is already listed in /etc/fstab, or the script is run twice, then what will happen? :?
ghulam yaseen
Naik
Posts: 68
Joined: Thu Aug 07, 2008 6:09 pm
Location: karachi

regarding using autofs as an alternative

Post by ghulam yaseen »

Hello :),

In this case, you can also configure autofs for this purpose

FYI: http://www.faqs.org/docs/Linux-mini/Automount.html

Regards,
Ghulam Yaseen
Muhammad Saad wrote:I have two more questions:

I have seen that there is a package named autofs listed in the package manager. Can this package be used in my case? Or the script is a better solution?

Second question is that if a partition is already listed in /etc/fstab, or the script is run twice, then what will happen? :?
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Re: regarding using autofs as an alternative

Post by Muhammad Saad »

ghulam yaseen wrote:Hello :),

In this case, you can also configure autofs for this purpose

FYI: http://www.faqs.org/docs/Linux-mini/Automount.html

Regards,
Ghulam Yaseen
After reading the introduction of autofs on this page, it seems to me that it cannot be used to detect all the partitions itself. So I shall prefer the script. :)
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Post by Muhammad Saad »

I also need to find out a way so that the script is executed during the boot process. In this way, there will be no chance of running it twice as the user will not have to run it manually.

Any advice how I should do this? The version is Ubuntu 8.10.
Muhammad Saad
Battalion Havaldaar Major
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Location: Dera Ismail Khan, Pakistan
Contact:

Post by Muhammad Saad »

I have changed this script a little so that /etc/fstab remains untouched. Here it is:

Code: Select all

# Get all other partitions
sfdisk -d | grep "^/dev/" | sed "s|\(.*\):.*Id=\(..\).*|\1 \2|" | \
    while read dev id; do
        # Ignore if id is "Extended" or "LVM", these are not usable partitions
        if [ "${id}" = "5" -o "${id}" = "8e" ]; then continue; fi
        # See if swap
        if [ "${id}" = "82" ]; then
            swapon ${dev}
            continue
        fi
        removable=""
        part=$( basename ${dev} )
        if [ $( cat /sys/block/${part:0:3}/removable 2>/dev/null ) -ne 0 ]; then
            removable="_rmv"
        fi
        mountdir=${part}${removable}
        mkdir -p /mnt/${mountdir}
        mount ${dev} /mnt/${mountdir}
    done
I need experts' advice about it. Is it good enough or not?

Another thing is that running the script causes an error at the last line (done). It says "Bad substitution". However, it does not affect the result. The entries are still added to /etc/fstab. But still, I want to know what is causing this error.
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

Dear Muhammad Saad,
Salam,

I think there will be no issue but i do not know how you will going to mount them. As for mounting these partition you need to write another script. Also it will mount current partitions which were already mounted at booting.

Best Regards.
Farrukh Ahmed
Post Reply