Swap File Setup

Creates and configures a swap file for systems with limited RAM

Script Author

Rowan de Haas's avatar
Rowan de Haas
Script Author

Script Details

Created 11 months ago
Size 1 KB

Tags

Script Content

Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#cloud-config
write_files:
  - path: /etc/sysctl.d/99-swappiness.conf
    content: |
      # Reduce swap usage
      vm.swappiness=10
      vm.vfs_cache_pressure=50

runcmd:
  # Create 2GB swap file
  - fallocate -l 2G /swapfile
  # Set permissions
  - chmod 600 /swapfile
  # Make swap
  - mkswap /swapfile
  # Enable swap
  - swapon /swapfile
  # Add to fstab for persistent mounting
  - echo '/swapfile none swap sw 0 0' >> /etc/fstab
  # Verify swap is active
  - swapon --show
  - free -h

How to Use This Script

Cloud Provider Examples

Amazon EC2

aws ec2 run-instances
  --image-id ami-12345678
  --instance-type t3.micro
  --user-data file://script.yaml

DigitalOcean

doctl compute droplet create
  --image ubuntu-22-04-x64
  --size s-1vcpu-1gb
  --user-data-file script.yaml
  my-droplet

Google Cloud

gcloud compute instances create
  my-instance
  --metadata-from-file
  user-data=script.yaml