Swap space notes

Whats using swap space

for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | less

Where are my swap files

 cat /proc/swaps

How to add more swap space

1. Create empty file:
This file will contain virtual memory contents so make file big enough for your needs. This one will create 1Gb file which means +1Gb swap space for your system:

dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=1024 count=1M

If you want to make 3Gb file then change count value to count=3M. See man dd for more information.

2. Bake swap file:
Following command is going to make “swap filesystem” inside your fresh swap file.

mkswap /media/fasthdd/swapfile.img

3. Bring up on boot:
To make sure that your new swap space is activated while booting up computer you should add it to filesystem configuration file /etc/fstab. Add it to end of file, this is recommended because other filesystems (at least one that contains swap file) must be mounted in read-write mode before we can access any files.

# Add this line to /etc/fstab
/media/fasthdd/swapfile.img swap swap sw 0 0

4. Activate:
You can either reboot your computer or activate new swap file by hand with following command:

swapon /media/fasthdd/swapfile.img

Original articles

https://askubuntu.com/questions/178712/how-to-increase-swap-space
https://www.cyberciti.biz/faq/linux-which-process-is-using-swap/

EasyRSA – Make a certificate and copy to ansible staging dir

I use this script on my CA server to create a certificate for each new server we provision. This allows our internal PKI to function.

This script creates a certificate then copies it to the Ansible server where is can be deployed to the destination host

Obviously you’ll need to take the necessary precautions around key security

ISSUE_NAME=$1.domain.local

cd /home/admin/EasyRSA-3.0.5/
/home/admin/EasyRSA-3.0.5/easyrsa build-server-full $ISSUE_NAME nopass
ssh edpk-ansible..local 'mkdir -p /home/admin/ansible/files/'$1'/'
scp /home/admin/EasyRSA-3.0.5/pki/issued/$ISSUE_NAME.crt edpk-ansible..local:/home/admin/ansible/files/$1/$1.crt
scp /home/admin/EasyRSA-3.0.5/pki/private/$ISSUE_NAME.key edpk-ansible..local:/home/admin/ansible/files/$1/$1.key
cd ~

Ubuntu interfaces file examples

Example 1 – Includes some static routes and manually specified IP’s

auto lo
iface lo inet static
address 103.90.59.9/32

auto ens3
iface ens3 inet static
address 172.2.1.17
network 172.2.1.0
netmask 255.255.254.0
up route add -net 172.2.0.0 netmask 255.255.0.0 gw 172.2.1.1
up route add -net 172.2.0.0 netmask 255.255.0.0 gw 172.2.1.1

iface ens3 inet6 static
address 2405:cc:ee:110::7
netmask 64
autoconf 0
accept_ra 0
gateway 2405:cc:ee:110:ff:ff

auto ens4
iface ens4 inet static
address 172.23.2.12
network 172.23.2.0
netmask 255.255.255.0
gateway 172.23.2.254

source /etc/network/interfaces.d/*.cfg

 

MYSQL Backup to S3 script

Script

#!/bin/bash

# Set the enviroment variable so read function knows to seperate on ",".
export IFS=","

NOW=$(date +"%Y_%m_%d_%H_%M")

DATABASES_CONFIG_FILE="/home/bfnadmin/databases.csv"

S3_ENDPOINT="https://my-s3-storage.com"
S3_BUCKET="s3://sql-backups"

TEMP_BACKUP_DIR="backups"

while read HOST USERNAME PASSWORD DB_SRV DB_NAME;
do
echo "[$DB_SRV - $DB_NAME]"

mysqldump --single-transaction --quick --lock-tables=false \
-h $HOST \
-u $USERNAME \
-p$PASSWORD \
$DB_NAME | gzip > $TEMP_BACKUP_DIR/$DB_SRV-$DB_NAME-$NOW.sql.gz

echo "Uploading backup to S3 storage - $DB_SRV-$DB_NAME-$NOW.sql.gz"

aws --endpoint-url=$S3_ENDPOINT s3 cp $TEMP_BACKUP_DIR/$DB_SRV-$DB_NAME-$NOW.sql.gz $S3_BUCKET

rm $TEMP_BACKUP_DIR/$DB_SRV-$DB_NAME-$NOW.sql.gz

echo -e "\n\n"

done < $DATABASES_CONFIG_FILE

~/ .aws/credentials

[default]
aws_access_key_id=xxx
aws_secret_access_key=yyy

databases.csv

ip_Address,sql_user,sql_password,Host_Display_name,DB_Name

Script to enable fast-diff on an entire pool of images and rebuild the object-map

This script enables the requisite features on all RBD images in a pool to allow you to run rbd du and have it return a result quickly as opposed to having to calculate the size very time

rbd ls -p backup1 | while read line; do
  echo "$line"
  rbd feature enable backup1/$line object-map exclusive-lock
  rbd object-map rebuild backup1/$line
  rbd snap ls backup1/$line | while read snap; do
        export snapname=$(echo $snap| awk '{print $2;}')
        if [ ! $snapname == "NAME" ]
        then
                echo "$line@$snapname"
                rbd object-map rebuild backup1/$line@$snapname
        fi
  done
done

KVM\Qemu\Openstack – Manage a live migration

virsh qemu-monitor-command {VMNAME} --pretty '{"execute":"migrate_cancel"}'

Allow Virsh more downtime(If it cant keepup with RAM utilization)

virsh migrate-setmaxdowntime VMNAME 2500

 

Check migration status

virsh domjobinfo instance-000002ac
Job type: Unbounded
Operation: Outgoing migration
Time elapsed: 1307956 ms
Data processed: 118.662 GiB
Data remaining: 9.203 MiB
Data total: 8.005 GiB
Memory processed: 118.662 GiB
Memory remaining: 9.203 MiB
Memory total: 8.005 GiB
Memory bandwidth: 41.294 MiB/s
Dirty rate: 35040 pages/s
Page size: 4096 bytes
Iteration: 197
Constant pages: 1751031
Normal pages: 31041965
Normal data: 118.416 GiB
Expected downtime: 3314 ms
Setup time: 70 ms

 

https://www.redhat.com/archives/libvirt-users/2014-January/msg00007.html
https://specs.openstack.org/openstack/nova-specs/specs/mitaka/implemented/abort-live-migration.html

 

https://www.server24.eu/private-cloud/complete-live-migration-vms-high-load/

 

Bonding in active-backup using linux bridges on Ubuntu 18

Because this was WAAYY more difficult to find any decent doco on that I had ever expected, here is what worked for me

I deleted the netplan config file at /etc/netplan/01-netcfg.yaml

rm /etc/netplan/01-netcfg.yaml

Ensure that ‘bonding’ appears in /etc/modules (I’s not here by default)

echo bonding >> /etc/modules

 

Here is /etc/network/interfaces

source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback

allow-hotplug ens3f0
iface ens3f0 inet manual
bond-master bond0

allow-hotplug ens3f1
iface ens3f1 inet manual
bond-master bond0

allow-hotplug bond0
iface bond0 inet static
address 172.16.103.12/24
gateway 172.16.103.254
mtu 9000

bond-mode active-backup
bond-miimon 100
bond-slaves none

 

error on subcontainer ‘ia_addr’ insert (-1)

If you use SNMP on Ubuntu you might see this annoying log entry repeat itself in syslog
Sep 15 17:29:34 myServer snmpd[3609]: error on subcontainer 'ia_addr' insert (-1)
Easy fix is
sed -i 's/Lsd/LS6d/g' /etc/default/snmpd
service snmpd restart

Linux static routes & fixing “Error: either “to” is duplicate, or gw is a garbage.”

I was having troubles adding a route using ip route, I was getting “Error: either “to” is duplicate, or “172.16.1.254” is a garbage.”

[root@adlwest-nms1 ~]# ip route add 172.16.103.0/24 172.16.1.254 dev eth1
Error: either "to" is duplicate, or "172.16.1.254" is a garbage.

But running ip route add subnet/netmask via gw worked

[root@adlwest-nms1 ~]# ip route add 172.16.103.0/24 via 172.16.1.254
[root@adlwest-nms1 ~]# ping 172.16.103.41
PING 172.16.103.41 (172.16.103.41) 56(84) bytes of data.
64 bytes from 172.16.103.41: icmp_seq=1 ttl=62 time=1.04 ms
64 bytes from 172.16.103.41: icmp_seq=2 ttl=62 time=0.992 ms