Loading...
X

In-RAM filesystem to speed up file handling and reduce disk load (tmpfs)

Table of contents

1. What is tmpfs

2. What can tmpfs be used for

3. How to use tmpfs

4. Interface (script) for working with tmpfs (virtual memory filesystem)

4.1 Download the script for working with tmpfs (tmpfs-mounter)

4.2 tmpfs-mounter options

4.3 tmpfs-mounter usage examples

Conclusion


This guide will tell you about tmpfs: how to create a tmpfs filesystem; this article will also provide a script that speeds up work with tmpfs and makes interaction with the tmpfs filesystem more convenient.

1. What is tmpfs

tmpfs is a virtual memory filesystem.

The tmpfs function allows you to create filesystems whose contents are located in virtual memory. Since files in such filesystems are usually located in RAM, access to files is extremely fast.

The filesystem is automatically created when mounting a tmpfs filesystem using the following command:

sudo mount -t tmpfs -o size=10M tmpfs /mnt/mytmpfs

The tmpfs filesystem has the following properties:

  • The filesystem can use swap space when there is not enough physical memory.
  • The filesystem consumes only as much physical memory and swap space as is required to store the current contents of the filesystem.
  • During a remount operation (mount -o remount), the filesystem can be resized (without losing the existing contents of the filesystem).

If a tmpfs filesystem is unmounted, its contents are discarded (lost).

2. What can tmpfs be used for

The tmpfs filesystem is ideal for resource-intensive tasks where the bottleneck is the speed of reading and writing to disk.

See an example of using tmpfs: How to speed up the generation of dictionaries with passwords

Also, tmpfs can be useful to reduce the wear of SSD/HDD when working with large but temporary files. For example, I need to export large databases from MariaDB to files, and then these files need to be compressed into an archive and sent over the network. After the sending is complete, the file is no longer needed and is deleted. That is, my SSD is used intensively, but ultimately the files are deleted after they have fulfilled their function. In this and similar situations, tmpfs is perfect.

Note that tmpfs loses all its contents when the computer is rebooted or even when the filesystem is unmounted. Therefore, tmpfs should be used for temporary files, or after finishing working with them, make a backup copy to a regular filesystem.

3. How to use tmpfs

In fact, creating a virtual filesystem in RAM is very simple. To create a tmpfs partition, let's start by creating a mount point. You can choose a permanent location, but I prefer to create a new directory to mount the tmpfs to in the /tmp folder:

mkdir /tmp/mytmpfs

Note: everything in the /tmp folder is deleted when you reboot your computer.

Then run the following command:

sudo mount -t tmpfs -o size=5g tmpfs /tmp/mytmpfs

In this command:

  • 5g is the size of the virtual filesystem in gigabytes
  • /tmp/mytmpfs is the mount point of the virtual filesystem

That's it – you can now go into the virtual filesystem and perform various operations:

cd /tmp/mytmpfs

Now, read and write operations to the disk will be very fast (unless the CPU or RAM speed becomes a bottleneck).

If during the operation of tmpfs you encounter the fact that you do not have enough space on this partition, you can increase the size of the partition without losing the data stored on it:

sudo mount -t tmpfs -o remount,size=10g tmpfs /tmp/mytmpfs

Note the addition of the remount option: since the size is also a mount option, which is specified after -o, these two options are listed separated by commas)

If you are afraid of getting confused in the syntax, you can simply specify the -o option twice (in this case, -o remount and -o size=10g are options for mounting the filesystem):

sudo mount -t tmpfs -o remount -o size=10g tmpfs /tmp/mytmpfs

The last two commands are identical.

When remounting, the data stored on the tmpfs partition is not lost.

After rebooting the computer or unmounting tmpfs, the data on this partition will be lost.

To unmount the tmpfs partition, run the command:

sudo umount /tmp/mytmpfs

4. Interface (script) for working with tmpfs (virtual memory filesystem)

4.1 Download the script for working with tmpfs (tmpfs-mounter)

As you can see above, you can start working with tmpfs with literally one command. But if you need to use the tmpfs virtual filesystem occasionally, you have to go to the help to remember the options for this command. I have the same problem and I solved it with the help of a script that makes working with tmpfs easier.

You may be surprised, but my script has more options than tmpfs 😄

Here is the source code of the script:

#!/bin/bash

# Made by MiAl for zaLinux.ru and Suay.site

PROGNAME='tmpfs-mounter'
#PROGNAME=$0

# default values ​​of command line options
size=10
path='mytmpfs'
umount=0
show_information=0
remount=0

RED='\033[1;31m'
BBlack='\033[1;30m'
NC='\033[0m' # No Color

# if you want to launch Double Commander, then set here your username here
user=

help="""
${RED}All options are not mandatory:${NC}
	-h | --help	Show this help and exit
	-s | --size	Virtual memory filesystem size [Default: 10 GB]
	-p | --path	Path (after /tmp/) to the virtual memory filesystem [Default: mytmpfs, i.e. /tmp/mytmpfs]
	-u | --umount	Unmount the virtual memory filesystem
	-i | --info	Check if the virtual memory filesystem is already mounted
	-r | --remount	Remount the virtual memory filesystem (you can set a new size with the -s option)

${RED}Examples:${NC}

${BBlack}Show this help and exit:${NC}
	tmpfs-mounter
	sudo tmpfs-mounter -h
	
${BBlack}Mount virtual memory filesystem with default values [-s 10 -p mytmpfs]:${NC}
	sudo tmpfs-mounter

${BBlack}Mount virtual memory filesystem of 20 GB in size with default name [-p mytmpfs]:${NC}
	sudo tmpfs-mounter -s 20

${BBlack}Mount virtual memory filesystem of 20 GB in size and the path '/tmp/tmpfs-mysql':${NC}
	sudo tmpfs-mounter -s 20 -p tmpfs-mysql
	
${BBlack}Check if the virtual memory filesystem is already mounted and its size:${NC}
	sudo tmpfs-mounter -i
	
${BBlack}Remount the virtual memory filesystem with the new size 15 GB:${NC}
	sudo tmpfs-mounter -r -s 15
	
${BBlack}Unmount the virtual memory filesystem with the default value [-p mytmpfs]:${NC}
	sudo tmpfs-mounter -u

${BBlack}Unmount the virtual memory filesystem at '/tmp/tmpfs-mysql':${NC}
	sudo tmpfs-mounter -u -p tmpfs-mysql	
	
"""

usage () {
	echo -e "${RED}Discription:${NC}
${BBlack}\ttmpfs-mounter is a script for managing tmpfs (virtual memory filesystem).
\tThe script mounts tmpfs of the specified size at the specified path.
\tThe script can also remount tmpfs with a new size, check the tmpfs status and unmount tmpfs.${NC}
\n${RED}Usage:${NC}
\t${BBlack}sudo $PROGNAME [OPTIONS]${NC}
$help"
	return
}

is_tmpfs_ready () {
	if [[ -d "/tmp/$path" ]]; then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi
}

show_tmpfs_info () {
	df -hT "/tmp/$path"
}

is_tmpfs_mounted () {
	mount | grep "/tmp/$path"
}

if [ "$EUID" -ne 0 ]
	then echo -e "${RED}Please run as root${NC}"
	usage
	exit
fi

while [[ -n $1 ]]; do
	case $1 in
		-s | --size)
			shift
			size=$1
			;;
			
		-d | --doublecmd)
			doublecmd=1
			size=
			path=
			;;

		-p | --path)
			shift
			path=$1			
			;;
			
		-u | --umount)
			umount=1
			;;

		-i | --info)
			show_information=1
			;;
			
		-r | --remount)
			remount=1
			;;
			
		-h | --help)
			usage
			exit
			;;
			
		*)
			usage >&2
			exit 2
			;;
	esac
	shift
done

if [[ $show_information -ne 0 ]]; then
	if [[ $(is_tmpfs_ready) -eq 1 ]]; then
		echo 'The mount directory exists'
		show_tmpfs_info
	else
		echo 'The directory does not exist'
	fi
	exit 0
	
elif [[ $umount -ne 0 ]]; then
	if [[ $(is_tmpfs_ready) -eq 1 ]]; then
		echo
	else
		echo 'The mount directory does NOT exist'
		exit 1
	fi
	umount "/tmp/$path"	
	if [[ -z "$(is_tmpfs_mounted)" ]]; then
		echo 'Virtual memory filesystem has been unmounted!'
		rm -rf "/tmp/$path"
		echo "/tmp/$path is removed!"
	else
		echo -e "${RED}Virtual memory filesystem is still mounted!\nMaybe it is busy.${NC}"
	fi
	
	exit 0

elif [[ $remount -eq 1 ]]; then
	if [[ $(is_tmpfs_ready) -eq 0 ]]; then
		echo 'The mount directory does NOT exist'
		exit 1	
	else
		mount -t tmpfs -o "remount,size=${size}g" tmpfs /tmp/$path
		show_tmpfs_info
		exit 0
	fi

elif [[ $doublecmd -ne 0 && "$user" ]]; then
	sudo -u $user doublecmd /tmp/$path
	exit 0

elif [[ $size -ne 0 && -n "$path" ]]; then
	if [[ $(is_tmpfs_ready) -eq 1 ]]; then
		echo 'The mount directory already exists'
		show_tmpfs_info
		exit 1
	fi
	mkdir /tmp/$path
	mount -t tmpfs -o size=${size}g tmpfs /tmp/$path
	show_tmpfs_info
	echo -e "\n\nTo change your current directory, type:\n${RED}cd /tmp/$path${NC}"
fi

To use it, copy the script to the file tmpfs-mounter.

Then go to the directory with the saved script.

Next, make this file executable:

chmod +x tmpfs-mounter

Everything is ready, now you can run it.

sudo ./tmpfs-mounter -h

If you want, you can also copy this script to the directory with user executables:

sudo cp tmpfs-mounter /usr/local/bin/

In this case, you can run the program from any location, without having to go to the directory with the saved script:

sudo tmpfs-mounter -h

4.2 tmpfs-mounter options

Usage:

sudo tmpfs-mounter [OPTIONS]

All options are not mandatory:

	-h | --help	Show this help and exit
	-s | --size	Virtual memory filesystem size [Default: 10 GB]
	-p | --path	Path (after /tmp/) to the virtual memory filesystem [Default: mytmpfs, i.e. /tmp/mytmpfs]
	-u | --umount	Unmount the virtual memory filesystem
	-i | --info	Check if the virtual memory filesystem is already mounted
	-r | --remount	Remount the virtual memory filesystem (you can set a new size with the -s option)

4.3 tmpfs-mounter usage examples

To display help, run the script with the -h option (or the long version --help). Also, running without sudo will cause the options to be displayed:

tmpfs-mounter

sudo tmpfs-mounter -h

The script requires root privileges to work, so the script must be run with sudo.

When running the script without options, the virtual memory filesystem will be mounted immediately:

sudo tmpfs-mounter

By default, its size will be 10 Gigabytes, it will be located in /tmp/mytmpfs.

With the -s option, you can choose any other size, for example, here is an example of creating a tmpfs of 20 Gigabytes:

sudo tmpfs-mounter -s 20

With the -p option, you can change the name of the tmpfs. This option can be combined with the -s option. For example, in this case, “/tmp/tmpfs-mysql” is selected as the name, and the size of the virtual memory filesystem will be 20 Gigabytes:

sudo tmpfs-mounter -s 20 -p tmpfs-mysql

The following command will check the status of tmpfs. If the virtual memory filesystem is already mounted, its size and used space will be displayed (this option can be combined with -p if you created a partition with a name other than the default):

sudo tmpfs-mounter -i

The following command will remount the virtual memory filesystem with the new size:

sudo tmpfs-mounter -r -s 15

This command will unmount the virtual memory filesystem:

sudo tmpfs-mounter -u

If you used a path to tmpfs other than the default, you can add the -p option to the -u option, followed by the path to the virtual memory filesystem:

sudo tmpfs-mounter -u -p tmpfs-mysql

Conclusion

tmpfs is an easy way to speed up tasks where disk speed is the performance bottleneck.

In addition, when frequently working with temporary files, tmpfs allows you to reduce SSD wear.


Leave Your Observation

Your email address will not be published. Required fields are marked *