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:
- sudo mkdir /blah
- sudo mount /dev/sda1 /blah
- cd /blah/home
- sudo rm you
- sudo ln -s base you
- sudo shutdown -h now
Secondly make a log file in the home directory since this is all about mucking with the home directory
- sudo touch /home/log
- 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:
- sudo mv ivor base
- 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:
- cd; du -h | sort -h
Figure out how much space your home directory is using
- 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..
- 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
- Verify everything is working:
ls -l /home # Your home drive should be a link pointing to the ram drive.
- cat /home/log
#Your log file should show the files were copied on startup and shutdown.
- 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.