Script for detecting and mounting partitions automatically

Taking care of your Linux box.

Script for detecting and mounting partitions automatically

Postby Muhammad Saad » Wed Jan 07, 2009 10:52 pm

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.
Muhammad Saad
Battalion Havaldaar Major
 
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Postby x2oxen » Thu Jan 08, 2009 7:41 pm

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
x2oxen
Major General
 
Posts: 1114
Joined: Wed Aug 22, 2007 3:17 pm
Website: http://usmanpk.com
WLM: x2oxen@hotmail.com
Yahoo Messenger: x2oxen
Location: Faisalabad

Postby Muhammad Saad » Thu Jan 08, 2009 7:48 pm

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
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Postby Muhammad Saad » Thu Jan 08, 2009 8:11 pm

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?
Muhammad Saad
Battalion Havaldaar Major
 
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Re:

Postby LinuxFreaK » Fri Jan 09, 2009 10:34 am

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
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Re:

Postby Muhammad Saad » Fri Jan 09, 2009 11:15 am

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?
Muhammad Saad
Battalion Havaldaar Major
 
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Re:

Postby LinuxFreaK » Mon Jan 12, 2009 9:25 am

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
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Re:

Postby LinuxFreaK » Mon Jan 12, 2009 9:34 am

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
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Postby Muhammad Saad » Mon Jan 12, 2009 10:43 pm

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
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Postby Muhammad Saad » Tue Jan 13, 2009 1:19 pm

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
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

regarding using autofs as an alternative

Postby ghulam yaseen » Wed Jan 14, 2009 2:22 pm

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? :?
ghulam yaseen
Naik
 
Posts: 68
Joined: Thu Aug 07, 2008 6:09 pm
Location: karachi

Re: regarding using autofs as an alternative

Postby Muhammad Saad » Wed Jan 14, 2009 8:10 pm

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
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Postby Muhammad Saad » Wed Jan 14, 2009 8:17 pm

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
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Postby Muhammad Saad » Fri Jan 16, 2009 5:13 pm

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.
Muhammad Saad
Battalion Havaldaar Major
 
Posts: 273
Joined: Sat Jan 27, 2007 11:07 pm
Website: http://tajassus.pk
Yahoo Messenger: muhammad.saad1
Location: Dera Ismail Khan, Pakistan

Re:

Postby LinuxFreaK » Wed Jan 21, 2009 11:04 am

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
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi


Return to “%s” Administration

Who is online

Users browsing this forum: No registered users and 2 guests

cron