@@ -247,12 +247,75 @@ cleanSwap() {
247247 free -h
248248}
249249
250+ # Try to find a different drive to put our data on so we don't need to run cleanup.
251+ # The availability of the disks we're probing isn't guaranteed,
252+ # so this is opportunistic.
253+ checkAlternative () {
254+ local gha_alt_disk=" /mnt"
255+
256+ # we need ~50GB of space
257+ local space_target_kb=$(( 50 * 1024 * 1024 ))
258+ local available_space_kb=$( df -k " $gha_alt_disk " --output=avail | tail -n 1)
259+
260+ # mount options that trade durability for performance
261+ # ignore-tidy-linelength
262+ local mntopts=" defaults,discard,journal_async_commit,barrier=0,noauto_da_alloc,lazytime,data=writeback"
263+
264+ # GHA has a 2nd disk mounted at /mnt that is almost empty.
265+ # Check if it's a valid mountpoint and it has enough available space.
266+ if mountpoint " $gha_alt_disk " && [ " $available_space_kb " -ge " $space_target_kb " ]; then
267+ local blkdev=$( df -k " $gha_alt_disk " --output=source | tail -n 1)
268+ echo " Sufficient space available on $blkdev mounted at $gha_alt_disk "
269+ # see cleanSwap(), swapfile may be mounted under /mnt
270+ sudo swapoff -a || true
271+
272+ # unmount from original location
273+ sudo umount " $gha_alt_disk "
274+
275+ # remount under the obj dir which is used by docker scripts to write most
276+ # of our build output. And apply optimized mount options while we're at it.
277+ mkdir ./obj
278+ if ! sudo mount $blkdev ./obj -o $mntopts ; then
279+ sudo dmesg | tail -n 20 # kernel log should have more details for mount failures
280+ echo " ::warning::Failed to remount $blkdev to ./obj with options: $mntopts "
281+ return
282+ fi
283+
284+ # ensure current user can access everything.
285+ # later scripts assume they have recursive access to obj
286+ sudo chown -R " $USER " :" $USER " ./obj
287+
288+ # Exit from this script to avoid wasting time removing disk space,
289+ # as we already have enough disk space in the alternative drive.
290+ exit 0
291+ fi
292+
293+ # ephemeral NVMe drives on AWS
294+ for dev in /dev/nvme* n1; do
295+ # check that it's a blockdev and not mounted.
296+ if [ -b " $dev " ] && [ " $( mount | grep " $dev " | wc -l) " -eq 0 ]; then
297+ echo " Found unused block device $dev , creating filesystem"
298+ sudo mkfs.ext4 -E lazy_itable_init=1,lazy_journal_init=1 " $dev "
299+ mkdir ./obj
300+ sudo mount " $dev " ./obj -o $mntopts
301+ sudo chown -R " $USER " :" $USER " ./obj
302+
303+ exit 0
304+ fi
305+ done
306+ }
307+
308+
309+
250310# Display initial disk space stats
251311
252312AVAILABLE_INITIAL=$( getAvailableSpace)
253313
254314printDF " BEFORE CLEAN-UP:"
255315echo " "
316+
317+ checkAlternative
318+
256319execAndMeasureSpaceChange cleanPackages " Unused packages"
257320execAndMeasureSpaceChange cleanDocker " Docker images"
258321execAndMeasureSpaceChange cleanSwap " Swap storage"
0 commit comments