[新增] 增加kdk_system_get_hostVirtType接口,获取当前宿主机是物理机还是虚拟机

This commit is contained in:
Debaucher 2022-05-09 20:12:46 +08:00
parent db7e843cb3
commit c0eb54f26a
4 changed files with 54 additions and 6 deletions

View File

@ -100,7 +100,7 @@ char* kdk_system_get_version(bool verbose)
int kdk_system_get_activationStatus(int *status_error_num,int *date_error_num)
{
if (NULL == status_error_num | NULL == date_error_num)
if (NULL == status_error_num || NULL == date_error_num)
{
klog_err("激活状态接口调用失败:参数错误\n");
return -1;
@ -200,7 +200,7 @@ int kdk_system_get_activationStatus(int *status_error_num,int *date_error_num)
}
else
dbus_message_iter_get_basic(&args, status_error_num);
klog_debug("激活状态:%d错误码%d\n",stat,*status_error_num);
// klog_debug("激活状态:%d错误码%d\n",stat,*status_error_num);
//释放status_pending_msg
dbus_message_unref(status_pending_msg);
@ -230,7 +230,7 @@ int kdk_system_get_activationStatus(int *status_error_num,int *date_error_num)
date_pending_msg = dbus_pending_call_steal_reply(date_pending);
if (NULL == date_pending_msg) {
klog_err(stderr, "date_pending_msgReply Null\n");
klog_err("date_pending_msgReply Null\n");
return -1;
}
if (!dbus_message_iter_init(date_pending_msg, &args)){
@ -306,7 +306,7 @@ data_free:
{
dbus_pending_call_unref(date_pending);
}
return 0;
}
char* kdk_system_get_serialNumber()
@ -320,6 +320,8 @@ char* kdk_system_get_serialNumber()
serial = get_val_from_file(fp, "key");
fclose(fp);
if (serial)
strskipspace(serial);
#else
int err;
serial = kylin_activation_get_serial_number(&err);
@ -327,7 +329,10 @@ char* kdk_system_get_serialNumber()
{
klog_err("序列号获取失败:%d\n", err);
}
else
{
strskipspace(serial);
}
return serial;
#endif // _KYLIN_ACTIVATION_H_
#endif // __linux__
@ -389,10 +394,35 @@ char* kdk_system_get_projectName()
project_codename = get_val_from_file(fp, "PROJECT_CODENAME");
fclose(fp);
}
if (project_codename)
strstripspace(project_codename);
#endif
return project_codename;
}
char* kdk_system_get_hostVirtType()
{
char *virtType = (char*)malloc(sizeof(char) * 65);
ASSERT_NOT_NULL(virtType, NULL);
#ifdef __linux__
char *cmdStr = "systemd-detect-virt"; // 准备运行的命令
FILE *pipeLine = popen(cmdStr, "r"); // 建立流管道
ASSERT_NOT_NULL(pipeLine, NULL);
if (fgets(virtType, 64 * sizeof(char), pipeLine) == NULL)
{
SAFE_FREE(virtType);
}
if (virtType)
strstripspace(virtType);
if (strcmp(virtType, "microsoft") == 0)
strcpy(virtType, "hyper-v");
else if (strcmp(virtType, "oracle") == 0)
strcpy(virtType, "orcale virtualbox");
pclose(pipeLine);
#endif
return virtType;
}
bool kdk_system_is_zyj(void)
{
bool bool_value = false;

View File

@ -82,6 +82,20 @@ extern char* kdk_system_get_eUser();
*/
extern char* kdk_system_get_projectName();
/**
* @brief 宿
*
* @return char* NULL
* [none, qemu, kvm, zvm, vmware, hyper-v, orcale virtualbox, xen, bochs, \
* uml, parallels, bhyve, qnx, arcn, openvz, lxc, lxc-libvirt, systemd-nspawn,\
* docker, podman, rkt, wsl]
* none
*/
extern char* kdk_system_get_hostVirtType();
extern char* kdk_system_get_hostCloudPlatform();
/**
* @brief
*

View File

@ -5,7 +5,7 @@
#include <stdbool.h>
/**
* @file systeminfo.hpp
* @file systeminfo.hpp使C语言接口
* @author liuyunhe (liuyunhe@kylinos.cn)
* @brief
* @version 0.1

View File

@ -38,5 +38,9 @@ int main()
free(res);
int zyj = kdk_system_is_zyj();
printf("专用机:%s\n", zyj == 0 ? "非专用机":"专用机");
res = kdk_system_get_virtType();
printf("虚拟机类型:%s\n", res);
free(res);
return 0;
}