Ivor O’Connor

February 18, 2013

Tutorial On Automatically Moving Home To Ram Drive And Back On Startup And Shutdown

Filed under: bash, howto, laptop, Linux, mint 14 xfce, rsync, SSD, tutorial, ubuntu — ioconnor @ 6:36 pm

This posting is meant to compliment the “Linux Mint 14 SSD Settings” and “The Best Linux Directory Structure?“posting. The idea is to move the home directory automatically at start up and shutdown to and from the ram disk. If you are using a ram disk then your computer is very fast. Possibly more importantly you are not wearing out your hard disk or SSD because all the applications that would normally write willy-nilly to the home directory are now fooled into RAM. If you don’t have the computer automatically doing these steps you might forget. So have the computer do them for you!

Firstly everybody should know how to recover in case during boot up the home directory is pointing to a non existent directory. You’ll need to boot up from a USB stick and follow these steps which I do so often I’m pulling them from memory:

  1. sudo mkdir /blah
  2. sudo mount /dev/sda1 /blah
  3. cd /blah/home
  4. sudo rm you
  5. sudo ln -s base you
  6. sudo shutdown -h now

Secondly make a log file in the home directory since this is all about mucking with the home directory

  1. sudo touch /home/log
  2. sudo chmod 777 /home/log

Thirdly follow the directions for /etc/fstab found in my “Linux Mint 14 SSD Settings” posting. I would follow all of the directions there, even for /etc/rc.local, whether or not you have a SSD drive.

Fourthly add the following code which will copy the ram drive back to the hard disk when the computer shutsdown.

Make a file called /etc/init.d/diskhome.sh and put in it something like the following but change the username:

#!/bin/sh

ivorPrintAndLog() {
tStamp=$(date +%Y-%m-%d@%T)
echo “$tStamp $1″
echo “$tStamp $1″ >> /home/log
}

argUser=”ivor”
fUserOnDisk=$(ls -l /home | grep “$argUser -> base”)
#echo “fUserOnDisk: ‘$fUserOnDisk’”
fUserOnRam=$(ls -l /home | grep “$argUser -> /tmp/”)
#echo “fUserOnRam: ‘$fUserOnRam’”

if [ "$fUserOnDisk" ]; then
if [ "$fUserOnRam" ]; then # On disk and ram!
sOut=”Script needs fixing because it reports ‘$argUser’ has home on both ram and disk.”
else # On disk but not ram
sOut=”User ‘$argUser’ is already residing on disk so nothing to do.”
fi
else
if [ "$fUserOnRam" ]; then # Not disk but ram
sOut=”Moving ram contents for user: ‘$argUser’ to disk.”
ivorPrintAndLog “$sOut”
rsync -av –delete /tmp/home/base /home
cd /home
sudo rm $argUser
sudo ln -s base $argUser
cd -
sOut=”Moved information to disk successfully”
else # Not disk and not ram
sOut=”Error with script: User: ‘$argUser’ is not on disk or ram.”
fi
fi

ivorPrintAndLog “$sOut”

Make it executable and put links to it from the appropriate places

chmod +x /etc/init.d
cd /etc/rc0.d
sudo ln -s ../init.d/diskhome.sh K99diskhome.sh
cd ../rc6.d
sudo ln -s ../init.d/diskhome.sh K99diskhome.sh

Fifthly update /etc/rc.local so it copies the home directory to ram by adding the following lines into the /etc/rc.local

ivorPrintAndLog() {
tStamp=$(date +%Y-%m-%d@%T)
echo “$tStamp $1″
echo “$tStamp $1″ >> /home/log
}

argUser=”ivor”
fUserOnDisk=$(ls -l /home | grep “$argUser -> base”)
echo “fUserOnDisk: ‘$fUserOnDisk’”
#fUserOnRam=$(ls -l /home | grep “$argUser -> /tmp/”)
echo “fUserOnRam: ‘$fUserOnRam’”

blah() {
if [ "$fUserOnDisk" ]; then
if [ "$fUserOnRam" ]; then # On disk and ram!
sOut=”Script needs fixing because it reports ‘$argUser’ has home on both ram and disk.”
else # On disk but not ram
sOut=”Moving disk contents for user: ‘$argUser’ to ram.”
ivorPrintAndLog “$sOut”
rsync -av /home/base /tmp/home
cd /home
sudo rm $argUser
sudo ln -s /tmp/home/base $argUser
cd -
sOut=”Moved information to ram successfully”
fi
else
if [ "$fUserOnRam" ]; then # Not disk but ram
sOut=”User ‘$argUser’ is already residing in ram so nothing to do.”
else # Not disk and not ram
sOut=”Error with script: User: ‘$argUser’ is not on disk or ram.”
fi
fi
ivorPrintAndLog “$sOut”
}

blah

Finally, the sixth step, rename your home directory to “base” and put a link to it. In my case:

    1. sudo mv ivor base
    2. sudo ln -s base ivor

That is all there is to it. The next time you reboot you should find your home directory on the tmp ram drive. Now there are a few problems. Like your home directory is too large. Or you would like to manually backup. The following commands may help:

    1. cd; du -h | sort -h
      Figure out how much space your home directory is using
    2. cd; sudo mkdir /home/Downloads; chmod 777 /home/Downloads; mv Downloads/* /home/downloads/; rm Downloads; ln -s /home/Downloads Downloads
      Basically just moving your Downloads directory which is probably rarely used to another place so it is not copied on to the ram disk. Maybe there are other directories that could also be moved because they take up lots of space and rarely get used. Like pictures, music, etc..
    3. Periodically check your ram disk usage. Mine is usually under 1GB which is less than 50% of it’s capability. (The RAM drives default to half your installed ram and the assumption is you have 4 GBs or more of RAM on your system.)
      df -h
    4. Verify everything is working:
      ls -l /home # Your home drive should be a link pointing to the ram drive.
    5. cat /home/log
      #Your log file should show the files were copied on startup and shutdown.
    6. Backup your ram drive from time to time using the bold red line above. Maybe even make it into an alias like this:
      echo ‘alias bkup=”rsync -av –delete /tmp/home/base /home”‘ >> ~/.bashrc

I’ve used these steps on a few computers and they work for me. However if you run into problems let me know and I’ll update this with the fixes.

9 Comments »

  1. One question that’s been bugging me;

    What happens when there is an unexpected shutdown after a significant amount of work has been done?

    As a side note to that; has it happened to you yet while running this way?

    Comment by joshfindit — April 23, 2013 @ 12:36 am

    • That’s a very good question. One I thought about extensively at the start. I’ve lost bits and pieces but nothing crucial. The issue has moved off my radar. To be replaced by another issue.

      I loved this idea so much that I set up every linux box I own this way. And others. So probably all-in-all a few over 10. The problem that replaced the obvious problem has become booting up off a USB thumb drive and setting the home directory back to the hard disk after a power failure. Can’t tell you how many times this has happened. I really need to make a script that does this for me automatically!

      I have installed email, dropbox, and other programs that take up a lot of space or can’t afford to get deleted as soft links from my home directory to real hard disk space. Usually to something like /home/dropbox. I like the /home directory for storing anything that did not get installed as part of the system. And for programming stuff I’m always saving to multiple repositories.

      Comment by ioconnor — April 23, 2013 @ 2:05 am

      • With linking valuable data directly to HD space negating 90% of the issues, I can see another benefit to this; if power is lost while the system is in ram, there is less chance of file system corruption since there is no hdd write to interrupt.

        I’ve gone through the instructions a few times, and while I’m on he road to being familiar enough to understand it, I’m still having trouble understanding where the system needs fixing after power loss; shouldn’t the system essentially pull the last ‘saved’ copy at boot when cloning the HD copy to RAM?

        Comment by joshfindit — April 23, 2013 @ 2:44 am

        • Sorry for the delayed response JoshFindIt. I tell myself I do these posts for myself so I can find useful information to me in the future.

          Anyways the problem is your home directory’s soft link is set to the ram drive when the power fails. So when you turn the computer back on it fails at start up because it can’t find your home folder. So on my keyring I have a USB thumb drive to boot up the computer with and then mount the hard disk and change the soft link. It does not happen often but enough so that you’ll soon memorize those commands.

          Comment by ioconnor — April 28, 2013 @ 1:51 pm

          • I think I understand the problem, and I’m just curious as it whether it could be resolved by changing the order of the scripts.
            For example, have the process run close to when the physical drives are mounted, and before the user folders are needed.
            In your specific setup (and tbh, I think many use cases fit this), we’re only doing it for one user, so it should be feasible to have a root level script on boot.

            Granted, I mainly use Linux as a platform for daemon-type applications, so there may be some boot-level knowledge that I’m missing in order to come up with a solution.

            Comment by joshfindit — May 2, 2013 @ 3:35 am

  2. edit: setting email followups

    Comment by joshfindit — April 23, 2013 @ 12:37 am

  3. Hi, great idea!, how would this apply to a system that mounts home folder using autofs?
    Thanks!

    Comment by Mihai — October 21, 2013 @ 8:09 am

    • I am not familiar with autofs. (I looked it up naturally but when I say I have no experience I mean I am not able to give you a solution I have actually tried.)

      Comment by ioconnor — October 22, 2013 @ 4:59 am

  4. Thanks a lot anyway, your solution is inspiring. The solution might be in the script map file of autofs (that means that autofs can run a script and then mount what the script outputs). I have to try it, but not enough time.

    Comment by Mihai — October 22, 2013 @ 1:52 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: