vzctl: allow upstream to safely stop container when setns is available

The test preventing the execution of in-container reboot right now just
checks whether or not we are running an upstream Linux Kernel. However,
it is perfectly possible to gracefuly stop the container if joining the
pid namespace is possible.

Update the test to reflect that.

Signed-off-by: Glauber Costa <glommer@parallels.com>
hooks_ct: fix pivot_root failure with mount namespaces.

Not only we don't need to call pivot_root when joining a mount namespace, we
can't do it. This is because the filesystem we currently are will become
invisible after we join the mount namespace. As a side effect, we are fully
protected because we will only have the filesystem view of the newly joined
namespace.

Because setns support for mount namespaces are not always present, we need
a flag do determine whether or not we've joined it. And because this is not
needed outside this scope at all, we can resort to a local only flag instead
of storing this information in the vps_handler

Signed-off-by: Glauber Costa <glommer@parallels.com>
ct_setcpus(): fix a comparison

Noticed the following warning when compiling vzctl for 32-bit:

	hooks_ct.c: In function 'ct_setcpus':
	hooks_ct.c:373: warning: comparison is always true due to limited range of data type

Apparently we have

	unsigned long max_lim = ~0UL;

so the comparison should be with ~0UL as well, not with ~0ULL as it was.

Introduced by commit f802781.

Cc: Glauber Costa <glommer@openvz.org>
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzctl snapshot-list -o desc: fix width

Fix formatting when printing description field in vzctl snapshot-list.
Obviously, since we use 32 characters for a header, we should use
same 32 characters for the value, not 38.

Before this patch, it looks something like this:

 # vzctl snapshot-list 1041 -o current,desc,current
 C DESC                             C
 *                                        *

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzctl snapshot-list -o device: fix width

Fix formatting when printing device field in vzctl snapshot-list.

Obviously, since we use 16 characters for a header, we should use
same 16 characters for the value, not 32.

Before this patch, it looks something like this:

 # vzctl snapshot-list 1041 -o current,device,current
 C DEVICE           C
 *                                  *

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: fix ploop-based CT migration wrt symlinks

If VE_ROOT or VE_PRIVATE is a symlink (or there's a symlink in one of
it's components), function get_ploop_info() is not working, since kernel
displays canonicalized paths in /proc/mounts and other files.

For example, if /vz is a symlink to /var/lib/vz and VE_ROOT is
/vz/root/$VEID, get_ploop_info() will fail.

To fix, canonicalize VE_ROOT and VE_PRIVATE in get_ploop_info() using
readlink -f.

Proper solution would be to add required fields to vzlist, something
like -o device, top_delta etc. But this commit fixes the problem.

https://bugzilla.openvz.org/2475

Reported-by: Paparaciz <helpaz@gmail.com>
Reported-by: Kalin Bogatzevski <kalin@bul.net>
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: fix a few messages

(1) Do -> Doing (in a few places)
(2) don't -> does not

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: rewrite/fix IP address check

First, checking IPs one by one is not very efficient, so let's optimize,
rewriting this code to check all IPs at once, by making a A|B|C regexp
out of them.

Second, any IPs with netmasks present would not be detected -- for this
to work we need to remove netmasks from IPs if there are any.

This patch does both.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: do ploop_get_info earlier

Move call to ploop_get_info earlier in the code, so we fail earlier
and don't have to undo anything.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: fix checking rsync/vzctl exit code

Well well well... This is a nice example that shows that one
should not touch something that works, or it might stop working.

We had this code in vzmigrate:

 logexec 2 $SSH root@$host vzctl set $VEID --applyconfig_map name --save
 # vzctl return code 20 or 21 in case of unrecognized option
 if [[ $? != 20 && $? != 21 && $? != 0 ]]; then

that apparently used bash-specific [[ ]] operator. It was "fixed"
by commit ebd5fb0 and after the fix the if statement became:

 if [ $? != 20 && $? != 21 && $? != 0 ]; then

That, of course, led to the error:

 /usr/sbin/vzmigrate: line 382: [: missing `]'

which was "fixed" by commit 5783162, and the if statement became:

 if [ $? != 20 ] && [ $? != 21 ] && [ $? != 0 ]; then

It fixed the "missing `]'" error but broke the code completely. Why?

Just because after the first [ ] operator the value of $? became 0.
So, apparently, we have managed to remove the error check at all.

Later, a similar buggy code was added in commit b9d4b8c to add rsync
exit code of 24 to exclude list. Well, it broke the error check, too.

First rsync pass check was broken since July 2010 (vzctl 3.0.25).

vzctl set name check was broken since Nov 2007 (vzctl 3.0.23).

This patch fixes both places. Or so I hope.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: add -o BatchMode=yes to SSH_OPTIONS

It should be there by default, we are not expecting interactive ssh to
work for us. It was already there in a few ssh invocations, but not
in all of them.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzcptcheck: added

This utility is to be used by vzmigrate to do some checks before
migration. See man page for more details.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
cgroup.c: fix a compiler warning

Got this trying to compile vzctl:

	  CC     cgroup.lo
	cgroup.c: In function 'container_apply_config':
	cgroup.c:109:4: warning: passing argument 3 of 'cgroup_get_value_uint64'
	from incompatible pointer type [enabled by default]
	/usr/include/libcgroup/groups.h:432:5: note: expected 'u_int64_t *' but
	argument is of type 'long unsigned int *'

Apparently we need to use u_int64_t here, like in other similar places.

Introduced in commit a6e1fda.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: add checks based on vzcptcheck

In case of live migration, we want to check that
 - CPT version on src and dest nodes are compatible
 - CPU capabilities on dest node are adeqate for given CT

Checks are performed using vzcptcheck(8) utility. If it is absent from
either src or dest nodes, a warning is printed and migration is
continued without checks (as it was the case before this patch).

Should fix #2461
https://bugzilla.openvz.org/2461

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: add --check-only (aka --dry-run)

Usable to just do preliminary migration checks.

Fix the man page accordingly.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>
vzmigrate: fix "Starting..." message

In case we are running with --check-only, saying
"Starting [live] migration" is misleading, so fix the text accordingly.

Signed-off-by: Kir Kolyshkin <kir@openvz.org>