Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

September 05, 2019

VMware - Increasing Disk sizes in Linux

http://echenh.blogspot.co.nz/2010/04/how-to-extend-lvm-on-vmware-guest-os.html
http://conrey.org/2011/05/18/lvm-and-vmware-linux-guests-just-do-it/

1. Increase Disk Size
2. Create Partition for used space using cfdisk / Primary Disk
3. pvcreate /dev/sda3
4. vgextend vg_vmlinuxcentos6 /dev/sda3
5. lvextend -l +100%FREE /dev/vg_vmlinuxcentos6/lv_root
6. lvdisplay
7. resize2fs /dev/vg_vmlinuxcentos6/lv_root

August 28, 2017

Perl script to shutdown VM's on ESXi 6.5

I use this script to shutdown all my VM's when UPS battery is low, or when I need to do updates on my NAS'

#!/usr/bin/perl

# Get list of ALL VM's
# (\d{1,2})\s+([\w\d\-]+).*\n

$cmdvmlist = "vim-cmd vmsvc/getallvms";

# RegEX for running VM's
# ([\d]+\-[\w\d\s\.]+)\n\s+World ID:\s(\d+)

$cmdvmrunning = "esxcli vm process list";

$esxhost = "root\@192.168.1.26";

$vmlist = `ssh $esxhost $cmdvmlist`;

#print $vmlist;

# ([\d]+\-[\w\d\s\.]+)\n\s+World ID:\s(\d+)

my @shutdownorder;

$count=0;

print "VM Inventory on $esxhost\n";

while ($vmlist =~ /(\d{1,2})\s+([\w\d\-]+).*\n/g)
        {

        # Don't shutdown the VM running this script until last as it also controls APCUPSD

        if ($1 != 30)
                {
                $shutdownorder[$count] = $1;
                $shutdownordername[$count] = $2;
                $count++;
                }

        }

print "VMID\tName\n";

my $index;
for my $order (@shutdownorder) {
  print "$order \t $shutdownordername[$index++]\n";
}

# If shutdown argument is present, shutdown VM's

if ($ARGV[0] eq "shutdown")
        {
        open (my $fh, '>', "/tmp/esxhostscript");

        print "\nCreating Script for ESX Host\nUploading ";

        while (@shutdownorder)
                {
                $vm = shift(@shutdownorder);

                print $fh "vim-cmd vmsvc/power.shutdown $vm\n";
                }

        close $fh;

        # Copy script to ESX Host

        system("scp","/tmp/esxhostscript","root\@192.168.1.26:/tmp/esxhostscript");

        # Add Execute Permission

        system("ssh","root\@192.168.1.26","chmod +x /tmp/esxhostscript");

        # Execute Script

        system("ssh","root\@192.168.1.26","/tmp/esxhostscript");

        print "Waiting for VM's to shutdown\n";
        $timeout = 300;

        while (CheckRunning())
                {
                print "$timeout ";
                sleep 5;
                $timeout=$timeout-5;
                if ($timeout<=0){exit;}
                }
        print "\n\n";

        }

else
        {
        printf "shutdown argument not passed, no action taken\n"
        }


sub CheckRunning
        {

        $vmrun = `ssh $esxhost $cmdvmrunning`;

        while ($vmrun =~ /([\d]+\-[\w\d\s\.]+)\n\s+World ID:\s(\d+)/g)
                {

                if ($1 ne "0-Control")
                        {
                        return 1;
                        }
                }
        return 0;
        }

To start them backup I use the autostart script on the ESXi Host:

ssh root@192.168.1.26 /sbin/vmware-autostart.sh start


April 11, 2017

mySQL Backup

nano /usr/local/bin/backup-mysql
#!/bin/bash


DB_BACKUP="/media/nas-backup/mx1.wjw.nz/mysql/`date +%Y-%m-%d`"
DB_USER="backup"
DB_PASSWD="asd"

# Create the backup directory
mkdir -p $DB_BACKUP

# Remove backups older than 10 days
# find /backups/mysql_backup/ -maxdepth 1 -type d -mtime +10 -exec rm -rf {} \;
 
# Backup each database on the system
for db in $(mysql --user=$DB_USER --password=$DB_PASSWD -e 'show databases' -s --skip-column-names|grep -viE '(staging|performance_schema|information_schema)');
do mysqldump --user=$DB_USER --password=$DB_PASSWD --events --opt --single-transaction $db | gzip > "$DB_BACKUP/mysqldump-$HN-$db-$(date +%Y-%m-%d).gz";   
done



chmod +x /usr/local/bin/backup-mysql mysql> grant select,show view, lock tables, event on *.* to 'backup'@localhost identified by 'xxxxxx';

March 24, 2017

Autostart VM's from CLI

If VM's are configured Startup/shutdown on boot, you can start manually by running:

/sbin/vmware-autostart.sh start

on the command line.


Login to Synology NAS with SSH Keys

On the host you want to be able to login from:

run "ssh-keygen -t rsa" make sure to have a blank passphrase

cat /root/.ssh/id_rsa.pub and save that info for later

On the Synology NAS

Edit /etc/ssh/sshd_config change

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys

to this:

#RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Add the necessary files, folders and permissions

mkdir /root/.ssh
touch /root/.ssh/authorized_keys
chmod 700 /root/.ssh
chmod 644 /root/.ssh/authorized_keys

Add your pub key to /root/.ssh/authorized_keys:

echo "ssh-rsa yourkey youruser@yourhost" >> /root/.ssh/authorized_keys

Done!

ESX VM and Host Shutdown Script

#!/usr/bin/perl

# Get list of ALL VM's
# (\d{1,2})\s+([\w\d\-]+).*\n

$cmdvmlist = "vim-cmd vmsvc/getallvms";

# RegEX for running VM's
# ([\d]+\-[\w\d\s\.]+)\n\s+World ID:\s(\d+)

$cmdvmrunning = "esxcli vm process list";

$esxhost = "root\@192.168.1.26";

$vmlist = `ssh $esxhost $cmdvmlist`;

#print $vmlist;

# ([\d]+\-[\w\d\s\.]+)\n\s+World ID:\s(\d+)

my @shutdownorder;

$count=0;

print "VM Inventory on $esxhost\n";

while ($vmlist =~ /(\d{1,2})\s+([\w\d\-]+).*\n/g)
        {

        # Don't shutdown the VM running this script until last as it also controls APCUPSD

        if ($1 != 30)
                {
                $shutdownorder[$count] = $1;
                $shutdownordername[$count] = $2;
                $count++;
                }

        }

print "VMID\tName\n";

my $index;
for my $order (@shutdownorder) {
  print "$order \t $shutdownordername[$index++]\n";
}

# If shutdown argument is present, shutdown VM's

if ($ARGV[0] eq "shutdown")
        {
        open (my $fh, '>', "/tmp/esxhostscript");

        print "\nCreating Script for ESX Host\nUploading ";

        while (@shutdownorder)
                {
                $vm = shift(@shutdownorder);

                print $fh "vim-cmd vmsvc/power.shutdown $vm\n";
                }

        print $fh "shutdown +3\n";

        close $fh;

        # Copy script to ESX Host

        system("scp","/tmp/esxhostscript","root\@192.168.1.26:/tmp/esxhostscript");

        # Add Execute Permission

        system("ssh","root\@192.168.1.26","chmod +x /tmp/esxhostscript");

        # Execute Script

        system("ssh","root\@192.168.1.26","/tmp/esxhostscript");

        print "Waiting for VM's to shutdown\n";

        while (CheckRunning())
                {
                print ".";
                sleep 5;
                }
        print "\n\n";

        # SHUTDOWN NAS

        @nasshut1 = `ssh -p 9999 192.168.1.14 shutdown -h now`;
        @nasshut2 = `ssh -p 9999 192.168.1.24 shutdown -h now`;

        }

else
        {
        printf "shutdown argument not passed, no action taken\n"
        }


sub CheckRunning
        {

        $vmrun = `ssh $esxhost $cmdvmrunning`;

        while ($vmrun =~ /([\d]+\-[\w\d\s\.]+)\n\s+World ID:\s(\d+)/g)
                {
                if ($2 ne "91789")
                        {
                        return 1;
                        }
                }
        return 0;
        }

January 09, 2017

LetsEncrypt

apt-get install python-letsencrypt-apache

letsencrypt -w /var/lib/roundcube -d webmail.wjw.nz

/etc/letsencrypt/live/webmail.wjw.nz/fullchain.pem


September 13, 2016

Cleanly shutdown ESXi 6.0 / 6.5 and Synology NAS with APCUPSD and ESXCLI

I have this working now:

You will need the NAS' to be set to startup on power failure and have wakeonlan enabled.

1. Setup autologins for the root user to:
Generate Key on master - ssh-keygen -t rsa
Access the ESXi Host via SSH
/etc/ssh/keys-root/authorized_keys
Access the Synology NAS via SSH
/root/.ssh/authorized_keys

More info here: Login-to-synology-nas-with-ssh-keys

2. Create a script on the Linux host that:
Gets a list of all "Running VM's"
Creates a script on the ESXHost containing the commands to shutdown the VM's
Execute the ESX Script via SSH
Shutdown the Synology NAS

  VM Shutdown Script

3. Setup APCUPSD to:
Run the script on the local linux host to create the ESX Script
Shutdown the ESX Host via SSH (local VM will shutdown as part of this)

First of all we need a linux host with APCUPSD

Edit /etc/apcupsd/onbattery

Add:

#Shutdown running VM's
/usr/local/bin/ESX-shutdown-running.pl
#Shutdown NAS1
ssh -p 9999 192.168.1.24 shutdown -h
#Shutdown NAS2
ssh -p 9999 192.168.1.14 shutdown -h

Also to get things going again if it comes off battery:

Edit /etc/apcupsd/offbattery

# ssh -p 9999 192.168.1.24 shutdown -h
wakeonlan 00:11:32:39:87:25
# ssh -p 9999 192.168.1.14 shutdown -h
wakeonlan 00:11:32:38:E8:EF
# Reboot ESX Host, for me this is better than just autostarting the shutdown
# VM's as the storage doesnt always reconnect correctly.
ssh 192.168.1.26 reboot


-----------------

Notes..

esxcli --sessionfile /root/esxhost  network ip interface list
esxcli --sessionfile /root/esxhost vm process list

esxcli vm process kill -t [soft,hard,force] -w WorldNumber


May 02, 2016

Flexget iwth Systemd in Ubuntu 15 & 16

Assumptions:

Config & DB Location: /etc/flexget/
User: debian-deluged
Group: debian-deluged

chown -R debian-deluged:debian-deluged /etc/flexget

nano /etc/systemd/system/flexget.service

[Unit]
Description=Flexget Daemon
After=network.target
[Service]
Type=simple
User=debian-deluged
Group=debian-deluged
UMask=022
WorkingDirectory=/etc/flexget/
ExecStart=/usr/local/bin/flexget --logfile /var/log/flexget.log --loglevel verbose daemon start
ExecStop=/usr/local/bin/flexget daemon stop
Restart=Always
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target

systemctl enable flexget.service
systemctl start flexget

April 26, 2016

Install StartSSL Certificate on Dovecot IMAP Server

wget http://www.startssl.com/certs/sub.class1.server.ca.pem -O sub.class1.server.ca.pem

cat www_certificate.pem sub.class1.server.ca.pem &gt; /etc/ssl/certs/dovecot.pem

cat www_privatekey.pem &gt; /etc/ssl/private/dovecot.pem

chown root:ssl-cert /etc/ssl/private/dovecot.pem

chmod 644 /etc/ssl/private/dovecot.pem

chown root:root /etc/ssl/certs/dovecot.pem

chmod 444 /etc/ssl/certs/dovecot.pem

In /etc/dovecot/conf.d/10-ssl.conf

ssl_cert_file = /etc/ssl/certs/dovecot.pem

ssl_key_file = /etc/ssl/private/dovecot.pem

wget http://www.startssl.com/certs/ca.pem -O /tmp/ca.pem

openssl s_client -CAfile /tmp/ca.pem -connect mail.example.org:993

February 03, 2016

init.d check if mount point exists

# Exit if mount point does not exist
if [ ! -s "/media/media-dupe/freenas" ]; then
  echo "Mount Point not Found"
  exit 0
fi

January 12, 2015

Perl Script to search a given folder and its sub folders and extract and rar files into the directory they were found in:


#!/usr/bin/perl
use File::Find;
use autodie;
use File::Basename;
use File::Slurp 'read_file';

my $fname = '/home/rarlist.txt';
my $rarfile_text = read_file ($fname); # grep with regular expression

# Search for RAR files

my @content;
find( \&wanted, $ARGV[0]);

open(FILE, ">> $fname");

foreach $filename (@content){
  if ($filename =~ /\.rar/)
    {
    if($rarfile_text !~ $filename) {
      print FILE "$filename\n";
      my($file, $dir, $ext) = fileparse($filename);
      print "Extracting $file to $dir\n";
      chdir $dir;
      `unrar e -u -ai -y $file`;
    }
  }
}

close(FILE);

exit;

sub wanted {
  push @content, $File::Find::name;
  return;
}

December 08, 2014

ZFS Notes

   28  zpool create -f 2TB-RAIDZ raidz /dev/sdc /dev/sdh /dev/sdi
   29  zpool status datastore
   30  mkdir /media/zfs
   31  zfs create -o mountpoint=/media/zfs/ datastore/zfs
   32  zfs list

Scan for new Hard Disks

ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done


zpool status -x datastore
  647  zpool status -v datastore
  667  zpool status -f datastore
  833  zpool health
  834  zpool list
  835  zpool history
 zfs get compressration
 zfs list

January 02, 2013

ipv6ip / 6in4 tunnels Ubuntu

Add the following to your /etc/network/interfaces file
———–
auto tun0
iface tun0 inet6 v4tunnel
    address <Tunnel IPv6 Address>
    netmask <IPv6 Subnet Mask>
    endpoint <IPv4 Address of Tunnel Server>
    ttl 255
    up ip link set mtu 1480 dev tun0
    up ip route add ::/0 dev tun0
———–
The local tunnel endpoint will be the IPv4 address of the interface facing the Tunnel Server. You may need to reduce the MTU although PMTU happens automatically. You can use tracepath6 ipv6.google.com to find the lowest MTU in your path.
Also make sure you add the following to your IPv4 IPtables rules:
-A INPUT -s <IPv4 Address of Tunnel Server> -i <WAN interface> -j ACCEPT

Kill Multiple processes by name

ps ax | grep “$PROCESS_NAME” | awk ‘{print $1}’ | xargs -i kill {} 2&>/dev/null

PureFTPD User add/del scripts

ftpadduser:
#!/bin/bash
echo Adding FTP user $1
pure-pw useradd $1 -u virftp -g virftp -D /home/virftpusers/$1
mkdir /home/virftpusers/$1
mkdir /home/virftpusers/$1/media
chown virftp:virftp /home/virftpusers/$1/ -R
mount –bind /mnt/500SATA/media/ /home/virftpusers/$1/media
pure-pw mkdb

ftpdeluser:
#!/bin/bash
echo Removing FTP user $1
pure-pw userdel $1
umount /home/virftpusers/$1/media
rm -r /home/virftpusers/$1
pure-pw mkdb