|
| 1 | +# number: 35 |
| 2 | +# tmt: |
| 3 | +# summary: Test composefs garbage collection with same and different kernel+initrd |
| 4 | +# duration: 45m |
| 5 | + |
| 6 | +use std assert |
| 7 | +use tap.nu |
| 8 | + |
| 9 | +$env.RUST_LOG = "debug" |
| 10 | + |
| 11 | +# bootc status |
| 12 | +let st = bootc status --json | from json |
| 13 | +let booted = $st.status.booted.image |
| 14 | + |
| 15 | +if not (tap is_composefs) { |
| 16 | + exit 0 |
| 17 | +} |
| 18 | + |
| 19 | +def get_podman_image_id [image_name: string] { |
| 20 | + let id = (podman inspect --format '{{.Id}}' $image_name | str trim) |
| 21 | + |
| 22 | + let full_id = if ($id | str starts-with "sha256:") { |
| 23 | + $id |
| 24 | + } else { |
| 25 | + $"sha256:($id)" |
| 26 | + } |
| 27 | + |
| 28 | + $full_id |
| 29 | +} |
| 30 | + |
| 31 | +# Create a large file in a new container image, then bootc switch to the image |
| 32 | +def first_boot [] { |
| 33 | + bootc image copy-to-storage |
| 34 | + |
| 35 | + echo $" |
| 36 | + FROM localhost/bootc |
| 37 | + RUN dd if=/dev/zero of=/usr/share/large-test-file bs=1k count=1337 |
| 38 | + RUN echo 'large-file-marker' | dd of=/usr/share/large-test-file conv=notrunc |
| 39 | + " | podman build -t localhost/bootc-derived . -f - |
| 40 | + |
| 41 | + let current_time = (date now) |
| 42 | + |
| 43 | + bootc switch --transport containers-storage localhost/bootc-derived |
| 44 | + |
| 45 | + # Find the large file's verity and save it |
| 46 | + # nu has its own built in find which sucks, so we use the other one |
| 47 | + # TODO: Replace this |
| 48 | + let file_path = ( |
| 49 | + /usr/bin/find /sysroot/composefs/objects -type f -size 1337k -newermt ($current_time | format date "%Y-%m-%d %H:%M:%S") |
| 50 | + | xargs grep -lx "large-file-marker" |
| 51 | + ) |
| 52 | + |
| 53 | + echo $file_path | save /var/large-file-marker-objpath |
| 54 | + |
| 55 | + echo "cat /var/large-file-marker-objpath" |
| 56 | + cat /var/large-file-marker-objpath |
| 57 | + |
| 58 | + tmt-reboot |
| 59 | +} |
| 60 | + |
| 61 | +# Create a container image derived from the first boot image, but update the initrd |
| 62 | +def second_boot [] { |
| 63 | + assert equal $booted.image.image "localhost/bootc-derived" |
| 64 | + |
| 65 | + let path = cat /var/large-file-marker-objpath |
| 66 | + |
| 67 | + echo "\$path" |
| 68 | + echo $path |
| 69 | + |
| 70 | + assert ($path | path exists) |
| 71 | + |
| 72 | + # Create another image with a different initrd so we can test kernel + initrd cleanup |
| 73 | + |
| 74 | + echo " |
| 75 | + FROM localhost/bootc |
| 76 | +
|
| 77 | + RUN echo 'echo hello' > /usr/bin/hello |
| 78 | + RUN chmod +x /usr/bin/hello |
| 79 | +
|
| 80 | + RUN mkdir /usr/lib/dracut/modules.d/99something |
| 81 | +
|
| 82 | + RUN cat <<-EOF > /usr/lib/dracut/modules.d/99something/module-setup.sh |
| 83 | + #!/usr/bin/bash |
| 84 | +
|
| 85 | + check() { |
| 86 | + return 0 |
| 87 | + } |
| 88 | +
|
| 89 | + depends() { |
| 90 | + return 0 |
| 91 | + } |
| 92 | +
|
| 93 | + install() { |
| 94 | + inst '/usr/bin/hello' /bin/hello |
| 95 | + } |
| 96 | + EOF |
| 97 | +
|
| 98 | + RUN set -x; kver=$(cd /usr/lib/modules && echo *); dracut -vf /usr/lib/modules/$kver/initramfs.img $kver; |
| 99 | + " | lines | each { str trim } | str join "\n" | podman build -t localhost/bootc-derived-initrd . -f - |
| 100 | + |
| 101 | + bootc switch --transport containers-storage localhost/bootc-derived-initrd |
| 102 | + |
| 103 | + tmt-reboot |
| 104 | +} |
| 105 | + |
| 106 | +# The large file should've been GC'd as we switched to an image derived from the original one |
| 107 | +def third_boot [] { |
| 108 | + assert equal $booted.image.image "localhost/bootc-derived-initrd" |
| 109 | + |
| 110 | + let path = cat /var/large-file-marker-objpath |
| 111 | + assert (not ($"/sysroot/composefs/objects/($path)" | path exists)) |
| 112 | + |
| 113 | + # Also assert we have two different kernel + initrd pairs |
| 114 | + let booted_verity = (bootc status --json | from json).status.booted.composefs.verity |
| 115 | + let rollback_verity = (bootc status --json | from json).status.rollback.composefs.verity |
| 116 | + |
| 117 | + let bootloader = (bootc status --json | from json).status.booted.composefs.bootloader |
| 118 | + |
| 119 | + let boot_dir = if ($bootloader | str downcase) == "systemd" { |
| 120 | + mkdir /sysroot/boot/efi |
| 121 | + mount /dev/vdb2 /sysroot/boot/efi |
| 122 | + |
| 123 | + "/sysroot/boot/efi/EFI/Linux" |
| 124 | + } else { |
| 125 | + "/sysroot/boot" |
| 126 | + } |
| 127 | + |
| 128 | + assert ($"($boot_dir)/($booted_verity)" | path exists) |
| 129 | + assert ($"($boot_dir)/($rollback_verity)" | path exists) |
| 130 | + |
| 131 | + echo $"($boot_dir)/($booted_verity)" | save /var/to-be-deleted-kernel |
| 132 | + |
| 133 | + # Now we create a new image derived from the current kernel + initrd |
| 134 | + # Switching to this and rebooting should remove the old kernel + initrd |
| 135 | + echo " |
| 136 | + FROM localhost/bootc-derived-initrd |
| 137 | + RUN echo 'another file' > /usr/share/another-one |
| 138 | + " | podman build -t localhost/bootc-final . -f - |
| 139 | + |
| 140 | + |
| 141 | + bootc switch --transport containers-storage localhost/bootc-final |
| 142 | + |
| 143 | + tmt-reboot |
| 144 | +} |
| 145 | + |
| 146 | +def fourth_boot [] { |
| 147 | + assert equal $booted.image.image "localhost/bootc-final" |
| 148 | + assert (not ((cat /var/to-be-deleted-kernel | path exists))) |
| 149 | + |
| 150 | + tap ok |
| 151 | +} |
| 152 | + |
| 153 | +def main [] { |
| 154 | + # Ensure we're running on composefs |
| 155 | + assert (tap is_composefs) |
| 156 | + |
| 157 | + match $env.TMT_REBOOT_COUNT? { |
| 158 | + null | "0" => first_boot, |
| 159 | + "1" => second_boot, |
| 160 | + "2" => third_boot, |
| 161 | + "3" => fourth_boot, |
| 162 | + $o => { error make { msg: $"Invalid TMT_REBOOT_COUNT ($o)" } }, |
| 163 | + } |
| 164 | +} |
| 165 | + |
0 commit comments