Page 1 of 1

Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 7:48 am
by AlfaOmega08
Hi, I need to copy the kernel executable to an Ext2 HD image everytime I run "make". This is easily done with the following commands:

Code: Select all

losetup /dev/loop0 image
mount /dev/loop0 tmp
cp kernel tmp/
umount tmp
losetup -d /dev/loop0
The problem is that both losetup and mount require root privileges. Is there any non-sudo way of doing this? I don't feel that confortable yet to write an ext2 write-enable driver for disk images.

Any suggestion?

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 8:45 am
by Solar
You can add the configuration to /etc/fstab, adding the "users" option so that any user could mount / unmount the image.

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 10:18 am
by linguofreak
Plus, if you chown the loop devices to root:loop, and add yourself to group loop, you can do losetup without root privileges.

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 11:07 am
by AlfaOmega08
Ok, the fstab thing works good, but the losetup still needs "sudo". Changing the group to loop0 didn't change anythings as it changes back to root:disk after a try. Even if I add myself to group disk, it still says "Permission denied". I'm on Ubuntu 11.10...

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 11:33 am
by Solar
Gosh, what do they teach the kids these days? :roll:

1) Generate and format image file.

Code: Select all

dd if=/dev/zero of=floppy.img bs=512 count=2880
sudo losetup /dev/loop0 floppy.img
sudo mkfs -t ext2 floppy.img
sudo losetup -d /dev/loop0
2) Add to /etc/fstab:

Code: Select all

/home/$USER/floppy.img    /mnt/wherever    ext2    loop,users    0    0
3) Use "mount /mnt/wherever" and "umount /mnt/wherever" at your leisure, no need for "losetup" anymore.

In case you're looking for a way around the initial "sudo losetup", that won't work. That both "mkfs" and "losetup" are located in /sbin should tell you something (like, you don't want them available without "sudo"). And since the image isn't formatted yet, fstab would balk at mounting it.

But you need that one only once, anyway.

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 11:53 am
by linguofreak
AlfaOmega08 wrote:Ok, the fstab thing works good, but the losetup still needs "sudo". Changing the group to loop0 didn't change anythings as it changes back to root:disk after a try. Even if I add myself to group disk, it still says "Permission denied". I'm on Ubuntu 11.10...
Oops. Yeah. It's not chown you need, it's a file in /etc/udev/rules.d .

On my system (Ubuntu 10.04), there's a file /etc/udev/rules.d/70-loopdev-group.rules, which contains the following:

Code: Select all

KERNEL=="loop[0-9]*", GROUP="loop"

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 12:01 pm
by Solar
Guys, step down before you hurt yourselves. You don't poke around in UDEV rules in order to avoid one single sudo.

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 12:09 pm
by linguofreak
Solar wrote:Gosh, what do they teach the kids these days? :roll:

1) Generate and format image file.

Code: Select all

dd if=/dev/zero of=floppy.img bs=512 count=2880
sudo losetup /dev/loop0 floppy.img
sudo mkfs -t ext2 floppy.img
sudo losetup -d /dev/loop0
2) Add to /etc/fstab:

Code: Select all

/home/$USER/floppy.img    /mnt/wherever    ext2    loop,users    0    0
It depends on your loop device usage patterns. It probably works well in AlfaOmega's case, where there's one image that's getting mounted alot, but in my case I have a fair number of images, with each individual one not being mounted very frequently (and quite a few being deleted after a few uses), and I have a feeling that my fstab would get cluttered with image files quite quickly. So I just have a few generic loop device entries in fstab, loop device ownership set to root:loop, and myself in loop.
3) Use "mount /mnt/wherever" and "umount /mnt/wherever" at your leisure, no need for "losetup" anymore.

In case you're looking for a way around the initial "sudo losetup", that won't work.
Well, it's possible (whether wise or not) to get around the "sudo" part of "sudo losetup" there. The initial losetup does have to be done, whether with sudo or not.

Re: Write to Ext2 Image without root user

Posted: Sat Jan 21, 2012 1:29 pm
by linguofreak
Solar wrote:Guys, step down before you hurt yourselves. You don't poke around in UDEV rules in order to avoid one single sudo.
In the OP's case you're probably right, and your solution is probably better. In my own case I feel that I'm avoiding more than a single sudo. If I break my system I give you permission to mock me mercilessly, but it hasn't happened yet.

Re: Write to Ext2 Image without root user

Posted: Sun Jan 22, 2012 3:52 am
by Kevin
I know that people have used debugfs to copy files to the image without mounting it. Should come very close to what you're looking for.