Skip to contentSkip to conversation

Howto take a snapshot from an image as ordinary user?

DISCUSSION Ferry Toth 2 participants 3 messages Mar 7, 2020 – Mar 15, 2020 Btrfs file system

lore
Share & tools
lore

Ferry Toth

author
#0 lore raw

I am generating a btrfs system image using Yocto.

I want to take a snapshot from the image preferably inside a Yocto recipe. The snapshot I can send 'over the air' to the remote (IoT) device.

Now I am able to loop mount the image file using udisksctl as an ordinary user, which mounts the image under /media/ferry.

However, the owner of the mount point is root, and it appears I am not allowed to take a snapshot as an ordinary user.

I think it is obvious that I don't want to run bitbake as root.

So what is the recommended way to generate a snapshot without becoming root?

Thanks,

Ferry

#1 lore raw

Hi

Op 07-03-2020 om 22:21 schreef Ferry Toth:

Quoted 7 lines

I am generating a btrfs system image using Yocto.

I want to take a snapshot from the image preferably inside a Yocto recipe. The snapshot I can send 'over the air' to the remote (IoT) device.

Now I am able to loop mount the image file using udisksctl as an ordinary user, which mounts the image under /media/ferry.

However, the owner of the mount point is root, and it appears I am not allowed to take a snapshot as an ordinary user.

To clarify, I know that I can chown the mount point after mounting. But to do that I need to sudo it.

Quoted 9 lines

I think it is obvious that I don't want to run bitbake as root.

So what is the recommended way to generate a snapshot without becoming root?

Thanks,

Ferry

author
#2 lore raw

Op 08-03-2020 om 16:39 schreef Ferry Toth:

Quoted 5 lines

Hi

Op 07-03-2020 om 22:21 schreef Ferry Toth:

Quoted 2 lines

I am generating a btrfs system image using Yocto.

Quoted 8 lines
Quoted 5 lines

I want to take a snapshot from the image preferably inside a Yocto recipe. The snapshot I can send 'over the air' to the remote (IoT) device.

Now I am able to loop mount the image file using udisksctl as an ordinary user, which mounts the image under /media/ferry.

However, the owner of the mount point is root, and it appears I am not allowed to take a snapshot as an ordinary user.

To clarify, I know that I can chown the mount point after mounting. But to do that I need to sudo it.

I found a way to own the mount point. The procedure is:
1) create an ext34 file system with
EXTRA_IMAGECMD_ext3 += " -E root_owner=/bin/ps -o user= -p $$:/bin/ps -o group= -p $$"

Yocto runs the image generation under pseudo (alternative fakeroot). The above allows to create an ext3 file system with the root owned by the real current user.

2) btrfs-convert the ext3 image to btrfs
3) take the snapshot

The final step (btrfs send) fails with:
ERROR: check if we support uuid tree fails - Operation not permitted
ERROR: failed to initialize subvol search: Operation not permitted

It appears it just can't be done currently as I could have from:
https://webcache.googleusercontent.com/search?q=cache:oZR58gPBVRkJ:https://www.spinics.net/lists/linux-btrfs/msg31188.html+&cd=1&hl=en&ct=clnk&gl=nl&client=ubuntu

HOSTTOOLS += " /usr/bin/udisksctl /bin/btrfs /bin/btrfs-convert /bin/ps"

# we are running as pseudo but we need ownership of the root as the real user
EXTRA_IMAGECMD_ext3 += " -E root_owner=`/bin/ps -o user= -p $$`:`/bin/ps -o group= -p $$`"
IMAGE_FSTYPES += " ext3"

python do_btrfs_snapshot() {
import subprocess

# convert the ext3 image
cmd_mkimg = 'btrfs-convert ' +
d.expand("${DEPLOY_DIR_IMAGE}/${IMAGE_BASENAME}-${MACHINE}.ext3")
ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True,
text=True)

    # mount the converted ext3 image
    cmd_mkimg = '/usr/bin/udisksctl loop-setup --no-user-interaction -f 
 ' + d.expand("${DEPLOY_DIR_IMAGE}/${IMAGE_BASENAME}-${MACHINE}.ext3")
    ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True, 
text=True)
    loop = ret.stdout.split(' ').pop().split('.')[0]
    # find the mounted partition
    cmd_mkimg = 'lsblk -o MOUNTPOINT -n ' + loop
    ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True, 
text=True)
    mount_point = ret.stdout.rstrip()
    # take a snapshot of the partition
    cmd_mkimg = '/bin/btrfs su snap -r ' + mount_point + ' ' + 
mount_point + '/@new'
    print(cmd_mkimg)
    ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True, 
text=True)
    # btrfs send the partition
    cmd_mkimg = '/bin/btrfs send ' + mount_point + '/@new/ > ' + 
d.expand("${DEPLOY_DIR_IMAGE}/${IMAGE_BASENAME}-${MACHINE}.snapshot")
    ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True, 
text=True)
    print(ret.stdout)
    print(ret.stderr)
    # unmount the btrfs image
    cmd_mkimg = '/usr/bin/udisksctl unmount -b ' + loop
    ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True, 
text=True)
    # delete the loop device
    cmd_mkimg = '/usr/bin/udisksctl loop-delete -b ' + loop
    ret = subprocess.run(cmd_mkimg, shell=True, capture_output=True, 
text=True)

return 0
}

addtask btrfs_snapshot after image_ext3 before do_bootimg

Quoted 11 lines
Quoted 9 lines

I think it is obvious that I don't want to run bitbake as root.

So what is the recommended way to generate a snapshot without becoming root?

Thanks,

Ferry