0

Is it, in some way, possible to create AND fill a ramdisk image with files without mounting the ramdisk?

I have a customized set of files for linux (so, just a bunch of files) which should be inside the filesystem. Now I want to have it executed during build which generates a ramdisk, formatting it with mkfs (needs no root) and then copy the files in some way into the ramdisk (but the way must not need root rights!) Is there a way to do so?

alabamajack
  • 557
  • 5
  • 18

2 Answers2

1

It's called a loopback device.

Package to install: libguestfs-tools.

And it needs the Linux kernel image file (/boot/vmlinuz-*-generic) be readable by the user.

# Create mountpoint.
mkdir space
# Create image.
dd if=/dev/zero of=space.img bs=1024 count=255
# Format image.
/sbin/mkfs.ext4 space.img

# Mount image.
#
# There is a bug with it at least on Ubuntu:
# "The kernel is no longer readable by non-root users"
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
#
# Fix: $ dpkg-statoverride --add --update root root 0644 /boot/vmlinuz-$(uname -r)
# Alternative fix is to chown the kernel image manually: chown a+r /boot/vmlinuz-*-generic
#
# Does uid/gid mapping, uses space.img as image, /dev/sda of the quest as partition, space/ as the mount point.
guestmount -o uid=$(id -u) -o gid=$(id -g) -o default_permissions -a space.img -m /dev/sda space

# do things

# Unmount.
guestunmount space
Velkan
  • 5,899
  • 5
  • 33
  • 69
0

mount is used for sharing a file system for user usage. That 'sharing' ( connection between file/thing containing the filesystem and user accessible directory and file structure) is done by a module in kernel (program running inside the kernel). That 'program' translates user request (ex. create file) to operations on the filesystem.
What we need is a filesystem program which runs in userspace and does not call mount. Userspace filesystems implementations are called fuse and i know there are fuse implementation of popular filesystems. Anyway, i am not aware of any popular program, that will for example create a file in a filesystem on a file without 'mounting' it to a directory. You may browse examples on wiki, maybe you will find such program.
I would go with writing such program myself.
You may write your own filesystem or choose to use existing one and write a program to modify the content of a file (or ramdisk image, on linux it's a file) and then use this program to create and modify the content of this filesystem, without executing linux mount call.
For example you may use fat_fs library, used in many embedded systems, implement fat_fs callbacks functions ex. disc_read() disc_write() to read and write from your file/ramdisc, and then call fat_fs functions to modify the content of the ramdisc/file. Such embedded libraries shouldn't do any calls to mount or any linux syscalls, as these calls are missing on embedded systems.
PS. On my university there are such classes, that students must write a simple filesystem program that opens a file and supports files creation, modification and deletion in the filesystem in this file.

KamilCuk
  • 69,546
  • 5
  • 27
  • 60