fakeroot

From Noah.org
Revision as of 16:31, 28 September 2010 by Root (Talk | contribs) (ownership does not persist outside of fakeroot)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


The Debian `fakeroot` tool is useful for building filesystem trees and packages where files must have ownership or 'root' or other privileged users. With `fakeroot` you can do this without `sudo` even if you have limited user privileges.

# fakeroot /bin/bash
# id -un
root
# id -u
0
# mkdir my_package
# cd my_package
# mkdir -p var/lib/mysql
# chown mysql:mysql var/lib/mysql
# ls -lAR
.:
total 4
drwxr-xr-x 3 root root 4096 2010-09-28T16:02:26-07 var

./var:
total 4
drwxr-xr-x 3 root root 4096 2010-09-28T16:02:26-07 lib

./var/lib:
total 4
drwxr-xr-x 2 mysql mysql 4096 2010-09-28T16:02:26-07 mysql

./var/lib/mysql:
total 0
# cd ..
# tar -cz -f my_package.tar.gz my_package
# exit
$ id -un
noah
$ tar -tz -v -f my_package.tar.gz 
drwxr-xr-x root/root         0 2010-09-28 16:02 my_package/
drwxr-xr-x root/root         0 2010-09-28 16:02 my_package/var/
drwxr-xr-x root/root         0 2010-09-28 16:02 my_package/var/lib/
drwxr-xr-x mysql/mysql       0 2010-09-28 16:02 my_package/var/lib/mysql/

ownership environment does not persist outside of fakeroot

Notice that `fakeroot` lets you mix different users in the same session. In reality all files that you created are owned by you, but while you are in a session `fakeroot` will keep track of the different fake UIDs and GIDs that are used. The ownership maps are lost when you exit the fakeroot session. Notice the `ls -lAR` command in the previous example. Files appear to be owned by root or mysql. Run the same `ls -lAR` command outside of the fakeroot session. Everything has one owner now.

$ ls -lAR
.:
total 4
drwxr-xr-x 3 noah noah 4096 2010-09-28T16:02:26-07 var

./var:
total 4
drwxr-xr-x 3 noah noah 4096 2010-09-28T16:02:26-07 lib

./var/lib:
total 4
drwxr-xr-x 2 noah noah 4096 2010-09-28T16:02:26-07 mysql

./var/lib/mysql:
total 0

Unfortunately, if you start a new fakeroot session the ownerships you had previously set will not be restored. All files will appear to be owned by root. You can preserve the environment between fakeroot sessions by using the -s and -i options to save and restore the fake ownership environment.