Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/secure-boot/no-customization.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

function exit_handler() {
set +ex
echo "Exit handler invoked"

# Process disk usage logs from installation period
rm -f /run/keep-running-df
sync
sleep 5.01s
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a fixed sleep to synchronize with the background df process is fragile and can lead to race conditions. If the system is under heavy load, the df command or other operations could be delayed, potentially causing this handler to proceed before the final disk usage is logged. A more robust approach would be to manage the background process via its Process ID (PID), allowing you to explicitly wait for it to terminate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll note on review of install_gpu_driver.sh that the df -h is interleaved with calls to sync. Can you tell me why I might have designed these disk usage checks in this manner?

# compute maximum size of disk during installation
# Log file contains logs like the following (minus the preceeding #):
#Filesystem 1K-blocks Used Available Use% Mounted on
#/dev/vda2 7096908 2611344 4182932 39% /
df / | tee -a "/run/disk-usage.log"

perl -e '($first, @samples) = grep { m:^/: } <STDIN>;
unshift(@samples,$first); $final=$samples[-1];
($starting)=(split(/\s+/,$first))[2] =~ /^(\d+)/;
($ending)=(split(/\s+/,$final))[2] =~ /^(\d+)/;
@siz=( sort { $a => $b }
map { (split)[2] =~ /^(\d+)/ } @samples );
$max=$siz[0]; $min=$siz[-1]; $inc=$max-$starting;
print( " samples-taken: ", scalar @siz, $/,
"starting-disk-used: $starting", $/,
" ending-disk-used: $ending", $/,
" maximum-disk-used: $max", $/,
" minimum-disk-used: $min", $/,
" increased-by: $inc", $/ )' < "/run/disk-usage.log"
Comment on lines +17 to +29
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The sort comparator { $a => $b } is incorrect for numeric sorting. The => operator (fat comma) is used for key/value pairs and does not perform the intended numeric comparison. This will result in incorrect maximum and minimum disk usage values being reported. For a descending numeric sort, you should use the numeric comparison operator <=>, like this: sort { $b <=> $a }.

Consider replacing the Perl script with a more robust, readable, and memory-efficient awk equivalent. awk is a standard tool well-suited for this kind of line-by-line processing.

awk '
    /\/\// {
        used = $3;
        if (samples++ == 0) {
            start = used;
            min = used;
            max = used;
        }
        if (used < min) { min = used; }
        if (used > max) { max = used; }
        last = used;
    }
    END {
        if (samples > 0) {
            inc = max - start;
            print "     samples-taken: " samples
            print "starting-disk-used: " start
            print "  ending-disk-used: " last
            print " maximum-disk-used: " max
            print " minimum-disk-used: " min
            print "      increased-by: " inc
        }
    }' "/run/disk-usage.log"

Copy link
Copy Markdown
Contributor Author

@cjac cjac Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. That was a typo of >= I think. I'll patch in a fix and amend the commit.

Regarding not using Perl: no thank you. My intention is to provide support for on Google platforms Perl at the same level as PHP and Python. Instead, let's work on ensuring the language has native support for all necessary interop interfaces. Join me on the p5p ml to discuss further.


# zero free disk space
if [[ -n "$(get_metadata_attribute creating-image)" ]]; then
dd if=/dev/zero of=/zero
sync
sleep 3s
rm -f /zero
fi

echo "exit_handler has completed"
return 0
}

# Monitor disk usage in a screen session
df / | tee "/run/disk-usage.log"
touch "/run/keep-running-df"
screen -d -m -LUS keep-running-df \
bash -c "while [[ -f /run/keep-running-df ]] ; do df / | tee -a /run/disk-usage.log ; sleep 5s ; done"

trap exit_handler EXIT

apt-get update -y -qq > /dev/null 2>&1

echo "exit handler will be triggered after this operation."