Manipulating users/groups in helper class with fakeroot/pseudo


Sean
 

I wrote a class, call it MYCLASS, whose purpose is to migrate certain files installed by standard recipes into a different directory and create symlinks to those files.

The reason for this is to have a disk partition layout with a read-only, replaceable rootfs partition and a persistent storage partition. The idea is to move certain configuration files installed by a recipe into persistent storage and replace the rootfs version with a symlink.

The user inherits this class in a .bbappend file in another layer and specifies the list of files that should have this treatment inside a MYCLASS_CONFFILES variable.

I implemented this logic in a Python function and added it as a task as follows. I abstracted away some details and used "example.conf" as a hardcoded example configuration file being moved to a partition mounted at "/persistent".

fakeroot python myclass_copy_and_symlink_conffiles() {
...
    sysconfdir_absfile = os.path.join(d.getVar('D'), d.getVar('sysconfdir'), 'example.conf')
    destdir_absfile = os.path.join(d.getVar('D', 'persistent/example.conf')

    st = os.stat(sysconfdir_absfile)
    shutil.copy(sysconfdir_absfile, destdir_absfile)
    shutil.chown(destdir_absfile, user=st.st_uid, group=st.st_gid)

    # omitted: replace original file with symlink to copied file

...

}

myclass_copy_and_symlink_conffiles[depends] += "virtual/fakeroot-native:do_populate_sysroot"

do_install[postfuncs] += "${MYCLASSINSTALLFUNCS}"

MYCLASSINSTALLFUNCS_class-target = "myclass_copy_and_symlink_conffiles"
MYCLASSINSTALLFUNCS = ""

 

The problem I'm having is that UID and GID don't seem to be available to the shutil.chown function or they're not read correctly from the pseudo database when os.stat is called. Using the function as shown above, MYCLASS doesn't preserve the UID/GID of the original file (as known to fakeroot/pseudo) and falls back to setting UID:GID=root:root on the copied file instead.

Any hints on how I can improve this approach? Is the problem that the system calls made by shutil/os functions aren't handled by pseudo? Should I replace this Python function with a shell function that can use fakeroot?