How to add extra space to Linux VM in XenServer

Before we start you will need to do the following in XenCenter

  • Shutdown the VM
  • Increase the virtual hard drive size of the Linux VM
  • Boot the VM back up

Before starting any of the following, it is a good idea to backup any data you would not want to lose.  You should not lose any data following these steps, but there is always the possibility for something to go wrong.

What is going to happen

  1. Delete the main partition.  We are going to recreate it.
  2. Create a new partition that starts on the same boundary as the previous partition
  3. Write changes to disk and reboot
  4. Resize the Disk
  5. Check that everything went well

The path to the disk is “/dev/xvda” the LVM path/name is “/dev/VolGroup/lv_root”  The goal is to increase lv_root’s size from about 8GB to about 12GB. If for some reason your drive is not “xvda” or your LVM name is different, change the commands accordingly.

Here are the commands in a nutshell.

fdisk /dev/xvda                        <-- Edit the Partition Table
reboot                                 <-- Reboot to apply the partition table updates
pvdisplay                              <-- Display Physical Volume info
pvresize /dev/xvda2                    <-- Resize Physical Volume
lvresize /dev/xvda2 -l +100%FREE       <-- Resize Logical Volume
resize2fs /dev/VolGroup/lv_root        <-- Resize File System

Example:

You may be able to substitute all the fdisk commands with
`parted /dev/xvda resizepart 2 100%`
Change 2 for the actual partition you need to resize.

All the keys and command that were hit and executed are in bold.

[root@localhost ~]# fdisk /dev/xvda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/xvda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00066ace

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64        1045     7875584   8e  Linux LVM
Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/xvda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00066ace

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (64-1566, default 64): 
Command (m for help): p

Disk /dev/xvda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00066ace

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              64        1566    12065871   83  Linux

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# reboot

Show the current size of the Physical Volume

pvdisplay
[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/xvda2
  VG Name               VolGroup
  PV Size               7.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1922
  Free PE               0
  Allocated PE          1922
  PV UUID               zKmGEt-Uf0A-I14h-NDYc-53rf-micT-VxNqsP
   
[root@localhost ~]#

Resize the Physical Volume

pvresize /dev/xvda2
[root@localhost ~]# pvresize /dev/xvda2
  Physical volume "/dev/xvda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
[root@localhost ~]#

Run pvdisplay again.  You should see more space under PV Size.

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/xvda2
  VG Name               VolGroup
  PV Size               11.51 GiB / not usable 2.08 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2945
  Free PE               1023
  Allocated PE          1922
  PV UUID               zKmGEt-Uf0A-I14h-NDYc-53rf-micT-VxNqsP
   
[root@localhost ~]#

Notice the “Free PE” section above.  If it says 0 then you won’t be able to run the next command.

Resize LVM

the “+100%FREE” part of the command tells it to uses up all of the free space available

lvresize /dev/VolGroup/lv_root -l +100%FREE
[root@localhost ~]# lvresize /dev/VolGroup/lv_root -l +100%FREE
  Extending logical volume lv_root to 10.71 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]#

Resize the File System

resize2fs /dev/VolGroup/lv_root
[root@localhost ~]# lvresize /dev/VolGroup/lv_root -l +100%FREE
  Extending logical volume lv_root to 10.71 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]#

And that is it.  Check out the extra space.

[root@localhost ~]# df -h /
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   11G  733M  9.3G   8% /
[root@localhost ~]# exit

How to Backup and Restore UniFi data from the Command Line

Backup

backup the UniFi directory

tar zcvf unifi-backup.tgz /usr/UniFi/

Move the tar archive off site.

Restore

Upload the backup file to the server.

Note: You will need to have the UniFi controller install already.

Copy the archive to the root directory and then extract it.

mv unifi-backup.tgz /
cd /
tar zxvf unifi-backup.tgz

Reboot the server and login to the UniFi controller.

reboot

Upload ssh key to multiple servers automatically

Here is a quick script I created to automate copying a ssh key to multiple remote servers.

Basic command – the command uses sshpass to upload the ssh key to a remote server, this allows you to execute the command and not have to enter in a password to authenticate.

sshpass -p password ssh-copy-id -o StrictHostKeyChecking=no admin@remotehost

Script

#!/bin/bash

remotehosts="$1"
username="admin"
password="MyCoolPassword123"

for host in `cat ${remotehosts}`
do
sshpass -p${password} ssh-copy-id -o StrictHostKeyChecking=no ${username}@${host}
echo "Uploaded key to " ${host}
done

echo "Finished!"

 

Using the script

  1. Download here.
  2. Make it executable
    chmod +x sshcopy.sh
    
  3. Edit the script and change the username and password.
  4. Create a file that contains each host’s IP address or hostname.
  5. Run script (change hostlist.txt to your host list you created in step 3.)
    ./sshcopy.sh hostlist.txt
  6. Wait for the script to finish.

Example:

wget www.incredigeek.com/home/downloads/SSHCopy/sshcopy.sh
chmod +x sshcopy.sh
sed -i s/admin/bob/g sshcopy.sh                      <-- Change username - you can just manually edit the file,
sed -i s/MyCoolPassword123/password/g sshcopy.sh     <-- Change password - it might be easier than using sed
echo "192.168.1.100" >> host.txt                     <-- Add 192.168.1.100 to the host list
echo "Bob" >> host.txt                               <-- Add hostname bob to host list
./sshcopy.sh host.txt                                <-- Upload ssh key to all host's in the host file i.e. "bob" and "192.168.1.100"

How to Install a ZenPack in Zenoss 5

Log into your Zenoss server via ssh.

ssh root@zenossserver

Create working direcotry

 mkdir /tmp/zenpack && cd /tmp/zenpack/

Now upload the ZenPack to “/tmp/zenpack/” on your Zenoss server with your favorite ftp tool.

Stop the Zenoss service

serviced service stop zenoss.core

Restart the services needed to import the ZenPack i.e. the following.

ZooKeeper
mariadb
RabbitMQ
redis
zeneventserver
Zope

You can start all of them with the following commands

serviced service start ZooKeeper
serviced service start mariadb 
serviced service start RabbitMQ
serviced service start redis
serviced service start zeneventserver 
serviced service start Zope

Install ZenPack with the following command

serviced service run zope zenpack install ZenPack.xxx.xxx.egg

Restart Zenoss

serviced service restart zenoss.core

Log into Zenoss and make sure that it is working right.

XenServer Unable to Import OVF

Check to see if you have an import template.

In the Main Menu click “View” and check “Hidden Objects”, in the Server View you should see a Template named something like “XenServer Transfer VM 6.2.0-123456c (hidden)”

If you don’t have one, then run the following command on the XenServer to create a template.

/opt/xensource/packages/files/transfer-vm/install-transfer-vm.sh

If the command fails double check that you have a default storage repository selected and try again.

Once the template is created import the OVF.

How To “Unblock” an IP from cPHulk from the Command Line

cPHulk uses a MySQL database to keep track of different IP’s to block, white list, black list, etc.

When there have been X amount of failed login attempts from an IP, cPHulk adds an entry for the IP in the brutes table.

To “unblock” the IP we need to delete the entry.

open up the cPHulk MySQL database.

root@localhost [~]# mysql cphulkd

If your interested you can view all tables that cPHulk uses.

show tables;

Take a look at all the IP’s in the brutes table.

SELECT IP FROM brutes;

example:

mysql> SELECT IP FROM brutes;
+---------------+
| IP            |
+---------------+
| 30.134.41.221 |     <--  IP we want to unblock
| 31.134.40.251 |
+---------------+
2 rows in set (0.00 sec)

mysql>

To unblock the IP we just need to delete the row that has the IP address we want.

mysql> DELETE FROM brutes WHERE IP="30.134.41.221";

It should return the following.

mysql> DELETE FROM brutes WHERE IP="30.134.41.221";
Query OK, 1 row affected (0.00 sec)

mysql>

Exit MySQL.

mysql> exit
Bye
root@localhost [~]#

 

How to set a Static ip Address in OS X from Command Line

You can set a static ip address in OS X with the following command. Replace en0 with the appropriate interface.

sudo ipconfig set en0 INFORM 192.168.10.12

The default ethernet interface on a Mac is usually en0. You can run ifconfig to view all the interfaces available on the machine.

The ip address will last until you reboot.

How to Install an OS X .pkg file from Command Line

The following command installs the .pkg to the local hard disk.

installer -pkg /Users/username/Desktop/package.pkg -target LocalSystem

You can also replace “LocalSystem with a “/” they both do the same thing.

If you want to install the package on a different disk you can change the -target to the drives path.

installer -pkg package.pkg -target "/Volumes/Macintosh HD2"

You can find more about the command by typing “man installer” in the terminal.

How to Change, Add, and Delete Users in Linux

Changing a users name:

sudo usermod -l new-username old-username

To change the users directory name do the following.

 sudo usermod -d /home/new-username

You might need to log in as root or another user to successfully execute the commands.

Adding a user:

sudo useradd -m newuser

The -m option create the new users home directory

Then to activate the user we need to setup a password.

sudo passwd newuser

Enter and reenter the password and your done.

If you need to add the user to a certain group(s) (sudo?) you can do that with usermod

sudo usermod -G sudo,othergroups newuser

 

Deleting a user:

sudo userdel -r username

The “-r” option deletes the users mail and home directory.  If you wanted to keep them then just discard that option.

Installing Raspbian on Raspberry Pi from the Linux Command Line

First download the Rasbian zip from here.

Unzip the zipped file.

unzip 2014-01-07-wheezy-raspbian.img

You need to find your sdcard name.  If you don’t now how to, take a look at this post.

Next format your card as Fat32.

Now write the image to the sdcard with the following command.  Replace the mmcblk0 part of “of=/dev/mmcblk0” with your drive name.

sudo dd if=~/2014-01-07-wheezy-raspbian.img of=/dev/mmcblk0 bs=4M

It will not display any information until it is finished, so be patience.

Once it is complete, eject it and plug it into your Raspberry Pi and boot it up.