diff --git a/.gitignore b/.gitignore index 723ef36..cfeba80 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +/rejuvenation/setup/virtualbox/*.ova \ No newline at end of file diff --git a/rejuvenation/machine_resources_monitoring/processes/LxcProcessMonitoring.sh b/rejuvenation/machine_resources_monitoring/processes/LxcProcessMonitoring.sh new file mode 100644 index 0000000..89a9cb8 --- /dev/null +++ b/rejuvenation/machine_resources_monitoring/processes/LxcProcessMonitoring.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Script to continuously monitor an LXC container process and log resource usage to a CSV file + +VM_NAME="debian12" +log_file="logs/lxc_monitoring-${VM_NAME}.csv" +mkdir -p logs +echo "CPU;MEM;VMRSS;VSZ;Threads;Swap;Date_Time" > "$log_file" + +while true; do + pid=$(lxc-info -n "$VM_NAME" -pH) + + date_time=$(date +%d-%m-%Y-%H:%M:%S) + + if [ -n "$pid" ]; then + data=$(pidstat -u -h -p $pid -T ALL -r 1 1 | sed -n '4p') + thread=$(cat /proc/"$pid"/status | grep Threads | awk '{print $2}') + cpu=$(echo "$data" | awk '{print $8}') + mem=$(echo "$data" | awk '{print $14}') + vmrss=$(echo "$data" | awk '{print $13}') + vsz=$(echo "$data" | awk '{print $12}') + swap=$(cat /proc/"$pid"/status | grep VmSwap | awk '{print $2}') + + echo "$cpu;$mem;$vmrss;$vsz;$thread;$swap;$date_time" >> "$log_file" + else + sleep 1 + echo "0;0;0;0;0;0;$date_time" >> "$log_file" + fi +done \ No newline at end of file diff --git a/rejuvenation/setup/lxc/Dependencies.sh b/rejuvenation/setup/lxc/Dependencies.sh new file mode 100644 index 0000000..242cae6 --- /dev/null +++ b/rejuvenation/setup/lxc/Dependencies.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +# ############################## IMPORTS ############################# +source ../../machine_resources_monitoring/general_dependencies.sh +# #################################################################### + +# SYSTEM_UPDATE() +# DESCRIPTION: +# Attempts to update the host's repositories and system apps +SYSTEM_UPDATE() { + sudo apt-get update && sudo apt-get upgrade -y +} + +INSTALL_UTILS() { + echo "Instalando utilitários necessários: lxc-templates e bridge-utils..." + if sudo apt-get install lxc-templates bridge-utils -y; then + echo "Utilitários instalados com sucesso." + else + echo "Erro ao instalar utilitários." >&2 + exit 1 + fi +} + +# LXC_INSTALL() +# DESCRIPTION: +# Install LXC if it's not installed +LXC_INSTALL() { + if ! dpkg -l lxc | grep -q '^ii'; then + echo "LXC não está instalado, instalando..." + if ! sudo apt-get install lxc lxd lxd-client -y; then + echo -e "\nERRO: Erro ao tentar instalar o LXC/LXD\n" >&2 + exit 1 + fi + echo -e "\nLXC/LXD instalado com sucesso, inicializando LXD...\n" + INITIALIZE_LXD + else + echo "LXC/LXD já está instalado, verificando configuração..." + INITIALIZE_LXD + fi +} + +# INITIALIZE_LXD() +# DESCRIPTION: +# Initialize LXD with a default storage pool and network +INITIALIZE_LXD() { + # Check if a default storage pool exists + if ! sudo lxc storage list | grep -q "default"; then + sudo lxc storage create default dir + fi + + # Check if a default NAT network exists + if ! sudo lxc network list | grep -q "lxdbr0"; then + sudo lxc network create lxdbr0 ipv4.address=auto ipv4.nat=true ipv6.address=none ipv6.nat=false + fi + + echo "LXD configurado com storage pool padrão e rede NAT." +} + +# INSTALL_DEPENDENCIES() +# DESCRIPTION: +# Starts dependency checking and installs dependencies requirements +INSTALL_DEPENDENCIES() { + if [[ -f /etc/os-release ]]; then + . /etc/os-release + else + echo "Não foi possível determinar a distribuição do sistema." + exit 1 + fi + + if [ $ID = "debian" ]; then + SYSTEM_UPDATE + INSTALL_GENERAL_DEPENDENCIES + LXC_INSTALL + echo -e "\nInstalações completas\n" + else + echo "ERRO: Este script é apenas para Debian." + exit 1 + fi +} + +INSTALL_DEPENDENCIES \ No newline at end of file diff --git a/rejuvenation/setup/lxc/SetupVm.sh b/rejuvenation/setup/lxc/SetupVm.sh new file mode 100644 index 0000000..fa2ee30 --- /dev/null +++ b/rejuvenation/setup/lxc/SetupVm.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +# ############################## IMPORTS ############################# +source ../../virtualizer_functions/LxcManager.sh +# #################################################################### + +readonly VM_NAME="vmDebian" +ISO_PATH="/software-aging/debian-12.5.0-arm64-netinst.iso" +STORAGE_POOL="default" + +# FUNCTION=CREATE_VM() +# DESCRIPTION: +# Limits.cpu and limits.memory - configure cpu and memory limit +# Disk source - adds and installation disk to the VM +CREATE_VM() { + if lxc list | grep -q "$VM_NAME"; then + echo "$VM_NAME já existe. Parando a função." + return + fi + + # Check if the storage pool exists + if ! lxc storage list | grep -q "$STORAGE_POOL"; then + echo "Storage pool '$STORAGE_POOL' não encontrado. Criando..." + lxc storage create "$STORAGE_POOL" dir + fi + + # Create VM and configure + lxc init "$VM_NAME" --empty --vm --profile default -s "$STORAGE_POOL" + lxc config device add "$VM_NAME" root disk pool="$STORAGE_POOL" path=/ + lxc config device add "$VM_NAME" iso disk source="$ISO_PATH" boot.priority=10 + lxc config set "$VM_NAME" limits.cpu=2 limits.memory=512MB + + lxc start "$VM_NAME" + echo "$VM_NAME iniciada com a ISO $ISO_PATH" +} + +START_VM() { + lxc start "$VM_NAME" +} + +STOP_VM() { + lxc stop "$VM_NAME" +} + +DELETE_VM() { + lxc delete "$VM_NAME" --force +} + +RESTART_VM() { + STOP_VM + sleep 5 + START_VM +} + +SETUP_LXC_VM() { + CREATE_VM + START_VM +} + +SETUP_LXC_VM \ No newline at end of file diff --git a/rejuvenation/virtualizer_functions/LxcManager.sh b/rejuvenation/virtualizer_functions/LxcManager.sh new file mode 100644 index 0000000..eaccb2b --- /dev/null +++ b/rejuvenation/virtualizer_functions/LxcManager.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash + +######################################## LXC FUNCTIONS ######################################## +# Universidade Federal do Agreste de Pernambuco # +# Uname Research Group # +# # +# ABOUT: # +# utilities for managing LXC virtual machines # +############################################################################################### + +VM_NAME="vmDebian" + +START_VM() { + lxc start "$VM_NAME" +} + +STOP_VM() { + lxc stop "$VM_NAME" +} + +DELETE_VM() { + lxc delete "$VM_NAME" --force +} + +# FUNCTION=GRACEFUL_REBOOT() +# DESCRIPTION: +# Graceful reboot by turning vm off and on again +GRACEFUL_REBOOT() { + STOP_VM + sleep 5 + START_VM +} + +# FUNCTION=FORCED_REBOOT() +# DESCRIPTION: +# Forcibly reboot the virtual machine +FORCED_REBOOT() { + lxc restart "$VM_NAME" +} + +CREATE_DISKS() { + local count=1 + local disks_quantity="$1" + local allocated_disk_size="$2" + + mkdir -p ./disks_lxc + + while [[ "$count" -le "$disks_quantity" ]]; do + local disk_path="./disks_lxc/disk$count.img" + truncate -s "$allocated_disk_size" "$disk_path" + echo "Disco $disk_path criado com tamanho $allocated_disk_size." + ((count++)) + done +} + +REMOVE_DISKS() { + local disk_files=(./disks_lxc/*.img) + + for disk_file in "${disk_files[@]}"; do + if [[ -f "$disk_file" ]]; then + echo -e "\n--->> Deletando o disco: $disk_file \n" + rm -f "$disk_file" + if [[ -f "$disk_file" ]]; then + echo -e "Erro: Falha ao deletar o disco: $disk_file \n" + else + echo "Disco $disk_file deletado com sucesso." + fi + fi + done +} + +# FUNCTION=ATTACH_DISK() +# DESCRIPTION: +# Attaches disks to virtual machine +# +# PARAMETERS: +# disk_path = $1 +# target = $2 +ATTACH_DISK() { + local disk_path="$1" + local target="$2" + + if [[ -z "$disk_path" || -z "$target" ]]; then + echo "ERRO: Parâmetros ausentes para anexar o disco." + return 1 + fi + + if [[ ! -f "$disk_path" ]]; then + echo "ERRO: Arquivo de disco não encontrado em $disk_path." + return 1 + fi + + if lxc config device add "$VM_NAME" "$VM_NAME-disk" disk source="$disk_path" path="$target"; then + echo "Disco anexado com sucesso ao alvo $target." + else + echo "ERRO: Falha ao anexar o disco ao alvo $target." + fi +} + +# FUNCTION=DETACH_DISK() +# DESCRIPTION: +# Detach disks to virtual machine +# +# PARAMETERS: +# target = $1 #Name of the device to detach +DETACH_DISK() { + local target="$1" + + if [[ -z "$target" ]]; then + echo "ERRO: Parâmetro ausente para desanexar o disco." + return 1 + fi + + if lxc config device remove "$VM_NAME" "$device_name"; then + echo "Disco desanexado com sucesso do dispositivo $device_name." + else + echo "ERRO: Falha ao desanexar o disco do dispositivo $device_name." + fi +} \ No newline at end of file diff --git a/rejuvenation/workloads/LxcWorkload.sh b/rejuvenation/workloads/LxcWorkload.sh new file mode 100644 index 0000000..1729a82 --- /dev/null +++ b/rejuvenation/workloads/LxcWorkload.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +######################################## LXC - WORKLOAD ####################################### +# ABOUT: # +# used to simulate workload on (LXC) virtualization infrastructure # +# # +# WORKLOAD TYPE: # +# DISKS # +############################################################################################### + +# ####################### IMPORTS ####################### +source ./virtualizer_functions/LxcManager.sh +# ####################################################### + +readonly wait_time_after_attach=10 +readonly wait_time_after_detach=10 + +LxcWorkloads() { + local count_disks=1 + local max_disks=50 + local disk_path="path/to/your/lxc/disks/disk" + + while true; do + # attach + for port in {1..3}; do + local target="/mnt/disk$port" + ATTACH_DISK "${disk_path}${count_disks}.img" "$port" + + if [[ "$count_disks" -eq "$max_disks" ]]; then + count_disks=1 + else + ((count_disks++)) + fi + sleep $wait_time_after_attach + done + + # detach + for port in {1..3}; do + DETACH_DISK "disk$port" + sleep $wait_time_after_detach + done + done +} + +LxcWorkloads \ No newline at end of file