Write to Ext2 Image without root user

Programming, for all ages and all languages.
Post Reply
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Write to Ext2 Image without root user

Post 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?
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Write to Ext2 Image without root user

Post by Solar »

You can add the configuration to /etc/fstab, adding the "users" option so that any user could mount / unmount the image.
Every good solution is obvious once you've found it.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Write to Ext2 Image without root user

Post 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.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: Write to Ext2 Image without root user

Post 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...
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Write to Ext2 Image without root user

Post 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.
Every good solution is obvious once you've found it.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Write to Ext2 Image without root user

Post 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"
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Write to Ext2 Image without root user

Post by Solar »

Guys, step down before you hurt yourselves. You don't poke around in UDEV rules in order to avoid one single sudo.
Every good solution is obvious once you've found it.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Write to Ext2 Image without root user

Post 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.
linguofreak
Member
Member
Posts: 510
Joined: Wed Mar 09, 2011 3:55 am

Re: Write to Ext2 Image without root user

Post 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.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Write to Ext2 Image without root user

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
Post Reply