Applying overlay changes ownership of root partition

I recently needed to add an overlay to my build. I do prepare the overlay as normal user. Unfortunately applying the overlay does change ownership of the root partition, as the overlay is applied using rsync. As a quick fix I change ownership of the overlay folder before applying it. In the long run I’d appreciate if there was a solution that could just apply an overlay without messing with files and folders that are already there…

Any suggestions are very welcome!

I was reading up about the --inplace option of rsync which might be a possible solution. What do you think?

I know it’s an old one…
Nevertheless, there is a way to do it via the OVERLAY_MODIFY_HOOKS (they are executed after overlays are applied):

  1. Ceate a custom configuration file under the configs directory- e.g. configs/custom_config
    Add the following content to the file:
    function update_ownership() {
        log_info "Changing ownership of path/to/custom/overlay/dir"
        run_and_log_cmd "chown -R root:root work/rootfs/path/to/custom/overlay/dir"
    }
    
    OVERLAY_MODIFY_HOOKS+=(update_ownership)
    
  2. Add --config configs/custom_config in the call to docker-mender-convert/mender-convert.

Even though its an old post, it is still applicable for current mender-convert version.

@yakirm-cr I like your approach, however, I find it a bit risky to recursively change ownership. Therefore, I have found a better approach - since we already know the files and directories to be overlaid, we can use that to only change ownership of overlaid files and directories.

update_ownership() {
  log_info "Updating overlaid file and directory ownerships and permissions after overlaying it to a destination file system"

  # Define source and destination directories
  src_dir="input/overlays/rootfs"
  dest_dir="work/rootfs"

  # Loop over the files and directories in the source directory
  find "$src_dir" -type d -o -type f | while read src_path; do
    # Construct the corresponding path in the destination directory
    dest_path="${src_path/#$src_dir/$dest_dir}"

    # Check if the file or directory exists in the destination directory
    if [ -e "$dest_path" ]; then
      # Change the owner
      run_and_log_cmd "chown root:root '$dest_path'"
    fi
  done
}

OVERLAY_MODIFY_HOOKS+=(update_ownership)

LE: Mender-convert overlay is application dependant which makes it impossible to find one-solution-fits-all. However, the script above might be a good baseline.

1 Like