如何知道我从哪里启动 [英] How to know where I am booting from
问题描述
我开发了一个可以在多个引导磁盘为'/dev/nftla1'或'dev/hdc1'的平台上运行的应用程序. Grub引导加载程序具有此信息.
I have an application developed to run on multiple platforms where the boot disk is either '/dev/nftla1' or 'dev/hdc1'. The Grub bootloader has this information.
但是一旦内核接管&该应用程序开始运行,它变得无关紧要.但是,在我的应用程序(主要位于'C'中)中,我想知道启动源,因为在这些平台上访问文件的方式不同.
But once the kernel takes over & the application starts running, it becomes irrelevant. But, in my application which is mainly in 'C', I would like to know the source of boot because the way the files are accessed is different on these platforms.
我的问题是:是否有系统命令或任何您知道可以帮助我实现目标的技巧?
My question is: Is there a system command or any tricks that you know that could help with what I am trying to achieve?
推荐答案
您可以通过grub传递内核引导选项,然后进行检查.
You can pass kernel boot options from grub and then check them.
cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-2.6.32-33-generic root=UUID=3c231d1a-b6cb-4526-95fe-eb8984c7a91a ro quiet splash
更多信息.
更新:
您可以使用此C代码来解析/proc/cmdline
:
UPDATE:
You can use this C code to parse /proc/cmdline
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int parse_option(const char *line, const char *option, char *value, size_t size)
{
const char *p0, *p1;
int len;
p0 = strstr(line, option);
if (!p0)
return 0;
p0 += strlen(option);
p1 = strchr(p0, ' ');
if (!p1)
p1 = p0 + strlen(p0);
len = p1 - p0;
if (len > size - 1)
len = size - 1;
memcpy(value, p0, len);
value[len] = '\0';
return len;
}
void get_cmdline_option(const char *option, char *value, size_t size)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
size_t read;
if (!size)
return;
*value = '\0';
fp = fopen("/proc/cmdline", "r");
if (fp == NULL)
return;
while ((read = getline(&line, &len, fp)) != -1) {
printf("%s", line);
if (parse_option(line, option, value, size))
break;
}
fclose(fp);
if (line)
free(line);
return;
}
int main(int argc, char **argv)
{
char root[128];
get_cmdline_option("root=", root, sizeof(root));
printf("root='%s'\n", root);
return 0;
}
这篇关于如何知道我从哪里启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!