2014-01-30 07:06:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2020-02-16 21:59:28 +00:00
|
|
|
#include <unistd.h>
|
2014-01-30 07:06:37 +00:00
|
|
|
|
|
|
|
#include "virscsi.h"
|
|
|
|
#include "testutils.h"
|
2014-03-29 10:58:32 +00:00
|
|
|
#include "virlog.h"
|
2014-01-30 07:06:37 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
#define VIR_SCSI_DATA "/virscsidata"
|
|
|
|
|
2014-03-29 10:58:32 +00:00
|
|
|
VIR_LOG_INIT("tests.scsitest");
|
|
|
|
|
2014-10-28 18:38:04 +00:00
|
|
|
static char *virscsi_prefix;
|
2014-01-30 07:06:37 +00:00
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
test1(const void *data G_GNUC_UNUSED)
|
2014-01-30 07:06:37 +00:00
|
|
|
{
|
2021-09-04 20:37:31 +00:00
|
|
|
g_autofree char *name = NULL;
|
2014-01-30 07:06:37 +00:00
|
|
|
|
|
|
|
if (!(name = virSCSIDeviceGetDevName(virscsi_prefix,
|
|
|
|
"scsi_host1", 0, 0, 0)))
|
|
|
|
return -1;
|
|
|
|
|
2014-01-30 11:48:23 +00:00
|
|
|
if (STRNEQ(name, "sdh"))
|
2021-09-04 20:41:36 +00:00
|
|
|
return -1;
|
2014-01-30 07:06:37 +00:00
|
|
|
|
2021-09-04 20:41:36 +00:00
|
|
|
return 0;
|
2014-01-30 07:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Two test devices are used, one has address "0:0:0:0", the
|
|
|
|
* other has address "1:0:0:0", see "virscsidata/" for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
test2(const void *data G_GNUC_UNUSED)
|
2014-01-30 07:06:37 +00:00
|
|
|
{
|
2021-08-20 13:53:53 +00:00
|
|
|
g_autoptr(virSCSIDeviceList) list = NULL;
|
2021-03-11 07:16:13 +00:00
|
|
|
virSCSIDevice *dev = NULL;
|
|
|
|
virSCSIDevice *dev1 = NULL;
|
2014-01-30 07:06:37 +00:00
|
|
|
bool free_dev = true;
|
|
|
|
bool free_dev1 = true;
|
2021-03-11 07:16:13 +00:00
|
|
|
virSCSIDevice *tmp = NULL;
|
2021-09-04 20:37:31 +00:00
|
|
|
g_autofree char *sgname = NULL;
|
2014-01-30 07:06:37 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
sgname = virSCSIDeviceGetSgName(virscsi_prefix,
|
|
|
|
"scsi_host1", 0, 0, 0);
|
|
|
|
|
2014-01-30 11:48:23 +00:00
|
|
|
if (!sgname || STRNEQ(sgname, "sg8"))
|
2014-01-30 07:06:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(dev = virSCSIDeviceNew(virscsi_prefix, "scsi_host1",
|
|
|
|
0, 0, 0, false, true)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (STRNEQ_NULLABLE(virSCSIDeviceGetName(dev), "1:0:0:0") ||
|
|
|
|
virSCSIDeviceGetAdapter(dev) != 1 ||
|
|
|
|
virSCSIDeviceGetBus(dev) != 0 ||
|
|
|
|
virSCSIDeviceGetTarget(dev) != 0 ||
|
|
|
|
virSCSIDeviceGetUnit(dev) != 0 ||
|
|
|
|
virSCSIDeviceGetReadonly(dev) ||
|
|
|
|
!virSCSIDeviceGetShareable(dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!virSCSIDeviceIsAvailable(dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
if (virSCSIDeviceSetUsedBy(dev, "QEMU", "fc18") < 0)
|
2014-01-30 07:06:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSCSIDeviceIsAvailable(dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
if (virSCSIDeviceSetUsedBy(dev, "QEMU", "fc20") < 0)
|
2014-01-30 07:06:37 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSCSIDeviceIsAvailable(dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(list = virSCSIDeviceListNew()))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSCSIDeviceListAdd(list, dev) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* virSCSIDeviceListDispose will take care of freeing
|
|
|
|
* the device.
|
|
|
|
*/
|
|
|
|
free_dev = false;
|
|
|
|
|
|
|
|
if (!virSCSIDeviceListFind(list, dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
virSCSIDeviceListDel(list, dev, "QEMU", "fc20");
|
2014-01-30 07:06:37 +00:00
|
|
|
|
|
|
|
if (!virSCSIDeviceListFind(list, dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSCSIDeviceIsAvailable(dev))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSCSIDeviceListCount(list) != 1)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(dev1 = virSCSIDeviceNew(virscsi_prefix, "scsi_host0",
|
|
|
|
0, 0, 0, true, false)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virSCSIDeviceListAdd(list, dev1) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* virSCSIDeviceListDispose will take care of freeing
|
|
|
|
* the device.
|
|
|
|
*/
|
|
|
|
free_dev1 = false;
|
|
|
|
|
|
|
|
if (virSCSIDeviceListCount(list) != 2)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(tmp = virSCSIDeviceListSteal(list, dev1)))
|
|
|
|
goto cleanup;
|
|
|
|
virSCSIDeviceFree(tmp);
|
|
|
|
|
|
|
|
if (virSCSIDeviceListCount(list) != 1)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:44 +00:00
|
|
|
cleanup:
|
2014-01-30 07:06:37 +00:00
|
|
|
if (free_dev)
|
|
|
|
virSCSIDeviceFree(dev);
|
|
|
|
if (free_dev1)
|
|
|
|
virSCSIDeviceFree(dev1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-29 10:58:32 +00:00
|
|
|
static int
|
|
|
|
create_symlink(const char *tmpdir, const char *src_name, const char *dst_name)
|
|
|
|
{
|
2021-09-04 20:37:31 +00:00
|
|
|
g_autofree char *src_path = NULL;
|
|
|
|
g_autofree char *dst_path = NULL;
|
2014-03-29 10:58:32 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
src_path = g_strdup_printf("%s/%s", virscsi_prefix, src_name);
|
2014-03-29 10:58:32 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
dst_path = g_strdup_printf("%s/%s", tmpdir, dst_name);
|
2014-03-29 10:58:32 +00:00
|
|
|
|
|
|
|
if (symlink(src_path, dst_path) < 0) {
|
|
|
|
VIR_WARN("Failed to create symlink '%s' to '%s'", src_path, dst_path);
|
2021-09-04 20:41:36 +00:00
|
|
|
return -1;
|
2014-03-29 10:58:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:41:36 +00:00
|
|
|
return 0;
|
2014-03-29 10:58:32 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 07:06:37 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2014-03-29 10:58:32 +00:00
|
|
|
char *tmpdir = NULL;
|
|
|
|
char template[] = "/tmp/libvirt_XXXXXX";
|
2014-01-30 07:06:37 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
virscsi_prefix = g_strdup_printf("%s" VIR_SCSI_DATA, abs_srcdir);
|
2014-01-30 07:06:37 +00:00
|
|
|
|
2019-11-13 21:35:47 +00:00
|
|
|
tmpdir = g_mkdtemp(template);
|
2014-03-29 10:58:32 +00:00
|
|
|
|
|
|
|
if (tmpdir == NULL) {
|
|
|
|
VIR_WARN("Failed to create temporary directory");
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define CREATE_SYMLINK(src_name, dst_name) \
|
|
|
|
do { \
|
2014-03-29 10:58:32 +00:00
|
|
|
if (create_symlink(tmpdir, src_name, dst_name) < 0) { \
|
2017-11-03 12:09:47 +00:00
|
|
|
ret = -1; \
|
|
|
|
goto cleanup; \
|
|
|
|
} \
|
2014-03-29 10:58:32 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
CREATE_SYMLINK("0-0-0-0", "0:0:0:0");
|
|
|
|
CREATE_SYMLINK("1-0-0-0", "1:0:0:0");
|
|
|
|
CREATE_SYMLINK("sg0", "sg0");
|
|
|
|
CREATE_SYMLINK("sg8", "sg8");
|
|
|
|
|
|
|
|
VIR_FREE(virscsi_prefix);
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
virscsi_prefix = g_strdup(tmpdir);
|
2014-03-29 10:58:32 +00:00
|
|
|
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("test1", test1, NULL) < 0)
|
2014-01-30 07:06:37 +00:00
|
|
|
ret = -1;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("test2", test2, NULL) < 0)
|
2014-01-30 07:06:37 +00:00
|
|
|
ret = -1;
|
|
|
|
|
2014-03-25 06:53:44 +00:00
|
|
|
cleanup:
|
2015-09-15 20:33:36 +00:00
|
|
|
if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
|
2014-03-29 10:58:32 +00:00
|
|
|
virFileDeleteTree(tmpdir);
|
2014-01-30 07:06:37 +00:00
|
|
|
VIR_FREE(virscsi_prefix);
|
2021-05-16 16:14:53 +00:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2014-01-30 07:06:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|