# Applying overlay changes ownership of root partition

**URL:** <https://hub.mender.io/t/applying-overlay-changes-ownership-of-root-partition/4762>\
**Category:** General Discussions\
**Tags:** mender-convert, overlay\
**Created:** [March 18, 2022, 6:50am UTC](https://hub.mender.io/t/applying-overlay-changes-ownership-of-root-partition/4762 "2022-03-18T06:50:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sensslen](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/sensslen/32/245_2.png) [@sensslen](https://hub.mender.io/u/sensslen)\
**Post date:** [March 18, 2022, 6:50am UTC](https://hub.mender.io/t/applying-overlay-changes-ownership-of-root-partition/4762/1 "2022-03-18T06:50:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![yakirm-cr](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/yakirm-cr/32/2047_2.png) [@yakirm-cr](https://hub.mender.io/u/yakirm-cr)\
**Post date:** [October 31, 2023, 7:42am UTC](https://hub.mender.io/t/applying-overlay-changes-ownership-of-root-partition/4762/2 "2023-10-31T07:42:28Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![dmitrijsan](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/dmitrijsan/32/1942_2.png) [@dmitrijsan](https://hub.mender.io/u/dmitrijsan)\
**Post date:** [November 7, 2023, 12:00pm UTC](https://hub.mender.io/t/applying-overlay-changes-ownership-of-root-partition/4762/3 "2023-11-07T12:00:41Z")

</div>

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.

```auto
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.
