Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Automatically update files upon mount
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
FlyingFoX
n00b
n00b


Joined: 02 Dec 2008
Posts: 8

PostPosted: Tue Dec 02, 2008 1:50 am    Post subject: Automatically update files upon mount Reply with quote

I want to do the following:
My partition /dev/sdb3 is usually mounted to /usr/portage/distfiles. But as sdb is an external hard drive it is not always connected to my computer and therefore /usr/portage/distfiles is sometimes empty. When /usr/portage/distfiles is empty i want to be able to put files there and when i connect /dev/sdb3 to my computer again i want the files from /usr/portage/distfiles to be copied to /dev/sda3 then have /dev/sda3 mounted again.

Do you have any suggestions how I can accomplish this behaviour automatically without copying those files manually at every new mount of /dev/sda3?

I already thought about writing a script that syncs /usr/portage/distfiles before mounting /dev/sda3 again, but don't really know how to invoke this automatically before every remount of /dev/sda3.
I could as well mount /dev/sda3 at a totally different position (let's say /mnt/sda3) and run my script after the actual mount process. Doing a sync between /usr/portage/distfiles and /mnt/sda3, deleting the content of /usr/portage/distfiles and then making /usr/portage/distfiles a symlink to /mnt/sda3. But there again I don't know how to invoke my script automatically after /dev/sda3 was mounted.

I wonder if there is something like an "overlay" of the file system, that behaves as desired, but i haven't heard of anything like that yet and don't even know how to search for that.

Any ideas and hints are welcome.
Back to top
View user's profile Send private message
richard.scott
Veteran
Veteran


Joined: 19 May 2003
Posts: 1497
Location: Oxfordshire, UK

PostPosted: Wed Dec 03, 2008 4:18 pm    Post subject: Reply with quote

with udev you can get it to run a command i.e. a script each time a device is found.

you could use that to copy files over each time you plug the drive in?
Back to top
View user's profile Send private message
FlyingFoX
n00b
n00b


Joined: 02 Dec 2008
Posts: 8

PostPosted: Thu Dec 04, 2008 12:19 am    Post subject: Reply with quote

udev looks like a good idea. I will read try if i can get it to work with udev.
Back to top
View user's profile Send private message
richard.scott
Veteran
Veteran


Joined: 19 May 2003
Posts: 1497
Location: Oxfordshire, UK

PostPosted: Thu Dec 04, 2008 1:09 am    Post subject: Reply with quote

FlyingFoX wrote:
udev looks like a good idea. I will read try if i can get it to work with udev.


this may help:

http://www.reactivated.net/writing_udev_rules.html#external-run
Back to top
View user's profile Send private message
FlyingFoX
n00b
n00b


Joined: 02 Dec 2008
Posts: 8

PostPosted: Sun Jan 11, 2009 12:25 pm    Post subject: Reply with quote

Thanks richard.scott! Udev nearly solved my problem, but unfortunately not completely.
After i spent some time on this topic and created an udev script that updates my files when plugging in the device I got another design problem here. When i (manually) unmount the partition the udev script is of course not triggered because the device is still plugged in, but just not mounted any more. So my files won't be updated when I unmount and then remount the partition some time later.
If I understood udev correctly this behaviour cannot be achieved with udev scripts.

So I am thinking of how else to do this.

I thought about moving "mount" and "umount" to some other place and replace them with a script that takes care of my special partition, but this is maybe not a so good idea. Does someone have another idea of how to trigger a script on every mount/umount of a partition or achieve my goal in another way?
Back to top
View user's profile Send private message
richard.scott
Veteran
Veteran


Joined: 19 May 2003
Posts: 1497
Location: Oxfordshire, UK

PostPosted: Mon Jan 12, 2009 10:43 am    Post subject: Reply with quote

You could run a cron script like this one:

Code:
#!/bin/bash

# MP = the mountpoint for your usb drive
MP="/mnt/usb"

# If .timestamp is there then USB drive must be mounted
if [ -f ${MP}/.timestamp ]; then
  # Check if the .timestamp File's data was last modified more than 120 minutes ago
  if [ $(find ${MP} -name .timestamp -mmin +120 | wc -l) -eq 1 ]; then

    # ...
    # Update some files
    # ...

   # Update the data in the .timestamp file
   echo "$(date)" > ${MP}/.timestamp
 
  fi
fi


It checks for a .timestamp file to exist in the mounted directory of your usb device and would only run your update commands if 120min has passed since the last update.

You'll need to first mount your device and then create a .timestamp file in its root and then add this script to root's crontab to run each min. Obviously you can tweak the 120 setting in the find command to reduce how often a gap there is between updates?

Hope this helps.

Rich
Back to top
View user's profile Send private message
FlyingFoX
n00b
n00b


Joined: 02 Dec 2008
Posts: 8

PostPosted: Tue Jan 13, 2009 5:19 pm    Post subject: Reply with quote

This seems a good solution for me. I'll try that out.

Thanks for the help!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
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