Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Raspberry Pi 4B SD-Card Install Image #93824

Merged
merged 1 commit into from Aug 8, 2020
Merged

Conversation

blitz
Copy link
Contributor

@blitz blitz commented Jul 25, 2020

In 9ac1ab1 / #82718 make-ext4-fs was refactored to use mkfs.ext4 instead of cptofs. This introduced a problem:

  • If populateImageCommands would create no files (which is fine), a cp
    invocation would fail due to missing source arguments.

Note that mkfs.ext4 relies on fakeroot to generate sane uids/gids and this currently fails when using the aarch64-linux binfmt emulation on x86.

I've used the following command for testing:

% nix-build nixos -I nixos-config=nixos/modules/installer/cd-dvd/sd-image-raspberrypi4.nix -A config.system.build.sdImage --argstr system aarch64-linux
Motivation for this change
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@blitz
Copy link
Contributor Author

blitz commented Jul 25, 2020

This fix should also make this Hydra job happy again: https://hydra.nixos.org/job/nixos/trunk-combined/nixos.sd_image_raspberrypi4.aarch64-linux/

@blitz
Copy link
Contributor Author

blitz commented Jul 25, 2020

I've verified that the resulting SD-Card image boots on my Raspberry Pi 4. 🍾

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 25, 2020

For me sudo didn't work any more. Could this possibly be because of this change?
I think some shared library wasn't owned by the correct user (root).
Edit: Can you confirm this behavior?

@misuzu
Copy link
Contributor

misuzu commented Jul 25, 2020

For me sudo didn't work any more. Could this possibly be because of this change?
I think some shared library wasn't owned by the correct user (root).
Edit: Can you confirm this behavior?

fakeroot call is required if If you're using Single User nix.

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 25, 2020

I'm cross-compiling on NixOS. What do you mean with single-user nix?

Edit: I will retry - will take some time as I also garbage collected before to be safe.

@misuzu
Copy link
Contributor

misuzu commented Jul 25, 2020

I'm cross-compiling on NixOS. What do you mean with single-user nix?

I had this issue when using https://nixos.org/nix/manual/#sect-single-user-installation on Ubuntu, that's why there is a fakeroot call.

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 25, 2020

As far as I understand https://git.qemu.org/?p=qemu.git;a=commit;h=d8c08b1e6c7b1a5be1ec70e339437823a41b1946 a new version of qemu might fix that and allow us to use fakeroot again? Also I consistently get an image file with invalid permissions inside (the ones from my user) with this pull request.

@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

As far as I understand https://git.qemu.org/?p=qemu.git;a=commit;h=d8c08b1e6c7b1a5be1ec70e339437823a41b1946 a new version of qemu might fix that and allow us to use fakeroot again? Also I consistently get an image file with invalid permissions inside (the ones from my user) with this pull request.

@mohe2015 I also see subtle issues with building this from a multi-user install that I didn't notice before:

[julian@nixos:~]$ sudo ls
sudo: error in /etc/sudo.conf, line 0 while loading plugin "sudoers_policy"
sudo: /nix/store/dga8f95c24vziiir8xalmrfay4f3jxhg-sudo-1.8.31p1/libexec/sudo/sudoers.so must be owned by uid 0
sudo: fatal error, unable to load plugins

[julian@nixos:~]$ ls -lh /nix/store/dga8f95c24vziiir8xalmrfay4f3jxhg-sudo-1.8.31p1/libexec/sudo/sudoers.so
-r--r--r-- 1 julian users 393K Jan  1  1970 /nix/store/dga8f95c24vziiir8xalmrfay4f3jxhg-sudo-1.8.31p1/libexec/sudo/sudoers.so

Mmh. I think this can be fixed by using mkfs.ext4 options:

                   root_owner[=uid:gid]
                          Specify the numeric user and group ID of the root directory.  If no UID:GID is  specified,
                          use  the user and group ID of the user running mke2fs.

Will try this later. Converting this PR to a draft until then to prevent people from merging it. :)

@blitz blitz marked this pull request as draft July 26, 2020 09:29
@mohe2015
Copy link
Contributor

mohe2015 commented Jul 26, 2020

I couldn't get it to work with the root_owner option. I think it's result is the following:

[moritz@nixos:~/nixpkgs]$ ls -l /run/media/moritz/
total 8
drwxr-xr-x 3 moritz users 4096 Jan  1  1970 NIXOS_BOOT
drwxr-xr-x 4 root   root  4096 Jan  1  1970 NIXOS_SD

[moritz@nixos:~/nixpkgs]$ ls -l /run/media/moritz/NIXOS_SD/
total 236
drwx------ 2 root   root   16384 Jan  1  1970 lost+found
drwxr-xr-x 3 moritz users   4096 Jan  1  1970 nix
-r--r--r-- 1 moritz users 217404 Jan  1  1970 nix-path-registration

So the root directory is owned by the specified user but not the files in it. Maybe I did something wrong so you should still try it out.

Edit: Updating qemu to 5.1.0-rc1 and using fakeroot did work but I don't know if that change would currently be accepted (as it is not yet a stable release; see https://wiki.qemu.org/Planning/5.1 for their current plans)

@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

@mohe2015 You are right, the root_owner option is not good enough to do what we want. What does confuse me is how the store paths in the image actually have my user's UID, even through they are built by a nixbld user.

@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

@mohe2015 I'm not sure how we ever end up with sane uid/gids using mkfs.ext4:

In the build sandbox the nixbld user is 1000:100, but outside of the sandbox this is usually the uid/gid of the first user in the system. Hence we get the broken owners in the image.

Ideally, mkfs.ext4 would support a uid mapping configuration, but unfortunately it doesn't. Manually doing chown -R root:root before packing it into the file system also doesn't work, because it ends up with lots of invalid argument errors.

I'm open to ideas...

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 26, 2020

For me it worked by building with qemu 5.1.0-rc1 and using fakeroot. At least sudo worked and I couldn't find something nonworking. But there still were some strange file permissions in there.

@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

For me it worked by building with qemu 5.1.0-rc1 and normally using fakeroot. At least sudo worked and I couldn't find something nonworking.

Ah, because fakeroot makes stat etc return uid 0 for files owned by the current user (nixbld) these end up being owned by root in the generated image. It feels a bit accidental. :)

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 26, 2020

Yeah, I think the problem is that running chown root:root without root permissions wouldn't work so you use fakeroot to fool mkfs.ext4 into thinking its a file owned by root so it can build the correct file system. But honestly I don't know how nix works and if I'm right.

@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

I think the problem is that running chown root:root without root permissions wouldn't work so you use fakeroot to fool mkfs.ext4 into thinking its a file owned by root so it can build the correct file system.

Yes, that's also my current understanding. Not an ideal situation.

@blitz blitz force-pushed the fix-rpi4-installer branch 2 times, most recently from c36cfc1 to 13bb122 Compare July 26, 2020 16:57
@blitz blitz marked this pull request as ready for review July 26, 2020 16:57
@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

I've added the fakeroot call again. This doesn't make the cross-compilation setup painless, but should work for native builds of the raspberry pi 4 install image at least.

@mohe2015
Copy link
Contributor

mohe2015 commented Jul 26, 2020

I think you need to also add back fakeroot in two other places.

And also do you know more whether qemu 5.1.0-rc1 would be accepted in nixpkgs unstable? I assume I will have to wait for the release until it gets incoporated here but I don't know.

@blitz
Copy link
Contributor Author

blitz commented Jul 26, 2020

I think you need to also add back fakeroot in two other places.

You were too fast, I fixed it right after @GrahamcOfBorg complained. :)

And also do you know more whether qemu 5.1.0-rc1 would be accepted in nixpkgs unstable? I assume I will have to wait for the release until it gets incoporated here but I don't know.

The binfmt emulation is pretty nice and having it fail in weird ways definitely sucks. No idea whether this counts as good reason to include a RC into unstable.

Btw, with the current version of this PR and qemu 5.1-rc1 on the build host, I can build a SD Card image that works. Finally!

In 9ac1ab1 this library function was
refactored to use mkfs.ext4 instead of cptofs. There are two problems:

If populateImageCommands would create no files (which is fine), a cp
invocation would fail due to missing source arguments.

Another problem is that mkfs.ext4 relies on fakeroot to have sane
uid/gids in the generated filesystem image. This currently doesn't
work for cross compiling.
@blitz blitz changed the title nixos/lib/make-ext4-fs: fix after mkfs.ext4 refactoring Fix Raspberry Pi 4B SD-Card Install Image Jul 28, 2020
@purcell
Copy link
Member

purcell commented Aug 1, 2020

Thanks for this fix -- I ran into this breakage locally and this worked like a charm.

@tfc
Copy link
Contributor

tfc commented Aug 3, 2020

As the pr became quite uncontroversial now, i would merge this in the next days if no one objects.

@purcell purcell mentioned this pull request Aug 3, 2020
@Robertof
Copy link

Robertof commented Aug 5, 2020

FWIW, the Debian maintainers decided to backport the QEMU fix @mohe2015 mentioned in their stable QEMU 5.0 package (original issue). Perhaps we can reuse the same patch and apply it on top of the current QEMU 5 package in nixpkgs?

https://sources.debian.org/patches/qemu/1:5.0-14/linux-user-refactor-ipc-syscall-and-support-of-semtimedop.patch/

@aaronjanse
Copy link
Member

In the meantime, is this ready to merge @tfc? I just got a Raspberry Pi 4B in the mail, and I'm hyped 👀

@tfc tfc merged commit 2a288cb into NixOS:master Aug 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants