Last active
October 31, 2023 17:21
-
-
Save wellington1993/09718ea95a4c1093d134338c8cdec20d to your computer and use it in GitHub Desktop.
Create Swap File in Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Cria um arquivo de swap | |
# Create a swap file | |
# Log file | |
LOGFILE="$HOME/create_swap_file.log" | |
echo "Iniciando a criação do arquivo de swap em: $(date)" >> "$LOGFILE" | |
echo "Initializing the swap file creation at: $(date)" >> "$LOGFILE" | |
# Verifica se o script está sendo executado como root (superusuário) | |
if [[ $EUID -ne 0 ]]; then | |
echo "Este script deve ser executado como root (superusuário)." | |
echo "This script must be run as root (superuser)." | |
exit 1 | |
fi | |
# Verifica se o local de armazenamento foi fornecido, caso contrário, usa /swap | |
LOCATION="${1:-/swap}" | |
# Verifica se o diretório de localização existe, senão cria | |
if [ ! -d "$LOCATION" ]; then | |
sudo mkdir -p "$LOCATION" | |
fi | |
# Verifica se o tamanho do arquivo foi fornecido, caso contrário, usa 8G | |
SIZE="${2:-8G}" | |
# Verifica se o tamanho do arquivo é válido (deve ser maior que 0) | |
if ! [[ "$SIZE" =~ ^[1-9][0-9]*[KMG]$ ]]; then | |
echo "Tamanho de arquivo de swap inválido. Use um formato válido, como 1G ou 512M." | |
echo "Invalid swap file size. Use a valid format, e.g., 1G or 512M." | |
exit 1 | |
fi | |
# Cria o arquivo de swap | |
sudo fallocate -l "$SIZE" "${LOCATION}/swapfile" | |
if [ $? -ne 0 ]; then | |
echo "Erro ao criar o arquivo de swap." | |
echo "Error creating the swap file." | |
exit 1 | |
fi | |
# Verifica se o arquivo de swap está acessível | |
if [ ! -f "${LOCATION}/swapfile" ]; then | |
echo "O arquivo de swap não está acessível." | |
echo "The swap file is not accessible." | |
exit 1 | |
fi | |
# Verifica se o arquivo de swap já está montado | |
if swapon --show | grep -q "${LOCATION}/swapfile"; then | |
echo "O arquivo de swap já está montado." | |
echo "The swap file is already mounted." | |
exit 1 | |
fi | |
# Configura permissões e formata o arquivo de swap | |
sudo chmod 600 "${LOCATION}/swapfile" | |
sudo mkswap "${LOCATION}/swapfile" | |
# Desativa e desmonta o arquivo de swap se estiver em uso ou montado | |
sudo swapoff "${LOCATION}/swapfile" | |
sudo umount "${LOCATION}/swapfile" | |
# Ativa o arquivo de swap | |
sudo swapon "${LOCATION}/swapfile" | |
# Adiciona o arquivo de swap ao /etc/fstab para montagem automática | |
if ! grep -q "${LOCATION}/swapfile" /etc/fstab; then | |
echo "${LOCATION}/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab | |
fi | |
# Mensagem de confirmação | |
echo "Arquivo de swap criado com sucesso em: $(date)" >> "$LOGFILE" | |
echo "Swap file created successfully at: $(date)" >> "$LOGFILE" | |
# Verifica o status do swap | |
free -h | |
swapon --show | |
# Data e hora | |
echo "Script executado em: $(date)" >> "$LOGFILE" | |
echo "Script executed on: $(date)" >> "$LOGFILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment