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

strict ordering of resize service #2522

Closed
wants to merge 7 commits into from
Closed

strict ordering of resize service #2522

wants to merge 7 commits into from

Conversation

jvonau
Copy link
Contributor

@jvonau jvonau commented Sep 15, 2020

Strict ordering to preform the resizing while mounted read only.

pi@box:~ $ sudo systemctl status fake-hwclock.service
● fake-hwclock.service - Restore / save the current clock
Loaded: loaded (/lib/systemd/system/fake-hwclock.service; enabled; vendor preset: enabled
Active: active (exited) since Tue 2020-09-15 10:07:37 BST; 15min ago
Docs: man:fake-hwclock(8)
Process: 124 ExecStart=/sbin/fake-hwclock load $FORCE (code=exited, status=0/SUCCESS)
Main PID: 124 (code=exited, status=0/SUCCESS)

124

pi@box:~ $ sudo systemctl status iiab-rpi-root-resize.service
● iiab-rpi-root-resize.service - Root Filesystem Auto-Resizer
Loaded: loaded (/etc/systemd/system/iiab-rpi-root-resize.service; enabled; vendor preset:
Active: active (exited) since Tue 2020-09-15 10:07:37 BST; 16min ago
Process: 136 ExecStart=/usr/sbin/iiab-rpi-max-rootfs.sh (code=exited, status=0/SUCCESS)
Main PID: 136 (code=exited, status=0/SUCCESS)
Sep 15 10:07:37 box.lan iiab-rpi-max-rootfs.sh[136]: Checking for .resize-rootfs
Sep 15 10:07:37 box.lan iiab-rpi-max-rootfs.sh[136]: + echo 'Checking for .resize-rootfs'
Sep 15 10:07:37 box.lan iiab-rpi-max-rootfs.sh[136]: + '[' -f /.resize-rootfs ']'
Sep 15 10:07:37 box.lan iiab-rpi-max-rootfs.sh[136]: + exit 0

136

pi@box:~ $ sudo systemctl status systemd-fsck-root.service
● systemd-fsck-root.service - File System Check on Root Device
Loaded: loaded (/lib/systemd/system/systemd-fsck-root.service; enabled-runtime; vendor pr
Active: active (exited) since Tue 2020-09-15 10:07:37 BST; 17min ago
Docs: man:systemd-fsck-root.service(8)
Process: 137 ExecStart=/lib/systemd/systemd-fsck (code=exited, status=0/SUCCESS)
Main PID: 137 (code=exited, status=0/SUCCESS)
Sep 15 10:07:37 box.lan systemd-fsck[137]: e2fsck 1.44.5 (15-Dec-2018)
Sep 15 10:07:37 box.lan systemd-fsck[137]: rootfs: clean, 691125/7638432 files, 16226570/311
Sep 15 10:07:37 box.lan systemd[1]: Started File System Check on Root Device.

137

pi@box:~ $ sudo systemctl status systemd-fsckd
● systemd-fsckd.service - File System Check Daemon to report status
Loaded: loaded (/lib/systemd/system/systemd-fsckd.service; static; vendor preset: enabled
Active: inactive (dead) since Tue 2020-09-15 10:08:10 BST; 17min ago
Docs: man:systemd-fsckd.service(8)
Process: 139 ExecStart=/lib/systemd/systemd-fsckd (code=exited, status=0/SUCCESS)
Main PID: 139 (code=exited, status=0/SUCCESS)
Sep 15 10:08:10 box.lan systemd[1]: systemd-fsckd.service: Succeeded.
lines 1-8/8 (END)

139

Could allow these two to run and resize before remounting read write, that will be the second commit

pi@box:~ $ sudo systemctl status systemd-remount-fs.service
● systemd-remount-fs.service - Remount Root and Kernel File Systems
Loaded: loaded (/lib/systemd/system/systemd-remount-fs.service; static; vendor preset: en
Active: active (exited) since Tue 2020-09-15 10:07:37 BST; 25min ago
Docs: man:systemd-remount-fs.service(8)
https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
Process: 143 ExecStart=/lib/systemd/systemd-remount-fs (code=exited, status=0/SUCCESS)
Main PID: 143 (code=exited, status=0/SUCCESS)
Sep 15 10:07:37 box.lan systemd[1]: Starting Remount Root and Kernel File Systems...
Sep 15 10:07:37 box.lan systemd[1]: Started Remount Root and Kernel File Systems.

143

@jvonau
Copy link
Contributor Author

jvonau commented Sep 15, 2020

pi@box:~ $ sudo systemctl status iiab-rpi-root-resize.service
● iiab-rpi-root-resize.service - Root Filesystem Auto-Resizer
Loaded: loaded (/etc/systemd/system/iiab-rpi-root-resize.service; enabled; vendor preset:
Active: active (exited) since Tue 2020-09-15 10:46:14 BST; 2min 12s ago
Process: 142 ExecStart=/usr/sbin/iiab-rpi-max-rootfs.sh (code=exited, status=0/SUCCESS)
Main PID: 142 (code=exited, status=0/SUCCESS)
Sep 15 10:46:14 box.lan systemd[1]: Starting Root Filesystem Auto-Resizer...
Sep 15 10:46:14 box.lan iiab-rpi-max-rootfs.sh[142]: + echo 'Checking for .resize-rootfs'
Sep 15 10:46:14 box.lan iiab-rpi-max-rootfs.sh[142]: + '[' -f /.resize-rootfs ']'
Sep 15 10:46:14 box.lan iiab-rpi-max-rootfs.sh[142]: + exit 0
Sep 15 10:46:14 box.lan iiab-rpi-max-rootfs.sh[142]: Checking for .resize-rootfs
Sep 15 10:46:14 box.lan systemd[1]: Started Root Filesystem Auto-Resizer.
pi@box:~ $ sudo systemctl status systemd-remount-fs.service
● systemd-remount-fs.service - Remount Root and Kernel File Systems
Loaded: loaded (/lib/systemd/system/systemd-remount-fs.service; static; vendor preset: en
Active: active (exited) since Tue 2020-09-15 10:46:14 BST; 2min 1s ago
Docs: man:systemd-remount-fs.service(8)
https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
Process: 143 ExecStart=/lib/systemd/systemd-remount-fs (code=exited, status=0/SUCCESS)
Main PID: 143 (code=exited, status=0/SUCCESS)
Sep 15 10:46:14 box.lan systemd[1]: Starting Remount Root and Kernel File Systems...
Sep 15 10:46:14 box.lan systemd[1]: Started Remount Root and Kernel File Systems.

The pid look ok, without a real test filesystem to enlarge the issue might be not blocking the next remount step while resizing. Wonder how cloud-guest-utils does the blocking.

@jvonau
Copy link
Contributor Author

jvonau commented Sep 15, 2020

@tim-moody comments?

@holta
Copy link
Member

holta commented Sep 15, 2020

This looks like a bug fix that if tested for #2517 should be merged shortly?

Thanks @jvonau & @tim-moody

@holta holta added the bug label Sep 15, 2020
@holta holta added this to the 7.2 milestone Sep 15, 2020
@holta holta changed the title strict ordering strict ordering [fix for #2517 boot failures on Zero W re: growpart ?] Sep 15, 2020
@tim-moody
Copy link
Contributor

I'm not sure what this aims to fix. Don't see how it can fix an upstream bug that prevents resizing from even taking place on a W. I have not seen a timeout when resizing actually runs, as on a 4. The amount of time it takes can be considerable and depends on the size of the card compared to the image, but the service seems just to launch resizing and then terminate.

For example if you ssh in at the first opportunity on first boot you can use df to watch the partition size grow. If this were to block, users would be locked out I think for up to a couple of minutes. Resize does not require the fs to be mounted read only.

I can see the benefit of fsck running before resize as the resized portion of the fs should not need checking, and there is a possible conflict. I'm not sure if there is any conflict with fake-hwclock.

To be clear I use images that require resizing frequently. Except on W I never see it fail, though I sometimes need to wait for it to finish. Even W used to succeed, though it required even more patience.

@tim-moody
Copy link
Contributor

@holta you have labelled this a bug. Please state what you think the bug is. The only bug in #2517 is upstream and not addressed by this PR.

@holta
Copy link
Member

holta commented Sep 15, 2020

The question is whether this PR is
helpful or not, for #2517 especially, or otherwise?

Please clarify opinions/recommendations.

(Even if this turns out not to be a bug fix for #2517, so be it.)

@holta holta added question and removed bug labels Sep 15, 2020
@holta holta changed the title strict ordering [fix for #2517 boot failures on Zero W re: growpart ?] strict ordering [fix for #2517 boot failures on Zero W re: growpart ? Partition fsck / resizing questions] Sep 15, 2020
@jvonau
Copy link
Contributor Author

jvonau commented Sep 15, 2020

I'm not sure what this aims to fix. Don't see how it can fix an upstream bug that prevents resizing from even taking place on a W. I have not seen a timeout when resizing actually runs, as on a 4. The amount of time it takes can be considerable and depends on the size of the card compared to the image, but the service seems just to launch resizing and then terminate.

RaspOS has a too old of a version to work correctly on the W, understood.

For example if you ssh in at the first opportunity on first boot you can use df to watch the partition size grow. If this were to block, users would be locked out I think for up to a couple of minutes. Resize does not require the fs to be mounted read only.

Yes I understand you can watch the progress, trouble is the whole machine is starting to write to the disk, with the boot paused while resizing is preformed the resizing should complete faster with no contention for CPU cycles or disk IO.

I can see the benefit of fsck running before resize as the resized portion of the fs should not need checking, and there is a possible conflict. I'm not sure if there is any conflict with fake-hwclock.

fake-hwclock was used as a reference for the starting order, based on the pids of the services started.

To be clear I use images that require resizing frequently. Except on W I never see it fail, though I sometimes need to wait for it to finish. Even W used to succeed, though it required even more patience.

Resizing is something that should be done at the equivalent to runlevel 1 same as where fsck is being run prior to letting the services access the filesystem for the resource contention issue to provide a nice clean slate to work with once the filesystem is remounted read write. See the potential speed benefits mentioned above. Note RaspOS does a reboot after resizing the initial filesystem for this reason, and Ubuntu blocks during resizing using the cloud-init package to get a clean filesystem into place.

@holta
Copy link
Member

holta commented Sep 15, 2020

Thanks everyone for the many above clarifications.

What OS / layouts are most needed to test this PR?

@jvonau
Copy link
Contributor Author

jvonau commented Sep 15, 2020

Testing this is a bit tricky, one would need a shrunken image with iiab-rpi-root-resize.service in place.
loop mount the image then chroot or after dd'ing mount then chroot into the filesystem
chroot "mountpoint" Replacing "mountpoint" with the real info
edit /etc/systemd/system/iiab-rpi-root-resize.service to match this PR
edit /usr/sbin/iiab-rpi-max-rootfs.sh to match this PR
rm /etc/systemd/system/multi-user.target.wants/iiab-rpi-root-resize.service
systemctl enable iiab-rpi-root-resize.service Creates the link /etc/systemd/system/sysinit.target.wants/iiab-rpi-root-resize.service pointing to /etc/systemd/system/iiab-rpi-root-resize.service.
exit the chroot and umount.
insert and boot.

@tim-moody
Copy link
Contributor

Testing this is a bit tricky

getting an image is trivial as there are several on archive.org including https://archive.org/details/iiab-PRE-7.2-200914-medical-content-ready-g01d418e.img which I just put there. Write it to an sd card and boot in rpi 4.

@tim-moody
Copy link
Contributor

the biggest point of contention is whether the file system needs to be locked during resizing. I believe it does not.

The speed varies greatly. A little while ago I tried the image above on a 32G card on rpi 4. It had resized before I could ssh in.

@tim-moody
Copy link
Contributor

If it can be demonstrated that this PR greatly increases the speed of resizing then I think it is worthwhile. But if not, users will be locked out and may power cycle the machine, which may prevent resizing from continuing.

Please use a 128G card to test as 32G may not take long enough.

@holta
Copy link
Member

holta commented Sep 15, 2020

Let's seek out methodical testers to try this out (often one or two show up, some weeks more than others!)

Thanks @jvonau & @tim-moody for the hard work making this possible.

@jvonau
Copy link
Contributor Author

jvonau commented Sep 15, 2020

If it can be demonstrated that this PR greatly increases the speed of resizing then I think it is worthwhile. But if not, users will be locked out and may power cycle the machine, which may prevent resizing from continuing.

Ubuntu's first boot would be an indication of how growpart is implemented, and what delay to expect. Do user's power cycle during first boot of Ubuntu? No, the delay is hidden behind cloud-init pretty display if you have a monitor hooked up otherwise you are waiting on ssh to become active.

Please use a 128G card to test as 32G may not take long enough.

The one that I have is busy atm... I'll try to pick another up, but if you want, write the image to a sdcard of any size and make a machine available on the vpn with the sdcard available in the usb adapter and I'll do the mods just to see if I'm barking up a wrong tree or not. You can watch it with your own eyes, would be a live test demo and that would be a data point to see if this works as I envision it or not. Let just leave the W's out of the equation, different issue. What do you think?

@tim-moody
Copy link
Contributor

if you want, write the image to a sdcard of any size and make a machine available on the vpn with the sdcard available in the usb adapter and I'll do the mods just to see if I'm barking up a wrong tree or not.

I can wait

@jvonau
Copy link
Contributor Author

jvonau commented Sep 16, 2020

How can setting up the base filesystem be considered a "good 1st issue" for somebody new to IIAB or linux in general? This skill set involved is way beyond that of a general user.

@holta
Copy link
Member

holta commented Sep 16, 2020

How can setting up the base filesystem be considered a "good 1st issue" for somebody new to IIAB or linux in general? This skill set involved is way beyond that of a general user.

True, but somebody new to IIAB (who is already browsing GitHub, reviewing this ticket, hi if that's you!) is rarely new to Linux.

e.g. if it happens to be a new IIAB volunteer with RPi/filesystem expertise, this could possibly be a "good 1st issue" for just such a person.

@tim-moody
Copy link
Contributor

I'm more interested in The After=systemd-remount-fs.service & Before=dphys-swapfile.service

me too. is there a target that is before sshd is turned on, if the objective is to prevent user login before resizing?
I take it the objective cannot be to resize before the fs is rw as that fails.

are you sure you are using growpart with the same safeguards as upstream uses

How could I be? There is code to lock the disk in growpart, though I have no idea if it works, but there is absolutely nothing in growpart that would prevent running it on a fully booted system. I have no idea what was in the mind of its author, but I would guess they might have tried to sense the proper conditions in which to run if they thought it necessary.

While I acknowledge all your points above, I have serious misgivings about the practicality of avoiding first boot when building anything but the most trivial images. I have built many images; most are 'content ready' but some include content. They are all tweaked in some way because of user requirements. So I need to be able to shrink an image so that it can then resize on next boot.

I wanted to use the RaspOS stock resizing, but after numerous attempts to shrink the rootfs, even when offline, in such a way that it would work I gave up and cobbled this together with help from others.

For me the bottom line remains that when testing on rpi I write my images that need to resize to an sdcard multiple times per day, mostly to rpi 4, and I never see them fail. Before I upload an image to archive.org, I always write it to an sdcard and boot it up to test it. Until the incident I reported that started this whole chain, I did not experience failures. If someone else has experienced a failure that this PR fixes, I'm all ears.

@jvonau
Copy link
Contributor Author

jvonau commented Sep 18, 2020

Required reading:

  1. https://cloudinit.readthedocs.io/en/latest/topics/boot.html#first-boot-determination
  2. https://cloudinit.readthedocs.io/en/latest/topics/examples.html?highlight=growpart
  3. https://cloudinit.readthedocs.io/en/latest/topics/modules.html?highlight=growpart#growpart
  4. https://git.launchpad.net/cloud-initramfs-tools/tree/growroot/dracut/modules.d/50growroot/growroot.sh

In a nutshell it appears cloud-init authors envision growpart being run by dracut in the initramfs while the disk is unmounted long before systemd gets a hold of the disk(4). On Ubuntu the automatic resizing can be re-enabled see the first-boot-determination(1).

On RaspOS an initramfs is not used at all and mounts the disk directly, this is why RaspOS's uses systemd to launch raspi-config to expand the partition using fdisk on first boot via do_expand_rootfs, then places /etc/init.d/resize2fs_once to expand the filesystem on the next boot.

Looks like the distros take great care to insure the root partition is expanded while not much else is running during the resizing operation.

@tim-moody
Copy link
Contributor

I have nothing further to add to this discussion.
@holta when @jvonau declares this PR ready to merge I will not object if you merge it.

@holta
Copy link
Member

holta commented Sep 25, 2020

@holta when @jvonau declares this PR ready to merge I will not object if you merge it.

If that's @jvonau's recommendation, I can merge it today.

With so many more OS versions now appearing on RPi (64-bit RaspiOS & Ubuntu) it's entirely possible this will need to evolve again in future?

Regardless: everyone should acknowledge a ton of thought has gone into this over the past 10 days.

@tim-moody tim-moody changed the title strict ordering [fix for #2517 boot failures on Zero W re: growpart ? Partition fsck / resizing questions] strict ordering of resize service Sep 25, 2020
@jvonau
Copy link
Contributor Author

jvonau commented Sep 26, 2020

Why merge this rather untested process when there are other PR's pending that have had wider testing is beyond my ability to understand what makes the cut for release.

@tim-moody
Copy link
Contributor

Just as another data point, I wrote the latest raspios to sdcard, booted, ssh in as pi, and noted the following:

pi@raspberrypi:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root       49527600 1210368  46273748   3% /
devtmpfs          823904       0    823904   0% /dev
tmpfs             956000       0    956000   0% /dev/shm
tmpfs             956000   24992    931008   3% /run
tmpfs               5120       4      5116   1% /run/lock
tmpfs             956000       0    956000   0% /sys/fs/cgroup
/dev/mmcblk0p1    258095   54603    203492  22% /boot
tmpfs             191200       0    191200   0% /run/user/1000
pi@raspberrypi:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root       55722620 1210316  52217168   3% /
devtmpfs          823904       0    823904   0% /dev
tmpfs             956000       0    956000   0% /dev/shm
tmpfs             956000   24992    931008   3% /run
tmpfs               5120       4      5116   1% /run/lock
tmpfs             956000       0    956000   0% /sys/fs/cgroup
/dev/mmcblk0p1    258095   54603    203492  22% /boot
tmpfs             191200       0    191200   0% /run/user/1000
pi@raspberrypi:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root       68112712 1270504  64043768   2% /
devtmpfs          823904       0    823904   0% /dev
tmpfs             956000       0    956000   0% /dev/shm
tmpfs             956000   24988    931012   3% /run
tmpfs               5120       4      5116   1% /run/lock
tmpfs             956000       0    956000   0% /sys/fs/cgroup
/dev/mmcblk0p1    258095   54603    203492  22% /boot
tmpfs             191200       0    191200   0% /run/user/1000
pi@raspberrypi:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root      120121180 1271020 113939056   2% /
devtmpfs          823904       0    823904   0% /dev
tmpfs             956000       0    956000   0% /dev/shm
tmpfs             956000   24980    931020   3% /run
tmpfs               5120       4      5116   1% /run/lock
tmpfs             956000       0    956000   0% /sys/fs/cgroup
/dev/mmcblk0p1    258095   54603    203492  22% /boot
tmpfs             191200       0    191200   0% /run/user/1000

@jvonau
Copy link
Contributor Author

jvonau commented Sep 26, 2020

Just a public service announcement that there are 2 distinct operations, partition resizing and filesystem resizing.

Just as another data point, I wrote the latest raspios to sdcard, booted, ssh in as pi, and noted the following:

To clarify, that is after the quick reboot that does the partition resizing, then you can watch the filesystem resizing.

@tim-moody
Copy link
Contributor

As part of that public service could you mention if it would be possible to write to the resized portion of a partition prior to resizing the file system.

@holta
Copy link
Member

holta commented Sep 27, 2020

All this info is a tremendous public service to the roughly 40 Million Raspberry Pi's out there.

If folks want this current PR merged into IIAB 7.2 RC2 (expected within about 24h) let me know.

@holta
Copy link
Member

holta commented Aug 1, 2022

@holta
Copy link
Member

holta commented Aug 7, 2022

  1. We have a working solution for Raspberry Pi OS (raspi-config --expand-rootfs and reboot) so I will revert April's PR 1-prep/templates/iiab-expand-rootfs: Avoid reboot #3160 allowing that automated solution to once again work reliably.

    [DONE, see: PR Revert "1-prep/templates/iiab-expand-rootfs: Avoid reboot" #3336]

  2. But let's also seek a solution for OS's other than RasPiOS.

  3. I am re-opening this thoughtful PR strict ordering of resize service #2522 for discussion. This should lead to solving rootfs can fail to fully expand: race condition betw iiab-expand-rootfs & systemd-fsck ? (journalctl confusingly/regularly portrays 2 simultaneous boots due to lack of RTC?) #3325 in a more comprehensive way, so everyone can put /.expand-rootfs in place to create images for their own school system (and not just on RasPiOS).

  4. Or, we can feel free to borrow the 30+ lines from raspi-config that do almost exactly the same thing — if this is a lower-maintenance approach: https://github.com/RPi-Distro/raspi-config/blob/10431dc73d05031788a2f6a8981cf8c4ef9d0f33/raspi-config#L213-L247

    Instead of re-inventing the wheel, let's be open-minded to code re-use — if in fact RasPiOS's solution is robust and sufficiently resilient across distros?

    (Quite simply, raspi-config puts resize2fs "$ROOT_PART" in /etc/init.d/resize2fs_once which then deletes itself on boot.)

See also @jvonau's 2022-04-02 summary:

A reboot is not that bad and is what RaspiOS does when booting a 'fresh' image out of the box, just a change in behavior when using the builtin tools. Using growpart is way more flexible, and is the standard on Ubuntu based arm64 images. Now the question becomes should growpart be installed on all installations[*] to take advantage of the revised root device detection that should work on all /dev/sd* root filesystems? [PR #3137] That would be a bonus if cloning an intel based machine could be achieved with the tools within iiab-factory.

[*] In fact growpart is installed as part of all IIAB's since 2022-03-15:

- cloud-guest-utils # 2022-04-02: For growpart command -- whereas RasPiOS's 'raspi-config --expand-rootfs' instead uses fdisk (requiring a reboot, see do_expand_rootfs() in https://github.com/RPi-Distro/raspi-config/blob/master/raspi-config). FYI Ubuntu pre-installs cloud-guest-utils, for use with cloud-init.

@holta holta reopened this Aug 7, 2022
@holta holta modified the milestones: 7.2, 8.0 Aug 7, 2022
@jvonau jvonau closed this Aug 7, 2022
@jvonau jvonau deleted the resize branch August 7, 2022 13:19
@holta
Copy link
Member

holta commented Aug 7, 2022

Several people read over this ticket quite carefully in recent days.

And are impressed by the progress that was made over ~12 days, by @jvonau and @tim-moody trying to work this out.

Hopefully much of this (or most, or possibly all) still applies almost 2 years later.

@jvonau
Copy link
Contributor Author

jvonau commented Aug 8, 2022

The opening post points straight at the race condition, went 747, I didn't think I needed to point out how wrong starting the resize before fsck can be but that was a bad assumption on my part.

@jvonau
Copy link
Contributor Author

jvonau commented Aug 8, 2022

This issue is 2 years old, and who are these "Several people read over this ticket quite carefully in recent days." that you speak of and for? I'm sick of the compartmentalization of the reports through a single channel, what is this, a branch of the CIA or an open source project with full transparency?

holta added a commit that referenced this pull request Sep 26, 2022
Straw Man 1-prep/templates/iiab-expand-rootfs.service based on 2020's PR #2522 + /usr/sbin/iiab-expand-rootfs "bash -xe" exit-on-error (to defer deleting /.expand-rootfs)
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

3 participants