# tmpfs little help



## sk8harddiefast (Oct 25, 2010)

I try to understand what exactly is tmpfs. I read on Google but I didn't understand what exactly does and why make Os run faster. Also. What I should write to add tmpfs on fstab? Now the only thing I have done is to add this line 
	
	



```
tmpfs_load="YES"
```
 on loader.conf


----------



## fronclynne (Oct 25, 2010)

tmpfs(5) is a very fast version of a regular filesystem on a memory disk.  I don't know how optomized it is on FreeBSD, but NetBSD claims it's much much faster than the multi-layered approach of putting a regular (UFS) filesystem on a md(4).

Keep in mind too that when using it for, say, a ports build you don't have to clean your working directories (not that that takes much time anyway), just umount the build directory when done.

The biggest disadvantage is to make data persist across reboots, you have to actively back-up and then restore it.  So if you were going to use a tmpfs(5) for your /usr/obj/ you would do something like 
	
	



```
# mount /usr/obj     (as a tmpfs)
# cd /usr/src && make buildworld && make buildkernel
# tar -zcvf /usr/tmp/buildworld_23_10_2010.tar.gz -C /usr/obj
# make installkernel && shutdown -r now
. . .
# mount /usr/obj && tar -zxvf /usr/tmp/buildworld_23_10_2010.tar.gz -C /usr/obj
# cd /usr/src && make installworld && mergemaster && umount /usr/obj
```
  Etc etc.

For your fstab(5)

```
tmpfs /your/mountpoint/here tmpfs rw,size=469762048 0 0
```
AFIK, you must have a size= argument(I'm wrong about this), size= can also be in things like 448m or 5g(no, it can't, I'm wrong about this, too).

I'm pretty sure it's a good idea to leave pass and dump numbers as 0, I think the only other argument in the options field is noauto.


----------



## sk8harddiefast (Oct 25, 2010)

Ok. I start understand some things. Can someone help me setup it?


> ```
> tmpfs /your/mountpoint/here tmpfs rw,size=469762048 0 0
> ```


/your/mountpoint/here... What mountpoint?


> AFIK, you must have a size= argument, size= can also be in things like 448m or 5g.


I have 2 Giga Ram and 4 giga swap. I don't know if is needed information.


----------



## Beastie (Oct 25, 2010)

sk8harddiefast said:
			
		

> Now the only thing I have done is to add this line
> 
> 
> 
> ...


You can remove it. tmpfs.ko will be loaded automatically once the *tmpfs* is mounted during startup (or when you mount it manually).



			
				sk8harddiefast said:
			
		

> /your/mountpoint/here... What mountpoint?


Any mountpoint you want, just like a second internal disk, external USB disk/pendrive, etc. Just create some directory somewhere and mount the *tmpfs* on it.


~~~~




			
				fronclynne said:
			
		

> AFIK, you must have a size= argument


I have been using tmpfs since it was first implemented without any size limit and I never had any problem.


----------



## sk8harddiefast (Oct 25, 2010)

I can create a folder named tmpfs into /tmp and add this line on fstab?

```
tmpfs /tmp/tmpfs tmpfs rw 0 0
```
Remove 
	
	



```
tmpfs_load="YES"
```
 from loader.conf , reboot and that's it?


----------



## fronclynne (Oct 25, 2010)

Beastie said:
			
		

> I have been using tmpfs since it was first implemented without any size limit and I never had any problem.



Ah, I see, yes, thank you for that hint.  The size= argument must be in bytes or it is ignored.  With ~15G of swap and 4G RAM my machine uses a 17G tmpfs by default.  I'll have to fiddle with the inodes= argument to see what that does.


----------



## sk8harddiefast (Oct 25, 2010)

> With ~15G of swap and 4G RAM my machine uses a 17G tmpfs by default


That means that with my 4 giga swap and 2 giga ram, I have 5 giga tmpfs?


----------



## Beastie (Oct 25, 2010)

sk8harddiefast said:
			
		

> I can create a folder named tmpfs into /tmp and add this line on fstab?
> 
> ```
> tmpfs /tmp/tmpfs tmpfs rw 0 0
> ...


Yes. Or simply `# mount /tmp/tmpfs`.


----------



## sk8harddiefast (Oct 25, 2010)

Thanks both of you  I rebooting.. Now


----------



## fronclynne (Oct 25, 2010)

sk8harddiefast said:
			
		

> I can create a folder named tmpfs into /tmp and add this line on fstab?
> 
> ```
> tmpfs /tmp/tmpfs tmpfs rw 0 0
> ...



Yes, but do let me know how large of a filesystem it allocates without a size argument.

Ah, from /usr/src/sys/fs/tmpfs/tmpfs.h

```
Therefore, the amount of memory to be
 * used is either the limit imposed by the user during mount time or the
 * amount of available memory, whichever is lower.  To avoid consuming all
 * the memory for a given mount point, the system will always reserve a
 * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account
 * by this macro (see above).
```
So there's that.


----------



## Beastie (Oct 25, 2010)

fronclynne said:
			
		

> how large of a filesystem it allocates without a size argument.


Free RAM + free swap space AFAIK.


----------



## sk8harddiefast (Oct 25, 2010)

> Yes, but do let me know how large of a filesystem it allocates without a size argument.


How to find this? My tmpfs is running now


----------



## Beastie (Oct 25, 2010)

`% df -h /tmp/tmpfs`


----------



## sk8harddiefast (Oct 25, 2010)

Returns me that:

```
Filesystem    Size    Used   Avail Capacity  Mounted on
tmpfs         5.4G    4.0K    5.4G     0%    /tmp/tmpfs
```


----------



## Beastie (Oct 25, 2010)

How much free RAM and swap space do you have?
`% top`


----------



## sk8harddiefast (Oct 25, 2010)

Top output:

```
last pid: 27060;  load averages:  0.33,  0.34,  0.26                                                                          up 0+00:12:37  04:01:41
89 processes:  1 running, 88 sleeping
CPU:  2.0% user,  0.0% nice,  2.1% system,  0.2% interrupt, 95.8% idle
Mem: 107M Active, 145M Inact, 195M Wired, 2956K Cache, 180M Buf, 1513M Free
Swap: 3972M Total, 3972M Free

  PID USERNAME     THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
 1779 emberdaemon    1  47    0   859M 26920K select  2   0:39  4.20% Xorg
 1786 emberdaemon    1  45    0 32508K  5540K select  0   0:02  0.78% conky
26800 emberdaemon    1  45    0 80016K 15484K select  3   0:00  0.68% urxvt
 1823 emberdaemon    2  44    0   738M 58536K kqread  2   0:01  0.10% chrome
 1813 emberdaemon   20  44    0   201M 84276K ucond   1   0:17  0.00% chrome
 1822 emberdaemon    2  44    0   744M 64452K kqread  1   0:09  0.00% chrome
 1787 emberdaemon    2  44    0 24176K  4796K select  1   0:02  0.00% ipager
 1479 root           1  44    0  6896K  1304K select  3   0:01  0.00% powerd
 1824 emberdaemon    2  44    0   743M 62616K kqread  0   0:01  0.00% chrome
  845 root           1  44    0  8044K  1396K select  0   0:01  0.00% moused
 1781 emberdaemon    1  44    0 38216K  7700K select  1   0:00  0.00% fluxbox
 1692 haldaemon      2  51    0 27540K  5540K piperd  1   0:00  0.00% hald
 1875 emberdaemon    1  44    0 80016K 15468K select  0   0:00  0.00% urxvt
 1815 emberdaemon    1  44    0   141M 26376K sbwait  0   0:00  0.00% chrome
 1814 emberdaemon    1  44    0   139M 22284K select  1   0:00  0.00% chrome
 1877 emberdaemon    1  64    0 10220K  2944K ttyin   2   0:00  0.00% bash
27060 emberdaemon    1  44    0  9336K  2472K CPU0    1   0:00  0.00% top
 1696 root           1  76    0 17944K  3052K select  2   0:00  0.00% hald-runner
 1600 root           1  44    0 12096K  3640K select  3   0:00  0.00% sendmail
  990 root           1  44    0  3204K   740K select  1   0:00  0.00% devd
 1680 root           1  45    0 21676K  2056K wait    0   0:00  0.00% login
 1540 messagebus     1  44    0  8092K  2184K select  3   0:00  0.00% dbus-daemon
  886 root           6  45  r1F 17680K  3852K cuse-s  0   0:00  0.00% webcamd
 1247 root           1  44    0  7024K  1588K select  1   0:00  0.00% syslogd
 1695 root          17  44    0 28536K  5156K waitvt  2   0:00  0.00% console-kit-daemon
 1758 emberdaemon    1  44    0 10220K  2744K wait    0   0:00  0.00% bash
 1739 root           1  76    0 16624K  2692K kqread  0   0:00  0.00% hald-addon-mouse-sy
 1722 root           1  76    0 16624K  2692K kqread  1   0:00  0.00% hald-addon-mouse-sy
 1611 root           1  44    0  7952K  1628K nanslp  2   0:00  0.00% cron
26821 emberdaemon    1  45    0 10220K  2944K wait    0   0:00  0.00% bash
 1760 emberdaemon    1  76    0  8264K  1836K wait    2   0:00  0.00% sh
 1876 emberdaemon    1  46    0 72020K  8600K sbwait  3   0:00  0.00% urxvt
 1778 emberdaemon    1  52    0 13340K  2096K wait    3   0:00  0.00% xinit
26809 emberdaemon    1  45    0 72020K  8600K sbwait  3   0:00  0.00% urxvt
 1682 root           1  76    0  6892K  1300K ttyin   3   0:00  0.00% getty
 1687 root           1  76    0  6892K  1300K ttyin   2   0:00  0.00% getty
 1684 root           1  76    0  6892K  1300K ttyin   1   0:00  0.00% getty
 1685 root           1  76    0  6892K  1300K ttyin   3   0:00  0.00% getty
 1686 root           1  76    0  6892K  1300K ttyin   1   0:00  0.00% getty
 1683 root           1  76    0  6892K  1300K ttyin   0   0:00  0.00% getty
 1681 root           1  76    0  6892K  1300K ttyin   2   0:00  0.00% getty
 1604 smmsp          1  76    0 12096K  3864K pause   2   0:00  0.00% sendmail
  126 root           1  76    0  2744K  1068K pause   0   0:00  0.00% adjkerntz
[...]
```


----------



## Beastie (Oct 25, 2010)

1513M + 3972M = 5485M = 5.36G ~= 5.4G available from df's output. Q.E.D.


----------



## sk8harddiefast (Oct 25, 2010)

Tmpfs merge swap and ram on a specific mountpoint or is my idea?


----------



## Beastie (Oct 25, 2010)

It does not really "merge" them. They are already one as far as the OS is concerned. tmpfs is a memory filesystem and your (virtual) memory is made of physical memory plus swap space.


----------



## fronclynne (Oct 25, 2010)

sk8harddiefast said:
			
		

> Tmpfs merge swap and ram on a specific mountpoint or is my idea?



Sort of.  I'd assume it's also somewhat like the virtual mountpoints in zfs that draw from a common pool of storage.  So the sum of the used space in all of your tmpfs mounts can't exceed your available swap+RAM.  If that makes any sense.


----------



## sk8harddiefast (Oct 25, 2010)

Ok. Something I understood. If you suppose that yesterday I have no idea what tmpfs is, is a start


----------



## fronclynne (Oct 25, 2010)

Here we go, a small test: 
	
	



```
> df -h /home/tmp/tmpfs*
Filesystem         Size    Used   Avail Capacity  Mounted on
tmpfs               17G    4.0K     17G     0%    /home/tmp/tmpfs0
tmpfs               17G    4.0K     17G     0%    /home/tmp/tmpfs1
tmpfs               17G    4.0K     17G     0%    /home/tmp/tmpfs2
tmpfs               17G    4.0K     17G     0%    /home/tmp/tmpfs3
. . . copy some junk into them . . .
> df -h /home/tmp/tmpfs*
Filesystem         Size    Used   Avail Capacity  Mounted on
tmpfs               17G    698M     16G     4%    /home/tmp/tmpfs0
tmpfs               16G    4.0K     16G     0%    /home/tmp/tmpfs1
tmpfs               16G    4.0K     16G     0%    /home/tmp/tmpfs2
tmpfs               16G    4.0K     16G     0%    /home/tmp/tmpfs3
. . . more junk . . .
> df -h /home/tmp/tmpfs*
Filesystem         Size    Used   Avail Capacity  Mounted on
tmpfs               15G    698M     15G     4%    /home/tmp/tmpfs0
tmpfs               15G    4.0K     15G     0%    /home/tmp/tmpfs1
tmpfs               16G    1.1G     15G     7%    /home/tmp/tmpfs2
tmpfs               15G    579M     15G     4%    /home/tmp/tmpfs3
```
  So, it just dynamically reallocates as stuff fills up.  It just does it kinda weirdly from the perspective of df(1).


----------



## sk8harddiefast (Oct 25, 2010)

If I open to see a video on youtube. The video normally been saved on /tmp and is not deleted after reboot. Now will be saved on /tmp/tmpfs and will be deleted after reboot? A youtube video saved on /tmp is a kind of junk. right? So tmpfs is "collecting" all this junks and delete them on reboot. Like Ram do?


----------



## fronclynne (Oct 25, 2010)

*for zsh, cat /dev/urandom >> .zshrc*



			
				sk8harddiefast said:
			
		

> If I open to see a video on youtube. The video normally been saved on /tmp and is not deleted after reboot. Now will be saved on /tmp/tmpfs and will be deleted after reboot? A youtube video saved on /tmp is a kind of junk. right?



If you want things to use /tmp/tmpfs/ for temporary storage, set TMPDIR in your shell startup.  For [t]csh [cmd=""]echo "setenv TMPDIR /tmp/tmpfs" >> .cshrc[/cmd]; for bash [cmd=""]echo "export TMPDIR=/tmp/tmpfs" >> .profile[/cmd].


----------



## sk8harddiefast (Oct 25, 2010)

Ok. Done  Is dangerous to delete now all files on /tmp? Are useless. Right?


----------



## jem (Oct 25, 2010)

There may be some confusion between tmpfs(5) and md(4)-based /tmp, set up by the _tmpmfs_ and _tmpsize_ directives in rc.conf.

tmpmfs is the original method of creating a fixed size memory disk, then mounting it as /tmp.

tmpfs is the newer, experimental virtual-memory based method.

The _only_ thing required to set up tmpfs is a line in fstab (as the kernel module will be loaded automatically).


```
tmpfs   /tmp    tmpfs   rw,mode=01777   0       0
```

You don't need to specify a filesystem size for tmpfs; it simply uses as much virtual memory as required.


----------



## Beastie (Oct 25, 2010)

sk8harddiefast said:
			
		

> Is dangerous to delete now all files on /tmp? Are useless. Right?


That would remove *tmpfs*' mountpoint (/tmp/tmpfs) thus breaking the startup next time your reboot.
Maybe you are misunderstanding something so let it be clear: *tmpfs* is mounted on the /tmp/tmpfs directory not on the /tmp directory. The disk-based filesystem mounted on /tmp contains many files that are unrelated to *tmpfs* (mostly sockets and FIFO pipes) besides your *tmpfs* mountpoint.
So you can remove all the files in /tmp but keep /tmp/tmpfs.

Or if you want the whole /tmp to be cleared every time your reboot the machine, then do as in jem's post above and mount *tmpfs* directly on /tmp. Do not forget to remove the UFS /tmp line.


----------



## sk8harddiefast (Oct 25, 2010)

I was meaning all except /tmp/tmpfs. I mounted tmpfs on /tmp and "df -h /tmp" now is:

```
Filesystem    Size    Used   Avail Capacity  Mounted on
tmpfs         5.4G    8.3M    5.4G     0%    /tmp
```
The only problem is: 

```
sudo echo "export TMPDIR=/tmp" >> .profile
bash: .profile: Permission denied
```


----------



## fronclynne (Oct 25, 2010)

sk8harddiefast said:
			
		

> I was meaning all except /tmp/tmpfs. I mounted tmpfs on /tmp and "df -h /tmp" now is:
> 
> ```
> Filesystem    Size    Used   Avail Capacity  Mounted on
> ...



Reset the owner/group/permissions on your ~/.profile and don't use sudo when editing your user files?

You could also put the TMPDIR statement in your global /etc/profile, if you're so inclined.


----------



## sk8harddiefast (Oct 25, 2010)

Sorry. I had run *[*echo "export TMPDIR=/tmp" >> .profile*]* on / so I guess that understood root's .profile. When I went to /home/user and execute the same command then done  Solved


----------



## nORKy (Oct 26, 2010)

Why not mount tmpfs to /tmp?
Why /tmp/xxx?


----------



## sk8harddiefast (Oct 26, 2010)

I changed it to /tmp.


----------



## DutchDaemon (Oct 26, 2010)

This bears repeating, and it will be printed in boldface when booting:

```
[B]WARNING: TMPFS is considered to be a highly experimental feature in FreeBSD.[/B]
```
Use with caution.


----------



## sk8harddiefast (Oct 26, 2010)

I saw it when I boot first time. Looks to run fine for me  If I see any problem, just I will remove the line from fstab. For now conky tells me that tmpfs is growing  I made a reboot to see if data will be deleted and deleted. Seems to work.


----------



## DutchDaemon (Oct 26, 2010)

If you like to have /tmp in RAM/swap, you can always fall back to trusted tmpmfs (run  grep ^tmp /etc/defaults/rc.conf for example). It's what I always used before playing with tmpfs.


----------



## sk8harddiefast (Oct 27, 2010)

I set tmpmfs="YES" on rc.conf and change on fstab this: 
	
	



```
tmpfs	/tmp    tmpfs	rw,mode=01777	  0     0
```
 to this:

```
tmpmfs	/tmp    tmpmfs	  rw,mode=01777	     0     0
```


----------



## Beastie (Oct 27, 2010)

sk8harddiefast said:
			
		

> For now conky tells me that tmpfs is growing  I made a reboot to see if data will be deleted and deleted. Seems to work.


No need to reboot. You can remove files (e.g. cache) manually at any time.


----------



## DutchDaemon (Oct 27, 2010)

@sk8harddiefast, when you use 'tmpmfs', you don't need anything for /tmp in fstab. The automatically created md device (usually /dev/md0) will be used, and it will show up in mount.

I used this in /etc/rc.conf for a 1G /tmp with tmpmfs:


```
tmpmfs="YES"
tmpsize="1024m"
tmpmfs_flags="-m 0 -o async,noatime -S -p 1777"
```


----------



## DutchDaemon (Oct 27, 2010)

Before people get confused: if you read this thread, be sure to distinguish between *tmpmfs* ("old and trusted") and *tmpfs* ("new and experimental"). They perform the same function, but they are very different.


----------



## sk8harddiefast (Oct 27, 2010)

Ok. I will use tmpmfs. I trust you.


----------



## aragon (Oct 27, 2010)

Does anyone know what to avoid (or test) with tmpfs that might make it blow up?


----------



## sk8harddiefast (Oct 27, 2010)

I don't know but both of them seems to work perfect


----------



## Beastie (Oct 27, 2010)

aragon said:
			
		

> Does anyone know what to avoid (or test) with tmpfs that might make it blow up?


Yes, nothing. As I said earlier, I have been using it extensively* since it was first introduced and never had any problem whatsoever.

* Every caching (browser, thumbnail, etc.) directory in my home directory is symlinked to it. I also store small temporary files there.


----------



## nORKy (Oct 28, 2010)

What is different between tmpmfs and tmpfs?


----------



## sk8harddiefast (Oct 28, 2010)

As I understood Tmpfs is a type of "fylesystem". Is mounted as partition on boot. The Tmpmfs is just an option to have all /tmp in Ram/Swap. Tmpfs do the same but Tmpmfs is option and not "filesystem" ? Something like that? Not sure. I am learning all this from the procedure.


----------



## Beastie (Oct 28, 2010)

sk8harddiefast said:
			
		

> As I understood Tmpfs is a type of "fylesystem". Is mounted as partition on boot. The Tmpmfs is just an option to have all /tmp in Ram/Swap. Tmpfs do the same but Tmpmfs is option and not "filesystem"


Well, obviously, both are filesystems since they both can hold and organize files.

As you can see in /etc/rc.d/tmp, a "tmpmfs" is nothing more than an md(4) memory disk mounted on /tmp.

As for the difference, I think there is not much. From what I read here and there, *tmpfs* _may_ be faster and more efficient than *md*.


----------



## fronclynne (Oct 28, 2010)

nORKy said:
			
		

> What is different between tmpmfs and tmpfs?



tmpmfs (configured from /etc/rc.conf) uses mdmfs(8) under /tmp/, essentially doing the same as if you issued `# mdconfig -a -t swap -u0 && newfs -U /dev/md0 && mount /dev/md0 /tmp`

tmpfs(5) is, AFIK a self contained filesystem & memory disk driver, supposedly designed to be a memory disk.  Maybe one of the boffins can clue us in, but I'm not aware that UFS is particularly optimized for use on md(4).  I suppose if someone were feeling testy they could run some benchmarks to compare.  I'm happy to just have a gin&tonic and call myself a naughty boy (later).


----------

