~serge-hallyn/ubuntu/raring/libvirt/libvirt-hugepages

« back to all changes in this revision

Viewing changes to tools/virt-host-validate-common.c

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-13 15:44:12 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20120513154412-fgmn5sxqdzgnzlx3
Tags: 0.9.12-0ubuntu1
* New upstream version:
  * Synchronize with debian packaging:
    - debian/control: Update build depends.
    - debian/libvirt-bin.postrm: Cleanup /var/log/libvirt
      on purge.
    - Bump standards verson (no changes).
    - debian/patches/Don-t-fail-if-we-can-t-setup-avahi.patch: Added
  * Dropped patches:
    - debian/patches/Debianize-libvirt-guests.patch
    - debian/patches/rewrite-lxc-controller-eof-handling-yet-again
    - debian/patches/ubuntu/libnl13.patch
    - debian/patches/ubuntu/fix-lxc-startup-error.patch
    - debian/patches/ubuntu/fix-bridge-fd.patch
    - debian/patches/ubuntu/skip-labelling-network-disks.patch
    - debian/patches/ubuntu/xen-xend-shutdown-detection.patch
    - debian/patches/ubuntu/xen-config-no-vfb-for-hvm.patch
    - debian/patches/debian/Disable-daemon-start-test.patch
    - debian/patches/debian/Disable-gnulib-s-test-nonplocking-pipe.sh.patch
    - debian/patches/ubuntu/9006-default-config-test-case.patch
    - debian/patches/fix-block-migration.patch
    - debian/patches/ubuntu/9022-qemu-unescape-HMP-commands-before-converting-them-to.patch
    - debian/patches/ubuntu/9023-qemu-change-rbd-auth_supported-separation-character-.patch
    - debian/patches/ubuntu/9024-qemu-allow-snapshotting-of-sheepdog-and-rbd-disks.patch
    - debian/patches/9025-qemu-change-rbd-auth_supported-separation-character-.patch
    - debian/patches/ubuntu/arm-gcc-workaround.patch
  * Rediffed:
    - debian/patches/Allow-libvirt-group-to-access-the-socket.patch
    - debian/patches/Disable-failing-virnetsockettest.patch
    - debian/patches/dnsmasq-as-priv-user
    - debian/patches/9002-better_default_uri_virsh.patch
  * debian/control: Add libnl-route-3-dev ass a build depends.
  * debian/patches/libnl3-build-fix.patch: Fix build with libnl3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * virt-host-validate-common.c: Sanity check helper APIs
 
3
 *
 
4
 * Copyright (C) 2012 Red Hat, Inc.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
19
 *
 
20
 */
 
21
 
 
22
#include <config.h>
 
23
 
 
24
#include <stdarg.h>
 
25
#include <stdlib.h>
 
26
#include <stdio.h>
 
27
#include <unistd.h>
 
28
#include <sys/utsname.h>
 
29
 
 
30
#include "util.h"
 
31
#include "memory.h"
 
32
#include "virfile.h"
 
33
#include "virt-host-validate-common.h"
 
34
 
 
35
static bool quiet;
 
36
 
 
37
void virHostMsgSetQuiet(bool quietFlag)
 
38
{
 
39
    quiet = quietFlag;
 
40
}
 
41
 
 
42
void virHostMsgCheck(const char *prefix,
 
43
                     const char *format,
 
44
                     ...)
 
45
{
 
46
    va_list args;
 
47
    char *msg;
 
48
 
 
49
    if (quiet)
 
50
        return;
 
51
 
 
52
    va_start(args, format);
 
53
    if (virVasprintf(&msg, format, args) < 0) {
 
54
        perror("malloc");
 
55
        abort();
 
56
    }
 
57
    va_end(args);
 
58
 
 
59
    fprintf(stdout, _("%6s: Checking %-60s: "), prefix, msg);
 
60
    VIR_FREE(msg);
 
61
}
 
62
 
 
63
static bool virHostMsgWantEscape(void)
 
64
{
 
65
    static bool detectTty = true;
 
66
    static bool wantEscape = false;
 
67
    if (detectTty) {
 
68
        if (isatty(STDOUT_FILENO))
 
69
            wantEscape = true;
 
70
        detectTty = false;
 
71
    }
 
72
    return wantEscape;
 
73
}
 
74
 
 
75
void virHostMsgPass(void)
 
76
{
 
77
    if (quiet)
 
78
        return;
 
79
 
 
80
    if (virHostMsgWantEscape())
 
81
        fprintf(stdout, "\033[32m%s\033[0m\n", _("PASS"));
 
82
    else
 
83
        fprintf(stdout, "%s\n", _("PASS"));
 
84
}
 
85
 
 
86
 
 
87
static const char * failMessages[] = {
 
88
    N_("FAIL"),
 
89
    N_("WARN"),
 
90
    N_("NOTE"),
 
91
};
 
92
 
 
93
verify(ARRAY_CARDINALITY(failMessages) == VIR_HOST_VALIDATE_LAST);
 
94
 
 
95
static const char *failEscapeCodes[] = {
 
96
    "\033[31m",
 
97
    "\033[33m",
 
98
    "\033[34m",
 
99
};
 
100
 
 
101
verify(ARRAY_CARDINALITY(failEscapeCodes) == VIR_HOST_VALIDATE_LAST);
 
102
 
 
103
void virHostMsgFail(virHostValidateLevel level,
 
104
                    const char *hint)
 
105
{
 
106
    if (virHostMsgWantEscape())
 
107
        fprintf(stdout, "%s%s\033[0m (%s)\n",
 
108
                failEscapeCodes[level], _(failMessages[level]), hint);
 
109
    else
 
110
        fprintf(stdout, "%s (%s)\n",
 
111
                _(failMessages[level]), hint);
 
112
}
 
113
 
 
114
 
 
115
int virHostValidateDevice(const char *hvname,
 
116
                          const char *dev_name,
 
117
                          virHostValidateLevel level,
 
118
                          const char *hint)
 
119
{
 
120
    virHostMsgCheck(hvname, "for device %s", dev_name);
 
121
 
 
122
    if (access(dev_name, R_OK|W_OK) < 0) {
 
123
        virHostMsgFail(level, hint);
 
124
        return -1;
 
125
    }
 
126
 
 
127
    virHostMsgPass();
 
128
    return 0;
 
129
}
 
130
 
 
131
 
 
132
bool virHostValidateHasCPUFlag(const char *name)
 
133
{
 
134
    FILE *fp = fopen("/proc/cpuinfo", "r");
 
135
    bool ret = false;
 
136
 
 
137
    if (!fp)
 
138
        return false;
 
139
 
 
140
    do {
 
141
        char line[1024];
 
142
 
 
143
        if (!fgets(line, sizeof(line), fp))
 
144
            break;
 
145
 
 
146
        if (strstr(line, name)) {
 
147
            ret = true;
 
148
            break;
 
149
        }
 
150
    } while (1);
 
151
 
 
152
    VIR_FORCE_FCLOSE(fp);
 
153
 
 
154
    return ret;
 
155
}
 
156
 
 
157
 
 
158
int virHostValidateLinuxKernel(const char *hvname,
 
159
                               int version,
 
160
                               virHostValidateLevel level,
 
161
                               const char *hint)
 
162
{
 
163
    struct utsname uts;
 
164
    unsigned long thisversion;
 
165
 
 
166
    uname(&uts);
 
167
 
 
168
    virHostMsgCheck(hvname, _("for Linux >= %d.%d.%d"),
 
169
                    ((version >> 16) & 0xff),
 
170
                    ((version >> 8) & 0xff),
 
171
                    (version & 0xff));
 
172
 
 
173
    if (STRNEQ(uts.sysname, "Linux")) {
 
174
        virHostMsgFail(level, hint);
 
175
        return -1;
 
176
    }
 
177
 
 
178
    if (virParseVersionString(uts.release, &thisversion, true) < 0) {
 
179
        virHostMsgFail(level, hint);
 
180
        return -1;
 
181
    }
 
182
 
 
183
    if (thisversion < version) {
 
184
        virHostMsgFail(level, hint);
 
185
        return -1;
 
186
    } else {
 
187
        virHostMsgPass();
 
188
        return 0;
 
189
    }
 
190
}