Script for detecting and mounting partitions automatically
-
- 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
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. I have already tried that.
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. I have already tried that.
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
I have found a script at http://bbs.archlinux.org/viewtopic.php?id=59884
Now the problem is that I do not understand what it does and what else I have to add. Can someone help?
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
-
- Site Admin
- Posts: 5132
- Joined: Fri May 02, 2003 10:24 am
- Location: Karachi
- Contact:
Re:
Dear Muhammad Saad,
Salam,
Salam,
Best Regards.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
Farrukh Ahmed
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
Re:
I had also read this sentence but the file /etc/fstab is not mentioned in the script. That is why I asked.LinuxFreaK wrote:Dear Muhammad Saad,
Salam,
Best Regards.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
And is there anything in the script that I shall need to change?
-
- Site Admin
- Posts: 5132
- Joined: Fri May 02, 2003 10:24 am
- Location: Karachi
- Contact:
Re:
Dear Muhammad Saad,
Salam,
Best Regards.
Salam,
It will generate fstab file it self.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?
Best Regards.
Farrukh Ahmed
-
- Site Admin
- Posts: 5132
- Joined: Fri May 02, 2003 10:24 am
- Location: Karachi
- Contact:
Re:
Dear Muhammad Saad,
Salam,
Generates
Best Regards.
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
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
Farrukh Ahmed
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
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?
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?
-
- Naik
- Posts: 68
- Joined: Thu Aug 07, 2008 6:09 pm
- Location: karachi
regarding using autofs as an alternative
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
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?
-
- 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
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.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
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
-
- Battalion Havaldaar Major
- Posts: 273
- Joined: Sat Jan 27, 2007 11:07 pm
- Location: Dera Ismail Khan, Pakistan
- Contact:
I have changed this script a little so that /etc/fstab remains untouched. Here it is:
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.
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
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.
-
- Site Admin
- Posts: 5132
- Joined: Fri May 02, 2003 10:24 am
- Location: Karachi
- Contact:
Re:
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.
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