I create a root file system for a Linux embedded system on my Linux PC. Its architecture is not 386. (Microbalze). It should be in 2 versions: as an image file (rootfs.img) with the Ext4 file system, this file will then be written to flash on the target embedded system, and as a directory accessible over the network via NFS.
Question: how to assign all root files of this root file system to the owner of root, without getting root privileges on my PC on which I collect it?
In the book "Linux from scratch" it is proposed to do this with the help of chroot, i.e. create mini-rootfs in a separate folder with all the necessary programs (bash, gcc, etc.), chroot into it and then work in it with superuser privileges. But this option has obvious drawbacks:
1. I already have all the programs installed, why install them for the second time?
2. The rootfs.img file belongs to me, but in order to edit it I must for some reason get root privileges.
3. In the end, it is possible that the user is not allowed to get root rights on his PC.
Thanks to everyone who answered.

  • I added my answer by adding brief information about guestfish and virt-make-fs . - aleksandr barakin

2 answers 2

Question: how to assign all root files of this root file system to the owner of root, without getting root privileges on my PC on which I collect it?

updated answer

In an already assembled image, you can “manage” using, for example, the guestfish program from the libguestfs-tools package . To speed up the work, it is required that the user be part of the kvm group (this is in debian gnu / linux , in other distributions it may be called differently).

An example with a file containing an образ file system (the > symbol at the beginning of the line marks the commands that are executed "inside" the pseudo-shell that is started by the guestfish program):

 $ guestfish -a образ > run 

look at the list of file systems - there is only one there, the one that is inside the file with the image that is specified with the -a option):

 > list-filesystems /dev/sda: ext2 

mount this file system as root:

 > mount /dev/sda / 

You can view the files (here, for example, there is only one файл in the file system):

 > ls / lost+found файл 

You can change the membership with the chown integrated command (both the user and the group must be specified in digital form):

 > chown 0 0 /файл 

as I understood, recursively this command, alas, does not work. so if you need to change the ownership of a large number of files, you will have to automate the process. man guestfish has examples to help with this automation.

You can end the pseudo-shell session either with the exit , or by pressing ctrl + d

about creating an image with virt-make-fs

By the way, in the already mentioned libguestfs-tools package there is a program virt-make-fs , with which you can create an image with the file system from the archive:

 $ virt-make-fs архив.tar образ 

Well, you can archive files with the necessary accessory, as usual, using fakeroot . for example, being in the directory that is the “root” of the file system being created:

 $ fakeroot bash $ chown -R пользователь:группа * $ tar -cf архив.tar * $ exit 

if the user and the group are root, then the call to chown may not be necessary, because “inside” the environment created by the fakeroot program, the current user looks like “ root ”, and the files / directories belonging to him look like “like root” ". see output $ ls -l

the old answer for genext2fs

but it is possible to use, for example, the program genext2fs ( ext2 filesystem generator for embedded systems ) from the package of the same name for the very assembly.

example (everything is performed on behalf of the ordinary user):

  1. create the “root” directory for our file system, and in it the file:

     $ mkdir корень; touch корень/файл 
  2. create an image with ext2 file system of 1024 blocks in size, with user substitution ( -U option):

     $ genext2fs -U -b 1024 -d корень образ 

check. here already (for mounting) you will need superuser rights:

 $ mkdir точка.монтирования $ sudo mount образ точка.монтирования 

and we see that the created file belongs to the root user :

 $ ls -l точка.монтирования total 52 drwx------ 2 root root 52224 Sep 28 18:26 lost+found -rw-r--r-- 1 root root 0 Sep 28 18:11 файл 

do not forget to unmount:

 $ sudo umount точка.монтирования 

ps in principle, and for mounting it is possible, as a last resort, to do without additional rights, using the fuse-ext2 program from the fuseext2 package :

 $ fuse-ext2 образ точка.монтирования 

the program will display many lines with information, from which in this case only notification that the file system is mounted in read-only mode is useful.

do not forget to unmount:

 $ fusermount -u точка.монтирования 

useful information:

  • Thanks for the answer. What systems for creating package builds for Linux programs are always better suited for cross-platform building? For example, Arhlinux PKGBUILD. - Reffum
  • @Reffum, I think, "better always fit" those that you know best. - aleksandr barakin

The answer will be "no way". All solutions that can be devised, either hacks in order to get the same root rights for a minute, or simply through root rights.

Why such strange restrictions? Why can't you spend 10 seconds on your PC root? Why does this file belong to the root?

  • The rootfs.img image file itself belongs to me, and I don’t want to be owned by root, I want root to be the owner of files in the file system created in this image. - Reffum
  • 3
    @Reffum, so change the owner: $ sudo chown владелец файл - aleksandr barakin
  • @Smithson, as it turned out, the answer "no way" is not quite correct. see my answer, in the guestfish part. - aleksandr barakin
  • I did not understand the question, I did not understand that it was necessary to change the owner within the image. - Smithson