I have built a home-grown Linux distribution, and I can make the complete disk image file as a non-root user with one exception -- installing the boot loader. I'm using syslinux (actually extlinux), and to install it I have to loop-back mount the boot partition, which requires root/sudo privileges. The commands are run from a makefile, and the variable names should clearly indicate what to replace them with.

sudo umount $(LOOP_MOUNT_POINT)
sudo losetup -d $(LOOP_DEVICE)

Is there a way to write syslinux or extlinux to the disk image file without requiring root privileges?

  • Are you wondering if it is possible to do all those commands without root? Or just the extlinux command? – BenjiWiebe May 20 '14 at 19:37
  • @BenjiWiebe If there is another way, then whatever commands are required, but as I understand it you need a mounted file system in order to run extlinux, and then extlinux needs to run with root privileges when done this way. – Patrick May 20 '14 at 19:41

This is possible for syslinux:

syslinux ~/floppy.ima

The syslinux installer contains enough magic to be run on an unmounted filesystem. (In fact, it is designed to do that.) The extlinux installer expects to be run on a mounted filesystem, though.

It is almost certainly possible to split off the extlinux installer into a part that copies the files (something like mtools for FAT, which is rare but appears to exist, although one could just integrate them directly with genext2fs), and a part that installs the bootsector (I might be able to cobble this together).

I did something like this for GRUB 2, which installs into the space between the MBR and the first partition, for Grml; this was actually easier to do because GRUB, unlike SYSLINUX, does not require as much from the bootsector. It basically depends on your broader requirements. If the above part about SYSLINUX does not help you, contact me, so we can work something out.

| improve this answer | |
  • Thank you for this info. I am using genext2fs, so your suggestion sounds like the best option. Is it simply a matter of dd'ing the extlinux code into the MBR and copying the files to the fs, or do the files need to go in certain blocks within the fs? – Patrick May 21 '14 at 14:33
  • I've not yet looked at EXTLINUX in detail (will do so and then delete this comment, but give me a bit of time) but for SYSLINUX, LILO, GRUB and other bootloaders, it's usually so that the code is copied into the PBR (partition boot record, vulgo bootsector, not MBR), and then the code is edited to have the position of the file hardcoded in it (hence the need for an installer). I'll cobble something together for you. – mirabilos May 21 '14 at 18:01
  • @Patrick I have some need for discussion on this, I opened a chat – mirabilos May 24 '14 at 15:55
  • Can you point me to your implementation this using grub? I currently have grub-install, but it still requires the boot partition to be mounted. – copy Jan 4 '18 at 22:01
  • @copy this is how it’s used in OpenADK, which has even more flexible image generation than Grml (which only needs to create an ISO 9660 filesystem). Perhaps that helps? If not, contact me directly. - Edit: oh wait, no, core.img generation… lemme see… check here for the grub-mkimage call. This is highly dependent on the target environment (basically, which modules are needed to load the others dynamically; GRUB 1 was much easier). – mirabilos Jan 5 '18 at 17:25

If you add a line in /etc/fstab saying something like:

/dev/loop0      /mnt                                    ext4    defaults,user   0 0

you can then mount/unmount /dev/loop0 as a regular user.

And if you do chown youruser:youruser <MOUNTPOINT> <LOOPDEVICE> then extlinux , losetup, mkfs, etc can be done as youruser.

| improve this answer | |
  • I want this to be part of a generic build process, and the location of the image file and mount point can vary from user to user. I would prefer not to require editing fstab for each user. – Patrick May 21 '14 at 14:35
  • @Patrick Editing fstab is global. – BenjiWiebe May 21 '14 at 16:15
  • But losetup remains root-only, no? – bmargulies Oct 16 '15 at 23:29
  • @bmargulies Not when the loop device has been chown'd or chmod'ed so that the user has access to it... or, on Fedora at least, you can just add the user to the disk group. (Or create a loop group and use that...) – BenjiWiebe Oct 17 '15 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.