# Can't write to a block device with chroot connection

**URL:** <https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193>\
**Category:** Get Help\
**Tags:** ubuntu\
**Created:** [April 29, 2024, 8:52am UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193 "2024-04-29T08:52:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![asad26](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/asad26/32/3020_2.png) [@asad26](https://forum.ansible.com/u/asad26)\
**Post date:** [April 29, 2024, 8:52am UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/1 "2024-04-29T08:52:48Z")

</div>

Hi

I am unable to write to a block device `/dev/sdb3` mounted as `/media/ajaved/PIROOT` using Ansible `chroot` connection. I am using Ubuntu Jammy (22.04) as host OS and my Ansible version is `2.10.8`.

Mount point:

```auto
/dev/sdb3 on /media/ajaved/PIROOT type ext4 (rw,nosuid,nodev,relatime,errors=remount-ro,uhelper=udisks2)

```

Please see the error below after running Ansible playbook:

```auto
pikvm3 unreachable | msg: Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in "/tmp", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p "` echo /tmp/.ansible-${USER} `"&& mkdir "` echo /tmp/.ansible-${USER}/ansible-tmp-1714377996.3163822-9271-11225315534968 `" && echo ansible-tmp-1714377996.3163822-9271-11225315534968="` echo /tmp/.ansible-${USER}/ansible-tmp-1714377996.3163822-9271-11225315534968 `" ), exited with result 126

```

We have set `remote_tmp` to `/tmp/.ansible-${USER}` in `ansible.cfg` file. My SUDO\_USER `ajaved` is also in `disk` group.  
Any help is much appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 29, 2024, 12:27pm UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/2 "2024-04-29T12:27:28Z")

</div>

Sorry if this is a stupid question but you are aware that the chroot connection only works locally and that if you are working on a remote server you have to run Ansible on the remote server?

---

<div class="post-metadata">

**Author:** ![asad26](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/asad26/32/3020_2.png) [@asad26](https://forum.ansible.com/u/asad26)\
**Post date:** [April 29, 2024, 2:43pm UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/3 "2024-04-29T14:43:22Z")

</div>

Thanks @chris

I am not working on a remote server. The block device (SD memory card) is attached and mounted locally to my computer where I run Ansible with chroot connection.  
Does this answer your question?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 29, 2024, 2:49pm UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/4 "2024-04-29T14:49:54Z")

</div>

Thanks for clarifying that, does the `/media/ajaved/PIROOT/tmp` directory exist?

What does `ls -lah /media/ajaved/PIROOT` return?

Does it work if you run the playbook as `root`? I’ve just checked how I use the chroot plugin – on remote servers – and I always run Ansible as root and don’t have `remote_tmp` set.

---

<div class="post-metadata">

**Author:** ![asad26](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/asad26/32/3020_2.png) [@asad26](https://forum.ansible.com/u/asad26)\
**Post date:** [April 29, 2024, 3:22pm UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/5 "2024-04-29T15:22:55Z")

</div>

Thanks for your reply @chris.

Yes, the `/media/ajaved/PIROOT/tmp` directory exists and it is empty at the moment.

Here is the `ls` output.

```auto
ls -lah /media/ajaved/PIROOT
total 80K
drwxr-xr-x 17 root root 4.0K Apr 17 18:38 .
drwxr-x---+ 6 root root 4.0K Apr 29 18:16 ..
lrwxrwxrwx 1 root root 7 Apr 17 18:38 bin -> usr/bin
drwxr-xr-x 2 root root 4.0K Apr 17 18:38 boot
drwxr-xr-x 2 root root 4.0K Mar 1 2023 dev
drwxr-xr-x 54 root root 4.0K Apr 17 18:38 etc
drwxr-xr-x 3 root root 4.0K Apr 17 18:36 home
lrwxrwxrwx 1 root root 7 Apr 17 18:38 lib -> usr/lib
drwx------ 2 root root 16K Apr 17 18:38 lost+found
drwxr-xr-x 2 root root 4.0K Feb 5 2023 mnt
drwxr-xr-x 2 root root 4.0K Apr 17 18:25 opt
dr-xr-xr-x 2 root root 4.0K Mar 1 2023 proc
drwxr-x--- 5 root root 4.0K Apr 17 18:36 root
drwxr-xr-x 2 root root 4.0K Mar 1 2023 run
lrwxrwxrwx 1 root root 7 Apr 17 18:38 sbin -> usr/bin
drwxr-xr-x 4 root root 4.0K Mar 1 2023 srv
dr-xr-xr-x 2 root root 4.0K Mar 1 2023 sys
drwxrwxrwt 2 root root 4.0K Apr 17 18:36 tmp
drwxr-xr-x 8 root root 4.0K Apr 17 18:36 usr
drwxr-xr-x 12 root root 4.0K Apr 17 18:25 var

```

I am running Ansible playbook with `sudo`, e.g.,

```auto
sudo ansible-playbook --limit pikvm3 flash.yml

```

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [April 29, 2024, 5:12pm UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/6 "2024-04-29T17:12:18Z")

</div>

Sorry I don’t know why you have a error creating the `TMPDIR`, do you still have an error if you don’t set `remote_tmp` in `ansible.cfg`?

Also how have you defined the chroot in the inventory? I use a YAML inventory file, `hosts.yml`:

```yaml
all:
  children:
    chroots:
      hosts:
        /chroot:
          ansible_connection: community.general.chroot

```

And in `ansible.cfg`:

```ini
[defaults]
inventory = hosts.yml

```

---

<div class="post-metadata">

**Author:** ![asad26](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/asad26/32/3020_2.png) [@asad26](https://forum.ansible.com/u/asad26)\
**Post date:** [April 30, 2024, 7:10am UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/7 "2024-04-30T07:10:18Z")

</div>

After some debugging, I managed to fix the issue. It was due to QEMU user emulation packages missing in my host OS. So, I basically installed `qemu-user` and `qemu-user-static` through APT package manager and Ansible was then able to create `TMPDIR` using chroot.

Anyways, thanks @chris for all the support.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 30, 2024, 7:10am UTC](https://forum.ansible.com/t/cant-write-to-a-block-device-with-chroot-connection/5193/8 "2024-05-30T07:10:24Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
