swap.sh(檔案已創建)
@@ -0,0 +1,38 @@ | |||
1 | + | #!/bin/bash | |
2 | + | ||
3 | + | # Define the swap file location and size | |
4 | + | SWAPFILE="/swapfile" | |
5 | + | SWAPSIZE="64G" | |
6 | + | ||
7 | + | # Check if the script is being run as root | |
8 | + | if [ "$(id -u)" -ne 0 ]; then | |
9 | + | echo "This script must be run as root" | |
10 | + | exit 1 | |
11 | + | fi | |
12 | + | ||
13 | + | # Ensure util-linux is installed for mkswap and swapon commands | |
14 | + | apt-get update | |
15 | + | apt-get install -y util-linux | |
16 | + | ||
17 | + | # Create the swap file with the defined size | |
18 | + | fallocate -l $SWAPSIZE $SWAPFILE | |
19 | + | ||
20 | + | # Set the correct permissions for the swap file | |
21 | + | chmod 600 $SWAPFILE | |
22 | + | ||
23 | + | # Set up the swap area | |
24 | + | /sbin/mkswap $SWAPFILE | |
25 | + | ||
26 | + | # Enable the swap file | |
27 | + | /sbin/swapon $SWAPFILE | |
28 | + | ||
29 | + | # Add the swap file to /etc/fstab for automatic mounting at boot | |
30 | + | echo "$SWAPFILE none swap sw 0 0" >> /etc/fstab | |
31 | + | ||
32 | + | # Verify the swap file is active | |
33 | + | /sbin/swapon --show | |
34 | + | ||
35 | + | # Confirm the swap file entry in /etc/fstab | |
36 | + | grep swapfile /etc/fstab | |
37 | + | ||
38 | + | echo "Swap file created and activated successfully." |
上一頁
下一頁