Category Archives: How-to

Move Proxmox Container to Different Storage (Updated for LXC)

Get Social!

2015-03-05 00_18_04-Proxmox Virtual Environment storageThe Proxmox Web GUI does not give us the ability to migrate a container from one storage device to another directly. To move a container onto different storage we have to take a backup of the container and restore it to the same ID with a different storage device specified. This can be time laborious when working with several containers.

This is an update to the OpenVZ script found here.

The below script allows you to move an LXC container from one storage device to another. The process requires that the container be stopped, which the script will handle.

Save the below script into a file called migrate.

vi migrate
#!/bin/bash
#
# Filename : migrate
# Description : Migrate Proxmox OpenVZ container from one storage to another
# Author : James Coyle
#
# Version:
# -Date       -Author      -Description
# 20-11-2013  James Coyle  Initial
# 13-12-2017  James Coyle  Changes for LXC
#
#

# Variables
TMP=/tmp      #Location to use to create the backup for transferring to new storage. This needs to be big enough to store the backup archive for the container.

# Do not edit
usage() { 
	echo "Usage: $0" 
	echo "          [-c Required: Container ID to migrate <int>] "
	echo "          [-s Required: Target storage ID <string>]"
	echo "          [-d Optional: Delete the backup file after CT restoration <boolean>]"
	echo ""
	echo "Example: $0 -c 100 -s nasarray"
	echo ""
	exit 1; 
}

while getopts "c:s:d" o; do
  case "${o}" in
    c)
      CT=${OPTARG}
      ;;
    s)
      TARGET_STORAGE=${OPTARG}
      ;;
    d)
      DELETE=true
      ;;
    *)
      usage
      ;;
    esac
done
shift $((OPTIND-1))

# Check mandatory fields
if [ -z "${CT}" ] || [ -z "${TARGET_STORAGE}" ]; then
  usage
fi

RUNNING=false

set -e
set -o pipefail

echo "Moving $CT to $TARGET_STORAGE..."
if pct list| fgrep -w -q "$CT" | grep "running"
then
    RUNNING=true
fi

if $RUNNING
then
    pct stop $CT
fi

vzdump --dumpdir $TMP $CT

ARCHIVE=$(ls -t $TMP/vzdump-lxc-$CT-*.tar | head -n 1)

pct restore $CT $ARCHIVE -force -storage $TARGET_STORAGE

if $RUNNING
then
    pct start $CT
fi

if $DELETE
then
    LOG=$(ls -t $TMP/vzdump-lxc-$CT-*.log | head -n 1)
    echo "Deleting $LOG and $ARCHIVE"
    rm -rf $ARCHIVE $TMP/$LOG
fi

Set execution permissions on the script:

chmod + x migrate

The script has several parameters which are detailed below:

  • -d is specified if you would like the script to delete the temporary backup after the process has completed. Leave this out if you would like the backup tar file to be kept, just in case anything goes wrong.
  • -s is required to specify the name of the target storage. You can find this from the Proxmox Web GUI.
  • -c is required for the container ID to migrate.

In addition, the script contains the variable TMP. This will be the location of the backup tar created as part of the migration process and must contain enough space to store the content of the container being migrated. You can change this to suit your environment.

Example command:

./migrate -d -s newstorage -c 101

 


Reduce Proxmox LXC Backup Size and Time

Category : How-to

Get Social!

Proxmox backs up guests byte-for-byte in a compressed archive. Looking at LXC backups specifically, the file system is compressed into the target backup file with just a few exceptions – temp files aren’t included. You can also add your own exceptions by editing the vzdump.conf to exclude specific file patterns.

All that said, one of the biggest disk space wasters is the cache directory for apt which caches the installation packages for software you have installed. This can generally be safely removed on internet connected machines which will reduce your overall backup size.

For example, a newly created Debian LXC that’s been recently updated shows a total of 206MB of disk used.

du -hs /var/cache/apt/
206M    /var/cache/apt/

After clearing this with the command apt-get clean we can see the space has mostly been freed.

apt-get clean
du -hs /var/cache/apt/
28K    /var/cache/apt/

Considering this whole container is only consuming approximately 1GB of disk space, 200MB is quite significant.

vzdump hooks

Now we can see how much space we can save, we need to make Proxmox issue the apt-get clean command before it creates the backup of our container.

vzdump, the utility which Proxmox uses to perform backups has the ability to call a script for various stages of the backup process – these stages are:

  • backup-start
  • backup-end
  • backup-abort
  • log-end
  • pre-stop
  • pre-restart
  • post-restart

We can use these hooks to run our own commands at any of these points of the backup. For the goal of this blog post, we want to run the apt-get clean command at the point of backup-start.

Create a script on your Proxmox host with the following content:

#!/bin/bash
 
if [ "$1" == "backup-start" ] && [ ${VMTYPE} == "lxc" ]; then
    echo "Running pre backup guest cleanup for $3"
    pct exec "$3" -- bash -c "apt-get clean"
fi

Now edit your vzdump.conf file and add the following line to point to your new script. Remember to change the location of where your script is – I’ve just saved mine in /root/.

script: /root/backup-hook.sh

 


Gitlab Runner Error: sudo: no tty present and no askpass program specified

Get Social!

After issuing the first build on a dynamically created Container I came across the following build error when running a command with sudo.

sudo: no tty present and no askpass program specified

The error is caused by trying to run a command with sudo, however the calling user has not been authorised to use sudo. The error isn’t helpful, and doesn’t really spell out where to go, but adding the calling user to the sudoers file will save the day.

Solution

Open up the sudoers file for editing in your favorite editor.

vi /etc/sudoers

And add your gitlab runner user to the bottom. If you installed your gitlab runner from the official apt repositories then your gitlab-runner process will run under the gitlab-runner user.

Add the following to the bottom of the file:

gitlab-runner ALL=(ALL) NOPASSWD: ALL

Retry your build and you should be back in business!

 


Scripted Install of Oracle Java 8 on Ubuntu 16.04

Get Social!

Please see Install Oracle Java In Debian/ Ubuntu using apt-get for more information.

apt install -y software-properties-common
apt-add-repository -y ppa:webupd8team/java
apt update
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
apt install -y oracle-java8-installer

 


Rename a Proxmox Host

Category : How-to

Get Social!

Renaming a Linux server’s hostname is usually a trivial task, and that’s no Different to a Proxmox server providing it’s not part of a cluster. If your machine is in a cluster then things get a bit more complicated and that’s a blog subject for another day.

For a single node machine it’s simple – Proxmox is Debian under the hood so simply follow the usual Debian process:

Change the hostname file to contain your new hostname

vi /etc/hostname

To save having to restart, you can also run the hostname command to implement the change on a running machine. Otherwise you’ll need to reboot your server to pick up the change.

hostname [NEW_HOSTNAME]

Next, edit your /etc/hostname file and change all occurrences of the old hostname with the new one.


OBI formatting grand totals – without XML!

Get Social!

Recently I needed to have some conditionally formated grand total rows in OBI. I remember years ago hacking around with the XML which is far from ideal. I then realised it could be done another way, here is the example:

 

Create an analysis with a measure column:

Apply the conditional format on the Actual % column

This should result in the following analysis

This is the special bit, add a new calculated item on the column where you want the total

Select the Function (aggregation Rule) and add in all of the values

Now the totals will be applied to the analysis

You can format this calculated row using the format option on the calculated item window


Visit our advertisers

Quick Poll

Do you use ZFS on Linux?

Visit our advertisers