Jump to content

bash scripting: determining partition types, and more...


Recommended Posts

ok, ran into another problem w/the bash scripts i'm working on.

 

i need to be able to tell if the "primary" partitions on a linux system are ext2

 

as for what is meant by primary, i'm not sure, i have to sort that out...but...i need to be able to tell what specific partitions are.

 

i've already written a script that makes sure that the /home and /usr directories are on different partitions, using the following if statement:

 

if [ `cat /etc/fstab | egrep -c "/home"`  -gt 0 -a `cat /etc/fstab | egrep -c "/usr" -gt 0 ]

 

does that make sense? and is there an extra type of bracket or something i can use to encase the two expressions on each side of the -a, safely? mostly for readability sake.

 

thanks for any help guys :) i really appreciate it.

Link to comment
Share on other sites

ok, ran into another problem w/the bash scripts i'm working on.  

 

i need to be able to tell if the "primary" partitions on a linux system are ext2  

 

as for what is meant by primary, i'm not sure, i have to sort that out...but...i need to be able to tell what specific partitions are.

would you please be a little bit more clear :)

 

<!--QuoteEBegin--><!--QuoteEBegin-->if [ `cat /etc/fstab | egrep -c "/home"`  -gt 0 -a `cat /etc/fstab | egrep -c "/usr" -gt 0 ]<!--QuoteEBegin--><!--QuoteEBegin-->

does that make sense? and is there an extra type of bracket or something i can use to encase the two expressions on each side of the -a, safely? mostly for readability sake.

 

That is a quite confusing way of doing something that is really simple. You just want to check if both strings "/home" and "/usr" are in /etc/fstab, so instead of counting the number of times that those expresions appear on that file, check if they are or not, w/o any output. (grep -q)

Notice that most of file processing or text editing commands can handle files their-selves without the need of cat.

 

The command that you want is:

 

partition_list="/etc/fstab"

if grep -q "/home" $partition_list && grep -q "/usr" $partition_list; then

   your code

fi

 

that 'if' statement evalues the return status of the two grep commands, if the return status of BOTH commands is '0' meaning that grep found the string (no error, true), then it runs 'your code'.

 

Note that instead of using fstab (static) you can use /etc/mtab (dynamic), but that would depend on what do you want to achieve.

 

hope that helps

Link to comment
Share on other sites

ok, i'll attempt to be a bit more clear on the first question.

 

the bash script has to check a linux machine to make sure that it is using the ext2 filesystem on the partitions that are considered to be primary. as for which partitions fall into the primary category, i am not sure, but i'll give an example:

 

if /dev/hda1 is ext3, or any other filesystem, then it is in error. it must be ext2, no other filesystem is permitted. note: this is simply checking to see if it is ext2, not changing it.

 

and thank you for the simplication of code, i have no idea why i used cat, i should have thought about that. i'm fairly new to bash scripting, and am learning...all be it slowly.

 

thanks!

Link to comment
Share on other sites

ok, i'll attempt to be a bit more clear on the first question.

 

the bash script has to check a linux machine to make sure that it is using the ext2 filesystem on the partitions that are considered to be primary.  as for which partitions fall into the primary category, i am not sure, but i'll give an example:

 

if /dev/hda1 is ext3, or any other filesystem, then it is in error.  it must be ext2, no other filesystem is permitted.  note: this is simply checking to see if it is ext2, not changing it.

 

OK if you are talking about 'primary' partitions versus 'logical', AFAIK, there can be 4 primary partitions (lest say hda1 to hda4), and one of them can be 'extended' in order to hold 'logical' partitions, named starting from number 5 (hda5 ...). So If I'm not wrong you can just parse the fstab file and look for the numbers of the partitions (/dev/hda# --first field of fstab--) and then check if they are ext2 (--third field--). Notice that the new nomenclature of the devfs is no more "/dev/hda1", now the partitions can also be named as "/dev/ide/host0/bus0/target0/lun0/part1", so you should know that in order to parse the fstab file. (Also you might want to take a look to fdisk(8 )).

 

But if you are talking about 'primary' as the 'root' partition, then you'll need to parse the /etc/fstab file looking for '/' to match the second field.

 

Sorry, but as I don't understand why are wrong filesystems other than ext2, and I don't know what are you trying to do, I can't give you more accurate help. :P

 

If you need help on parsing the fstab file, please ask me, I will enjoy helping you.

Link to comment
Share on other sites

not primary as in primary vs. logical, just primary as in / or /root

so, i believe parsing is what i'm looking to do, so if you could give me some help in that area, i would be grateful :)

 

believe me, i don't really understand why this is being done, but it's not really my choice...

Link to comment
Share on other sites

well, that are good news as it can be achieved even just using 'df':

mount_point="/"

filesystem="ext2"

if df -T $mount_point | grep -q $filesystem; then

    echo "partition at $mount_point is ${filesystem}: WARNING" 

fi

 

If you want to parse /etc/fstab (I insist in that you can also parse mtab), this is a cool way:

#!/bin/bash

mount_point="/"  # name of the partition that we are asking for.

filesystem="ext2" # name of the fs wanted.



root_fs=($(awk '$2 == "'$mount_point'" {print $1" "$3}' /etc/fstab)) 

# now ${root_fs[@]} is an array with the device name of the root partition and the filesystem that it uses (as in "/dev/hda1 ext2").



if [ "${root_fs[1]}" == "$filesystem" ]; then  # checks that the root partition is ext2

      echo "The partition ${root_fs[0]}, mounted at ${mount_point} is ${filesystem}: WARNING"

fi

 

I admit that there are simplier ways of doing the second script, but since you are learning, I thought that maybe you're interested in some advanced shell tips, such as conditional parsing with awk or arrays in bash ;)

 

PS: the 'WARNING" in the output is just the 'dramatic note'. I know that you are interested in the "/" being "ext2" so the warning here is out of place :P

Link to comment
Share on other sites

Just a note: The /etc/fstab file may have commented lines, which might (improbable but possible) induce errors. So to be sure, you'll need to add a step to pre-process that file in order to remove the commented lines (ie using sed). Or, better, use /etc/mtab which doesn't include commented lines, and it must show at any time the root partition (but which is not true for other partitions such as /usr /home... because mtab only shows mounted partitions).

Link to comment
Share on other sites

i'll take a look at /etc/mtab and see if it is useful

 

thing is, this has to work across various different Linux distro's (redhat, suse, mandrake, you name it) and i, personally, have no way of knowing which ones may be used-my test system is redhat 8.0

 

dunno if that makes any difference at all, about to try the code you posted in a short bit-we'll see how it turns out :)

Link to comment
Share on other sites

those little examples should work not in every linux, but in every modern UNIX (well, here I'm maybe exaggerating a bit), because all the commands used are plain standard unix tools (maybe except the "-T" flag used with 'df' which is a GNU option).

 

Also AFAIK /etc/fstab and /etc/mtab exists on every Linux

 

So don't worry :wink:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...