Repository: rust-lang/rust
File: src/ci/scripts/free-disk-space-linux.sh (Shell)

Code before:
    sudo docker image ls
    echo "=> Removing docker images..."
    sudo docker image prune --all --force || true
}

# Remove Swap storage
cleanSwap() {
    sudo swapoff -a || true
    sudo rm -rf /mnt/swapfile || true
    free -h
}

# Try to find a different drive to put our data on so we don't need to run cleanup.
# The availability of the disks we're probing isn't guaranteed,
# so this is opportunistic.
checkAlternative() {
    local mountpoint="/mnt"

    # we need ~50GB of space
    local space_target_kb=$((50 * 1024 * 1024))
    local available_space_kb=$(df -k "$mountpoint" --output=avail | tail -n 1)

    # ignore-tidy-linelength
    local mntopts="defaults,discard,journal_async_commit,barrier=0,noauto_da_alloc,lazytime,data=writeback"

    # GHA has a 2nd disk mounted at /mnt that is almost empty
    if mountpoint /mnt && [ "$available_space_kb" -ge "$space_target_kb" ]; then
        local blkdev=$(df -k "$mountpoint" --output=source | tail -n 1)
        echo "Sufficient space available on $blkdev mounted at $mountpoint"
        sudo swapoff -a || true
        mkdir ./obj
        # remount with O_EATMYDATA while we're at it
        sudo umount /mnt
        sudo mount $blkdev ./obj -o $mntopts || sudo dmesg | tail -n 20
        sudo chown -R "$USER":"$USER" ./obj

        exit 0
    fi

    # ephemeral NVMe drives on AWS
    for dev in /dev/nvme*n1; do
        if [ -b "$dev" ] && [ "$(mount | grep "$dev" | wc -l)" -eq 0 ]; then
            echo "Found unused block device $dev, creating filesystem"
            sudo mkfs.ext4 -E lazy_itable_init=1,lazy_journal_init=1 "$dev"
            mkdir ./obj
            sudo mount "$dev" ./obj -o $mntopts
            sudo chown -R "$USER":"$USER" ./obj

            exit 0
        fi
    done

Review comment (suggestion):
```suggestion
    # GHA has a 2nd disk mounted at /mnt that is almost empty.
    # Check if it's a valid mountpoint and it has enough available space.
```

Code after:
# Remove Docker images.
# Ubuntu 22 runners have docker images already installed.
# They aren't present in ubuntu 24 runners.
cleanDocker() {
    echo "=> Removing the following docker images:"
    sudo docker image ls
    echo "=> Removing docker images..."
    sudo docker image prune --all --force || true
}

# Remove Swap storage
cleanSwap() {
    sudo swapoff -a || true
    sudo rm -rf /mnt/swapfile || true
    free -h
}

sufficientSpaceEarlyExit() {
    local available_space_kb=$(df -k . --output=avail | tail -n 1)

    if [ "$available_space_kb" -ge "$space_target_kb" ]; then
        echo "Sufficient disk space available (${available_space_kb}KB >= ${space_target_kb}KB). Skipping cleanup."
        exit 0
    fi
}

# Try to find a different drive to put our data on so we don't need to run cleanup.
# The availability of the disks we're probing isn't guaranteed,
# so this is opportunistic.
checkAlternative() {
    local gha_alt_disk="/mnt"

    local available_space_kb=$(df -k "$gha_alt_disk" --output=avail | tail -n 1)

    # mount options that trade durability for performance
    # ignore-tidy-linelength
    local mntopts="defaults,discard,journal_async_commit,barrier=0,noauto_da_alloc,lazytime,data=writeback"

    # GHA has a 2nd disk mounted at /mnt that is almost empty.
    # Check if it's a valid mountpoint and it has enough available space.
    if mountpoint "$gha_alt_disk" && [ "$available_space_kb" -ge "$space_target_kb" ]; then
        local blkdev=$(df -k "$gha_alt_disk" --output=source | tail -n 1)
        echo "Sufficient space available on $blkdev mounted at $gha_alt_disk"
        # see cleanSwap(), swapfile may be mounted under /mnt
        sudo swapoff -a || true

        # unmount from original location
        sudo umount "$gha_alt_disk"

        # remount under the obj dir which is used by docker scripts to write most
        # of our build output. And apply optimized mount options while we're at it.