Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 22 Oct 2018, 14:39
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
sed - grep - and Unmount all Mounts in a sub directory
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [2 Posts]  
Author Message
s243a

Joined: 02 Sep 2014
Posts: 1227

PostPosted: Thu 21 Jun 2018, 01:11    Post subject:  sed - grep - and Unmount all Mounts in a sub directory  

I created a simple script to unmount all mounts in a sub directory.

It will also clean up the empty directories but it will only do so back one level and not recurse backwards deleting directories. Solutions to the latter issue will be discussed at the end of the post.

The script is
Code:

#!/bin/sh
while read line; do
  umount -l "$line"
  rmdir $line
done < <\

  mount  | grep TazPup | \
  grep -v '/TazPup/Full' | \
  sed -n -E 's/^.*[ ]([^ ]*\/TazPup[^ ]*)[ ].*$/\1/p'  \
)

https://pastebin.com/w9dSvDwB

I will explain it a little. Mount will list all the mount points. For example:
Code:

/dev/sdc6 on /mnt/sdc6/TazPup/etc type ext4 (rw,relatime,data=ordered)


In my case I want to grab all mounted directories that have "TazPup" in the name but one can change this to their liking. The next thing I'm doing is excluding directories with "TazPup/Full" in their name.

I'll explain a bit. On a removable drive, I want to run an ISO in a chroot enviornment. I have the following paths:
Code:

/TazPup/ISOs #Put ISOs here
/TazPup/ISOs/extracted  #Put extracted ISOs here
/TazPup/TazWoof/ #An experminal Woof like system for TazPup
/TazPup/Builder/ #The tools to build a TazPup
/TazPup/Full #I extracted the ISOs fully here including the sfs files


Now chroot environment I want is in "/TazPup/Full" for example on chroot point might be
Code:

/TazPup/Full/Taz/15


for the alpha 15 version but I want from alpha15 to be able to See all other files from the TazPup root. I suppose I could create a reference within "/TazPup/Full/Taz/15" back to "/TazPup/" using bind but this would make "/TazPup/Full/Taz/15" less pristine and also I might forget to remove the back reference. Besides, if we do it the easy way then we don't get to play with scripts using sed and grep.

So with this setup the reason that I have to exclude mountpoint in /TazPup/Full is
Code:

grep -v '/TazPup/Full'

because I'm getting mounts like:
Code:

none on /mnt/sdc6/TazPup/Full/Taz/14/dev/pts type devpts (rw,relatime,gid=2,mode=620,ptmxmode=000)


I think this might be because I bound the /dev/ directory back to the host OS (tahrpup in my case) because I was directly doing my chroot in this folder before and hence now it is less pristine and as bonus maybe justification for my script. Smile

Anyway, so I have a script to map all the folders from my pseudo full install back to the /TazPup/ root called, map_distro. The script is much more complicated then it needs to be because it can also merge directories using symbolic links. It is based on my map_save script which I discussed in the thread, "Bind & Link a Save File using Bash into a Running Puppy".

The advantage of this approach is it uses less ram then a layered file system. However, if one starts merging a lot of directories all the mount points and symbolic links can start to look like a complicated structure that will take a while to mount and clean up.

The toughest thing for me to figure out was how to get sed to do what I want. I didn't find a good example but here are some links I used as guides:

sed: “repetition-operator operand invalid” using capturing groups
RegEx: Smallest possible match or nongreedy match
Invalid reference \1 using sed when trying to print matching expression
How do I get sed to read from standard input?
What characters do I need to escape when using sed in a sh script?
How to match any non white space character except a particular one? Surprised

Now after we are done we need to unmount the folders
Code:

unmount -l "$line"

and remove the empty directories.
Code:

  rmdir $line

Anyway, regarding the cleanup. One could use "rm -rf" after they do all the unmounts to delete the entire directory tree but this carries some risk if someone types the wrong thing.

Another approach to my script would be to recurse through the directories and if something is a mount point then unmount and delete the resulting empty directory. One could then recurse backwards and delete all empty directories. This might be safer but would use more memory. To detect if something is a mount point one can use code like:
Code:

IsBind="True"
mountpoint -q "$f" || IsBind="False"

like I did on lines 107 to 108 of map_distro.

Last edited by s243a on Fri 22 Jun 2018, 00:19; edited 1 time in total
Back to top
View user's profile Send private message 
s243a

Joined: 02 Sep 2014
Posts: 1227

PostPosted: Thu 21 Jun 2018, 23:50    Post subject:  

I added the following check to make sure we don't delete too much

Code:

if [ ${#ROOT} -lt 2 ]; then
  exit
fi


The complete code is now:

Code:

#!/bin/sh
ROOT=/mnt/sdc6/TazPup
declare -a  dirs_to_clean=\
( "/etc" "/DEBIAN" "/bin" "/etc" "/lib" "/lib64" \
   "/opt" "/root" "/sbin" "/var" "/usr" "/var" \
)
while read line; do
  umount -l "$line"
  rmdir $line
done < <(  mount  | grep "$ROOT" | grep -v '/TazPup/Full' | sed -n -E 's/^.*[ ]([^ ]*\/TazPup[^ ]*)[ ].*$/\1/p'  )
if [ ${#ROOT} -lt 2 ]; then
  exit
fi
for DIR in ${dirs_to_clean[@]}; do
  rm -rf $ROOT$DIR
done

https://pastebin.com/MKCkEbVL
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [2 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0728s ][ Queries: 12 (0.0208s) ][ GZIP on ]