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

Code before:
    # 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
}


checkAlternative

# Display initial disk space stats

AVAILABLE_INITIAL=$(getAvailableSpace)

printDF "BEFORE CLEAN-UP:"
echo ""
execAndMeasureSpaceChange cleanPackages "Unused packages"
execAndMeasureSpaceChange cleanDocker "Docker images"
execAndMeasureSpaceChange cleanSwap "Swap storage"
execAndMeasureSpaceChange removeUnusedFilesAndDirs "Unused files and directories"

# Output saved space statistic
echo ""
printDF "AFTER CLEAN-UP:"

echo ""
echo ""

printSavedSpace "$AVAILABLE_INITIAL" "Total saved"


Review comment (suggestion):
```suggestion
        # Exit from this script to avoid wasting time removing disk space,
        # as we already have enough disk space in the alternative drive.
        exit 0
```

Code after:
    # 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.
        mkdir ./obj
        if ! sudo mount $blkdev ./obj -o $mntopts; then
            sudo dmesg | tail -n 20 # kernel log should have more details for mount failures
            echo "::warning::Failed to remount $blkdev to ./obj with options: $mntopts"
            return
        fi

        # ensure current user can access everything.
        # later scripts assume they have recursive access to obj
        sudo chown -R "$USER":"$USER" ./obj

        # Exit from this script to avoid wasting time removing disk space,
        # as we already have enough disk space in the alternative drive.
        exit 0
    fi

    # ephemeral NVMe drives on AWS
    for dev in /dev/nvme*n1; do
        # check that it's a blockdev, not mounted and doesn't have partitions
        if [ -b "$dev" ] && [ "$(mount | grep "$dev" | wc -l)" -eq 0 ] && [ "$(lsblk -no PARTTYPE "$dev" | grep . | wc -l)" -eq 0 ]; then
            echo "Found unused block device $dev, creating filesystem"
            if ! sudo mkfs.ext4 -E lazy_itable_init=1,lazy_journal_init=1 "$dev" ; then
                sudo dmesg | tail -n 5 # kernel log should have more details for mkfs failures
                echo "Failed to create ext4 filesystem on $dev"
                continue
            fi
            mkdir ./obj
            sudo mount "$dev" ./obj -o $mntopts
            sudo chown -R "$USER":"$USER" ./obj

            exit 0
        fi
    done
}



# Display initial disk space stats