0

Im trying to write a batch script that will find a specific file (EX .homemnt) on a random partion then mount that partition to what ever location I want (EX /home)..

Added notes: Will be used on multiple PCs with different device partitions location for specific folder (ex: /home)

Long story: Im working on a project that envolves LIVECD and I currently mount the home partition as writable but I have to manually find the device partition on each different computer. Im not trying to access other users files only boot to live cd mount home folder partition then log off/on and all the configs, data is there.. Currently it works great but I need a script that is "dummy" proof to allow others to boot to livecd and not have to Guess at partitions or devices.

Ive done scripts for awhile but nothing this advanced and I could use some help.

My Ideas but willing to consider anything at this point. Example script mounts 1 partions to /lookup search for file (.homemnt) then dismounts partition.. If it does not find file, moves on to next device/partition IF it find file then it save location as a variable,, UNMOUNTS partition

I then need to be able to take that variable to call it in a mount command. and mount partitsion as /home I dont need to mount specific users folder, just the whole partition to /home

im new to posting stuff and I hope that i did not overlook the answer somewhere else. Any help will be very much appriciated.

3
  • 1
    that would be easier to identify only the filesystem via its uuid; then you don't have to mount it to look for .homemnt. You can get uiud with blkid
    – solsTiCe
    May 29, 2015 at 13:29
  • Thanks for replying.This will be used on multiple pc each having their own UUID and custome device.. BUT only one home partition.
    – mgeo202
    May 29, 2015 at 13:58
  • actually untill i can find the script that does what I want I can code all the UUID into the basic mount command one line at a time.. Will fail untill it get a correct UUID... not what i was looking for but its a great idea untill i figure the rest out.
    – mgeo202
    May 29, 2015 at 14:39

1 Answer 1

1

That should do it

#!/bin/bash

declare -a mounted
i=0
while read m
do
    mounted[$i]="$m"
    i=$((i+1))
done < <(mount|grep ^/dev|awk '{print $1}')

function ismounted {
    for i in ${mounted[*]}
    do
        if [ "$1" == "$i" ] ;then
            return 0
        fi
    done
    return 1
}

tmp=`mktemp -d /tmp/mount.XXXXXX`
found=""
while read m
do
    if ! ismounted $m; then
        mount $m $tmp 2>/dev/null
        r=$?
        if [ $r -eq 0 ] ;then
            if [ -f $tmp/.homemnt ] ;then
                found=$m
            fi
            umount $m
        fi
        if [ -n "$found" ]; then
            break
        fi
    fi
done < <(blkid -o device)
rmdir $tmp
if [ -n "$found" ] ;then
    mount $found /home
fi
echo $found
3
  • Sweet!! I will test it out here soon and give feed back on it.
    – mgeo202
    May 29, 2015 at 15:30
  • It did not work for some reason but when I added a few echo commands to see what was happening to the variables it started working.. not sure why but im re-arranging my partions and gonna to another test.
    – mgeo202
    May 29, 2015 at 21:12
  • Working now..mounts home dir to linux iso and im able to apply settings and stick. THANKS!!!
    – mgeo202
    May 29, 2015 at 22:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .