Mount A loop Device In An OpenVZ Container

Mount A loop Device In An OpenVZ Container

Category : How-to

Get Social!

You can pass a loop device through to an OpenVZ container with the vzctl command. You’ll need to mount the loop device on your host, as there is no support within an OpenVZ container to mount the device within the container. This means that the source of the loop device will also need to be available on the guest.

Note: This has been disabled in the latest versions of vzctl.

The device will then be passed through to the container so that it can be used by the container.

Run the vzctl command with the Container ID and loop device name that you’d like to use.

vzctl set [CTID] --devnodes [LOOP]:rw

For example, to pass loop0 through to Container ID 100 use:

vzctl set 100 --devnodes loop0:rw

Bash Command to Check Container Disk Space in Proxmox/ OpenVZ

Get Social!

proxmox logo gradKeeping an eye on all of your containers disk space can be time consuming if done one by one. Luckily, the vzlist command gives us access to many of the data and counters associated with each container.

Running vzlist on it’s own will give a list of the currently running containers and a few extra columns of information such as IP address and hostname.

vzlist
CTID NPROC STATUS  IP_ADDR     HOSTNAME
100  27    running 10.10.10.10 test.jamescoyle.net

You might be thinking that the above command doesn’t show anything about containers which are currently turned off, or about disk space. You’d be right!

Adding the –all switch will include all containers, regardless of their running state.

To add the disk space information we add the –output switch followed by the column names we want to display. For disk space, the column name is helpfully named diskspace so the command will look like this:

vzlist --all --output diskspace
DSPACE
1184648

We now see the disk space of all of our containers, but with a problem. We have no idea (although in this example we do because we only have one container) which container the disk space output is for. We need to add a few more columns to the –output switch such as ctid and hostname.  (see below for a complete list of output columns)

vzlist --all --output ctid,hostname,diskspace
CTID HOSTNAME             DSPACE
100  test.jamescoyle.net  1184660

The last trick here is to add a sort using the –sort switch and the column name

vzlist --all --output ctid,hostname,diskspace --sort diskspace
CTID HOSTNAME             DSPACE
100  test.jamescoyle.net  1184660

Let’s put all that together, plus a spot of awk magic to get a nice disk full percentage that we can work with:

vzlist --all --output ctid,hostname,diskspace,diskspace.s,diskspace.h --sort diskspace | awk '{if (NR>1) {printf("%-4s %-30s %-10s %-10s %-10s %d\n", $1, $2, $3, $4, $5, $3/$4*100)} else printf("%-4s %-30s %-10s %-10s %-10s %s\n", $1, $2, $3, $4, $5, "PERC_USED")}'

Output:

CTID HOSTNAME            DSPACE  DSPACE.S DSPACE.H PERC_USED
100  test.jamescoyle.net 1184684 5242880  5767168  22

Complete list of vzlist column headings

You can get a complete list of vzlist column headings with the following command:

vzlist -L
ctid            CTID
veid            CTID
vpsid           CTID
private         PRIVATE
root            ROOT
mount_opts      MOUNT_OPTS
origin_sample   ORIGIN_SAMPLE
hostname        HOSTNAME
name            NAME
smart_name      SMARTNAME
description     DESCRIPTION
ostemplate      OSTEMPLATE
ip              IP_ADDR
nameserver      NAMESERVER
searchdomain    SEARCHDOMAIN
status          STATUS
kmemsize        KMEMSIZE
kmemsize.m      KMEMSIZE.M
kmemsize.b      KMEMSIZE.B
kmemsize.l      KMEMSIZE.L
kmemsize.f      KMEMSIZE.F
lockedpages     LOCKEDP
lockedpages.m   LOCKEDP.M
lockedpages.b   LOCKEDP.B
lockedpages.l   LOCKEDP.L
lockedpages.f   LOCKEDP.F
privvmpages     PRIVVMP
privvmpages.m   PRIVVMP.M
privvmpages.b   PRIVVMP.B
privvmpages.l   PRIVVMP.L
privvmpages.f   PRIVVMP.F
shmpages        SHMP
shmpages.m      SHMP.M
shmpages.b      SHMP.B
shmpages.l      SHMP.L
shmpages.f      SHMP.F
numproc         NPROC
numproc.m       NPROC.M
numproc.b       NPROC.B
numproc.l       NPROC.L
numproc.f       NPROC.F
physpages       PHYSP
physpages.m     PHYSP.M
physpages.b     PHYSP.B
physpages.l     PHYSP.L
physpages.f     PHYSP.F
vmguarpages     VMGUARP
vmguarpages.m   VMGUARP.M
vmguarpages.b   VMGUARP.B
vmguarpages.l   VMGUARP.L
vmguarpages.f   VMGUARP.F
oomguarpages    OOMGUARP
oomguarpages.m  OOMGUARP.M
oomguarpages.b  OOMGUARP.B
oomguarpages.l  OOMGUARP.L
oomguarpages.f  OOMGUARP.F
numtcpsock      NTCPSOCK
numtcpsock.m    NTCPSOCK.M
numtcpsock.b    NTCPSOCK.B
numtcpsock.l    NTCPSOCK.L
numtcpsock.f    NTCPSOCK.F
numflock        NFLOCK
numflock.m      NFLOCK.M
numflock.b      NFLOCK.B
numflock.l      NFLOCK.L
numflock.f      NFLOCK.F
numpty          NPTY
numpty.m        NPTY.M
numpty.b        NPTY.B
numpty.l        NPTY.L
numpty.f        NPTY.F
numsiginfo      NSIGINFO
numsiginfo.m    NSIGINFO.M
numsiginfo.b    NSIGINFO.B
numsiginfo.l    NSIGINFO.L
numsiginfo.f    NSIGINFO.F
tcpsndbuf       TCPSNDB
tcpsndbuf.m     TCPSNDB.M
tcpsndbuf.b     TCPSNDB.B
tcpsndbuf.l     TCPSNDB.L
tcpsndbuf.f     TCPSNDB.F
tcprcvbuf       TCPRCVB
tcprcvbuf.m     TCPRCVB.M
tcprcvbuf.b     TCPRCVB.B
tcprcvbuf.l     TCPRCVB.L
tcprcvbuf.f     TCPRCVB.F
othersockbuf    OTHSOCKB
othersockbuf.m  OTHSOCKB.M
othersockbuf.b  OTHSOCKB.B
othersockbuf.l  OTHSOCKB.L
othersockbuf.f  OTHSOCKB.F
dgramrcvbuf     DGRAMRB
dgramrcvbuf.m   DGRAMRB.M
dgramrcvbuf.b   DGRAMRB.B
dgramrcvbuf.l   DGRAMRB.L
dgramrcvbuf.f   DGRAMRB.F
numothersock    NOTHSOCK
numothersock.m  NOTHSOCK.M
numothersock.b  NOTHSOCK.B
numothersock.l  NOTHSOCK.L
numothersock.f  NOTHSOCK.F
dcachesize      DCACHESZ
dcachesize.m    DCACHESZ.M
dcachesize.b    DCACHESZ.B
dcachesize.l    DCACHESZ.L
dcachesize.f    DCACHESZ.F
numfile         NFILE
numfile.m       NFILE.M
numfile.b       NFILE.B
numfile.l       NFILE.L
numfile.f       NFILE.F
numiptent       NIPTENT
numiptent.m     NIPTENT.M
numiptent.b     NIPTENT.B
numiptent.l     NIPTENT.L
numiptent.f     NIPTENT.F
swappages       SWAPP
swappages.m     SWAPP.M
swappages.b     SWAPP.B
swappages.l     SWAPP.L
swappages.f     SWAPP.F
diskspace       DSPACE
diskspace.s     DSPACE.S
diskspace.h     DSPACE.H
diskinodes      DINODES
diskinodes.s    DINODES.S
diskinodes.h    DINODES.H
laverage        LAVERAGE
uptime          UPTIME
cpulimit        CPULIM
cpuunits        CPUUNI
cpus            CPUS
ioprio          IOP
onboot          ONBOOT
bootorder       BOOTORDER
layout          LAYOUT
features        FEATURES
vswap           VSWAP
disabled        DISABL

 


Proxmox Error: (warning) Incorrect quota shutdown for id xxx recalculating disk usage

Category : How-to

Get Social!

openvz-logo-150px_new_3I came across this error today when trying to manually backup the container to create an OpenVZ container template.

It occurred, in my case, when trying to start the container VMID 600 after been playing around within it’s root folder from the host’s terminal. The following error was presented:

Starting container ...
vzquota : (warning) Incorrect quota shutdown for id 600, recalculating disk usage
vzquota : (error) quota check : lstat `EelIMf': Input/output error
vzquota on failed [1]
TASK ERROR: command 'vzctl start 600' failed: exit code 60

To fix the issue, we need to turn off the quota for the VMID and then back on again.

vzquota off 600

The output should look like below:

vzquota : (error) Quota is not running for id 600
vzquota : (warning) Repairing quota: it was incorrectly marked as running for id 600

You then need to turn the quota back on, to ensure your container’s disks are restricted to the size you have set for it.

vzquota on 600

You should now be able to successfully start the container, error free!


Move Proxmox Container to Different Storage

Get Social!

2015-03-05 00_18_04-Proxmox Virtual Environment storageA task often required when new storage is added or removed, and containers grow over time is to move a container onto another storage device.

The  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.

The below script allows you to move an OpenVZ 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
#
#

# 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 vzlist | fgrep -w -q " $CT "
then
    RUNNING=true
fi

if $RUNNING
then
    vzctl stop $CT
fi

vzdump --dumpdir $TMP $CT

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

vzrestore $ARCHIVE $CT -force -storage $TARGET_STORAGE

if $RUNNING
then
    vzctl start $CT
fi

if $DELETE
then
    LOG=$(ls -t $TMP/vzdump-openvz-$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

 


OpenVZ USB Passthrough in Proxmox

Get Social!

usb-logoUSB Passthrough is the concept of passing a USB device that is plugged into the host server to a guest. This post will detail how to make the USB device available in an OpenVZ container.

The first step is usually the easiest; take your USB device and plug it into your Proxmox host. Wait a few moments for it to be recognised and then run the below command to identify which bus the device is plugged into.

lsusb

The below shows an example output of the command.

Bus 002 Device 003: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

The device I’m after for this example is on Bus 002 Device 003 which I identified from the name. Yours will more than likely be different

Often the names of the devices mean little and you’ll then need to compare the  output of lsusb before and after plugging the device in. For example, run the below command with the device unplugged.

lsusb > /tmp/before

Then plug the device in and run the following command:

lsusb > /tmp/after

Finally compare the two command outputs and identify the difference:

diff /tmp/before /tmp/after

Once you had identified the row in the output of lsusb it’s  time to make a note of the usb bus it’s attached to. As above, mine is 002:003, which are the details I’ll need in a moment.

The way Linux accesses most IO devices is through the /dev/ mounts, and USB devices are no different. You can navigate to the USB bus in /dev/bus/ by following the bus address.

/dev/bus/usb/002/003

As you can see, the above path starts with /dev/bus/usb/ and follows with the bus address in the lsusb command. We’ll need this to pass to our OpenVZ container in just a moment so make a note of it.

One final check we can do to make sure we have the correct bus address is to use the above path with lsusb -D to get a more detailed output of the device. This will usually contain a manufacture ID and other information that may help us identify the device we need. I’ll spare you the output, but an example is below:

lsusb -D /dev/bus/usb/002/003

The next step is to tell the OpenVZ container all about the USB devices bus location. Using vzctl set we can specify the OpenVZ container ID, USB device and access level.

Before continuing, make sure that the container is turned off.

The command to run on the turned off container is below. Make sure to substitute the bracketed values as follows:

  • [VMID] is the ID of the openVZ container that the USB device will be passed to.
  • [BUS_PATH] is the path to the USB device that we discovered earlier. Be sure to drop the /dev/ prefix.
vzctl set [VMID] --devnode [BUS_PATH]:rw --save

For example:

vzctl set 107 --devnode bus/usb/002/003:rw --save

And there it is, turn on the container and point your application to /dev/bus/usb/002/003 and you should be up and running.


Create a Ubuntu 14.04 OpenVZ Template for Proxmox

Get Social!

proxmox logo gradThe latest Ubuntu long term support is now available, called Ubuntu 14.04.

There isn’t currently a template available over on OpenVZ however I’m sure that will be shortly rectified. In the meantime, however, you can use the below steps to create a 14.04 Ubuntu template for OpenVZ/ Proxmox. This template has only been lightly tested so please report any errors as you find them.

This template is BETA, please report any problems in the comments.

Vistit Downloads Page

 

You can download a pre-created VM from here directly, or you can create your own using the below notes.

 

Before continuing, this guide assumes that you already have an installation of Ubuntu up and running which you can SSH to. This could be either a KVM or physical machine.

We will use debootstrap to create the template so make sure it’s installed and install it if you haven’t already.

apt-get install -y debootstrap

Use debootstrap to download and configure all the required packages to a temporary directory. For this example, we’ll use /tmp/deb.

debootstrap --arch amd64 trusty /tmp/deb ftp://ftp.ubuntu.com/ubuntu

Copy the below script into the tmp directory of the template root which has just been created. For this example you’ll need to copy the text into this path:

vi /tmp/deb/tmp/client.sh
#!/bin/bash

echo "root:password" | chpasswd

apt-get update

apt-get purge -y console-setup ntpdate whiptail eject ureadahead sudo vim-tiny rsync
apt-get install -y vim openssh-server

find / -name *ondemand -exec rm -rf {} \;
rm -f /etc/init/console* /etc/init/tty*

sed -i -e 's/^\$ModLoad imklog/#\$ModLoad imklog/g' /etc/rsyslog.conf
sed -i -e 's@\(space:\)\(/var/log/\)@\1-\2@' /etc/rsyslog.d/*.conf
sed -i -e 's/^\#cron./cron./g' /etc/rsyslog.d/50-default.conf

sed -i -e 's/^\console output/#console output/g' /etc/init/rc.conf
sed -i -e 's/^\env INIT_VERBOSE/#env INIT_VERBOSE/g' /etc/init/rc.conf

locale-gen en_US.UTF-8
locale-gen en_GB.UTF-8
dpkg-reconfigure locales

cp /usr/share/zoneinfo/Europe/London /etc/localtime

cat <<EOF > /etc/init/tty1.conf
# tty1 - getty
#
# This service maintains a getty on tty1 from the point the system is
# started until it is shut down again.

start on stopped rc RUNLEVEL=[2345]

stop on runlevel [!2345]

respawn
exec /sbin/getty -8 38400 tty1
EOF

rm -f /etc/ssh/ssh_host_*

cat << EOF > /etc/init.d/generate_ssh_keys
#!/bin/bash
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''
rm -f \$0
EOF

chmod a+x /etc/init.d/generate_ssh_keys
update-rc.d generate_ssh_keys defaults

apt-get clean
find /var/ -name *.log -exec rm -rf {} \;
rm -rf /boot /dev /media /opt /run /srv /tmp /root/.bash_history /root/.viminfo /etc/ssh/ssh_host_*
mkdir /dev /run /tmp
touch /dev/null

exit

Make the script runnable which chmod.

chmod +x /tmp/deb/tmp/client.sh

Run the above script using the chroot command to set up the template.

chroot /tmp/deb /tmp/client.sh

The script will now run and set up the template using /tmp/deb/ as the templates root.

Once completed, create an archive of the template root device and install it on your OpenVZ/ Proxmox server.

cd /tmp/deb
tar -czpf /tmp/ubuntu-14.04-x86_64-initial1.tar.gz .

Copy the /tmp/ubuntu-14.04-x86_64-initial1.tar.gz file to your cache directory of your Proxmox install and create your first Ubuntu 14.04 container!

proxmox-ubuntu-1404-template


Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers