learningbydeveloping.com

 

Learning by developing

...because practice teaches you

First of all, I do not recommend you to move tmp to the usb if you are not totally sure. This might reduce the life of your usb drive(if it is flash memory) and leave it unusable.

 

In any case, if you want to move tmp to the usb drive for any reason this is the way:

 

1st. Copy the actual state of the tmp directory somewhere else:

$ cp -a /tmp/* /overlay/tmp/

 

2nd. Umount the /tmp directory. If it is in use it will fail and give some error:

$ umount /tmp

 

3rd. Since it will, most likely give an error, we have to umount it in a “lazy” way:

$ umount –l /tmp

 

4th. Once the tmp directory is umounted from memory, we will just mount it in the directory we want, for instance /overlay/tmp:

$ mount /overlay/tmp /tmp

 

One option to automatically do this would be to do it during the booting of the device in rc.local:

 

rm -rf /overlay/tmp/*

cp -a /tmp/* /overlay/tmp/

umount /tmp

[ $? -ne 0 ] && {

umount -l /tmp

}

mount /overlay/tmp/ /tmp

 

In this case the line “[ $? -ne 0 ]” checks if the result of “umount /tmp” was different to 0, it means it failed(‘0’ would mean the umount was successful) and so it umounts in a lazy way and mounts it in /overlay/tmp afterwards.

 

However, if we want to limit the size of the ‘tmp’ directory for some reason, we could do as done before but mounting the device with some special options:

 

rm -rf /overlay/tmp/*

cp -a /tmp/* /overlay/tmp/

umount /tmp

[ $? -ne 0 ] && {

umount -l /tmp

}

mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=1024K

 

You just have now to substitute 1024K (Kilobytes) to the amount of Kbytes you want to limit  tmp.

 

Note: TMP allocates memory when it needs and deallocates when this memory is not needed. Most of the times limiting the size of tmp is not necessary and I would not recommend this practice either. But in any case, we are here for trying and experimenting.

 

Have a nice week and until the next article!

 

David.

Leave a Reply