2016-09-15 07:37:06 -04:00
---
layout: doc
2016-09-15 08:11:56 -04:00
title: How to make any file in a TemplateBasedVM persistent using bind-dirs
2016-09-15 07:37:06 -04:00
permalink: /doc/bind-dirs/
redirect_from:
- /en/doc/bind-dirs/
---
2016-09-15 16:14:38 -04:00
# How to make any file in a TemplateBasedVM persistent using bind-dirs #
2017-05-11 20:12:02 -04:00
## What are bind-dirs? ##
2016-09-15 07:37:06 -04:00
2017-03-20 19:33:24 -04:00
With [bind-dirs ](https://github.com/QubesOS/qubes-core-agent-linux/blob/master/vm-systemd/bind-dirs.sh )
any arbitrary files or folders can be made persistent in TemplateBasedVMs.
2016-09-15 07:37:06 -04:00
2016-09-15 16:14:38 -04:00
## What is it useful for? ##
2016-09-15 07:37:06 -04:00
2018-02-15 05:35:57 -05:00
In a TemplateBasedVM all of the file system comes from the template except `/home` , `/usr/local` , and `/rw` .
2017-03-20 19:33:24 -04:00
This means that changes in the rest of the filesystem are lost when the TemplateBasedVM is shutdown.
bind-dirs provides a mechanism whereby files usually taken from the template can be persisted across reboots.
2019-06-06 12:35:08 -04:00
For example, in Whonix, [Tor's data dir /var/lib/tor has been made persistent in the TemplateBased ProxyVM sys-whonix][whonix]
2017-03-20 19:33:24 -04:00
In this way sys-whonix can benefit from the Tor anonymity feature 'persistent Tor entry guards' but does not have to be a StandaloneVM.
2016-09-15 07:37:06 -04:00
2016-09-15 16:14:38 -04:00
## How to use bind-dirs.sh? ##
2016-09-15 07:37:06 -04:00
2019-06-06 12:35:08 -04:00
In this example, we want to make /var/lib/tor persistent.
Inside the TemplateBasedVM.
2017-03-03 11:11:45 -05:00
2016-12-20 18:19:33 -05:00
1. Make sure folder `/rw/config/qubes-bind-dirs.d` exists.
2016-09-15 07:37:06 -04:00
2016-12-20 18:19:33 -05:00
sudo mkdir -p /rw/config/qubes-bind-dirs.d
2019-06-06 12:35:08 -04:00
2. Create a file `/rw/config/qubes-bind-dirs.d/50_user.conf` with root rights.
2017-03-03 11:11:45 -05:00
2019-06-06 12:35:08 -04:00
3. Edit the file 50_user.conf to append a folder or file name to the `binds` variable.
2017-03-03 11:11:45 -05:00
binds+=( '/var/lib/tor' )
2016-12-20 18:19:33 -05:00
4. Save.
2016-09-15 07:37:06 -04:00
2017-03-20 19:33:24 -04:00
5. Reboot the TemplateBasedVM.
2016-09-15 07:37:06 -04:00
2016-12-20 18:19:33 -05:00
6. Done.
2016-09-15 07:37:06 -04:00
2019-06-06 12:35:08 -04:00
From now on any files within the/var/lib/tor folder will persist across reboots.
2019-06-10 20:52:26 -04:00
You can make make many files or folders persist, simply by making multiple entries in the `50_user.conf` file, each on a separate line.
2019-06-06 12:35:08 -04:00
For example, if you added the file `/etc/tor/torrc` to the `binds` variable, any modifications to *that* file will persist across reboots.
binds+=( '/var/lib/tor' )
binds+=( '/etc/tor/torrc' )
2017-03-03 11:11:45 -05:00
2016-09-15 16:14:38 -04:00
## Other Configuration Folders ##
2016-09-15 07:37:06 -04:00
* `/usr/lib/qubes-bind-dirs.d` (lowest priority, for packages)
* `/etc/qubes-bind-dirs.d` (intermediate priority, for template wide configuration)
* `/rw/config/qubes-bind-dirs.d` (highest priority, for per VM configuration)
2017-03-20 19:33:24 -04:00
## How does it work? ##
2019-06-06 12:35:08 -04:00
bind-dirs.sh is called at startup of a TemplateBasedVM, and configuration files in the above configuration folders are parsed to build a bash array.
2017-03-20 19:33:24 -04:00
Files or folders identified in the array are copied to /rw/bind-dirs if they do not already exist there, and are then bind mounted over the original files/folders.
2019-06-06 12:35:08 -04:00
Creation of the files and folders in /rw/bind-dirs should be automatic the first time the TemplateBasedVM is restarted after configuration.
2019-06-10 20:52:26 -04:00
If you want to circumvent this process, you can create the relevant file structure under /rw/bind-dirs and make any changes at the same time that you perform the configuration, before reboot.
2019-06-06 12:35:08 -04:00
Note that you must create the full folder structure under /rw/bind-dirs - e.g you would have to create /rw/bind-dirs/var/lib/tor
2017-03-20 19:33:24 -04:00
2016-09-15 16:14:38 -04:00
## Limitations ##
2016-09-15 07:37:06 -04:00
2017-03-20 19:33:24 -04:00
* Files that exist in the TemplateVM root image cannot be deleted in the TemplateBasedVMs root image using bind-dirs.sh.
2019-06-06 12:35:08 -04:00
* Re-running `sudo /usr/lib/qubes/init/bind-dirs.sh` without a previous `sudo /usr/lib/qubes/init/bind-dirs.sh umount` does not work.
* Running `sudo /usr/lib/qubes/init/bind-dirs.sh umount` after boot (before shutdown) is probably not sane and nothing can be done about that.
2017-03-20 19:33:24 -04:00
* Many editors create a temporary file and copy it over the original file. If you have bind mounted an individual file this will break the mount.
2019-02-09 23:11:20 -05:00
Any changes you make will not survive a reboot. If you think it likely you will want to edit a file, then either include the parent directory in bind-dirs rather than the file, or perform the file operation on the file in /rw/bind-dirs.
2019-06-06 12:35:08 -04:00
* Some files are altered when a qube boots - e.g. `/etc/hosts` .
If you try to use bind-dirs on such files you may break your qube in unpredictable ways.
You can add persistent rules to /etc/hosts using [/rw/config/rc.local][config-file]
2019-02-10 00:01:14 -05:00
2016-09-15 16:14:38 -04:00
## How to remove binds from bind-dirs.sh? ##
2016-09-15 07:37:06 -04:00
2019-06-06 12:35:08 -04:00
`binds` is actually just a bash variable (an array) and the bind-dirs.sh configuration folders are sourced as bash snippets in lexical order.
2017-03-20 19:33:24 -04:00
Therefore if you wanted to remove an existing entry from the `binds` array, you could do that by using a lexically higher configuration file.
2019-06-06 12:35:08 -04:00
For example, if you wanted to make `/var/lib/tor` non-persistent in `sys-whonix` without manually editing /usr/lib/qubes-bind-dirs.d/40_qubes-whonix.conf, you could use the following in:
2016-09-15 07:37:06 -04:00
`/rw/config/qubes-bind-dirs.d/50_user.conf`
2016-09-15 16:20:30 -04:00
~~~
2016-09-15 07:37:06 -04:00
binds=( "${binds[@]/'/var/lib/tor'}" )
2016-09-15 16:20:30 -04:00
~~~
2016-09-15 07:37:06 -04:00
2017-05-25 02:43:11 -04:00
(Editing `/usr/lib/qubes-bind-dirs.d/40_qubes-whonix.conf` directly is strongly discouraged, since such changes get lost when that file is changed in the package on upgrades.)
2016-09-15 07:37:06 -04:00
2016-09-15 16:14:38 -04:00
## Discussion ##
2016-09-15 07:37:06 -04:00
2016-09-15 16:21:04 -04:00
[TemplateBasedVMs: make selected files and folders located in the root image persistent- review bind-dirs.sh ](https://groups.google.com/forum/#!topic/qubes-devel/tcYQ4eV-XX4/discussion )
2016-09-15 07:37:06 -04:00
2019-06-06 12:35:08 -04:00
[config-file]: /doc/config-files
[whonix]: https://github.com/Whonix/qubes-whonix/blob/8438d13d75822e9ea800b9eb6024063f476636ff/usr/lib/qubes-bind-dirs.d/40_qubes-whonix.conf#L5