How to know where I am booting from

2019-06-25 11:38发布

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.

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?

3条回答
相关推荐>>
2楼-- · 2019-06-25 12:01

You can get what you are looking for under /proc/mounts

For example:

$ grep \ /\  /proc/mounts 
rootfs / rootfs rw 0 0
/dev/disk/by-uuid/<uuid> / ext4 rw,noatime,user_xattr,barrier=1,data=ordered 0 0

instead of /dev/disk/by-uuid/<uuid>, it can be /dev/disk/by-label/<label>, /dev/disk/by-id/<id> or /dev/disk/by-path/<path>

and then

$ readlink /dev/disk/by-uuid/<uuid>
../../sda3

Hope the above is useful in your application.

查看更多
你好瞎i
3楼-- · 2019-06-25 12:06

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

More info.

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;                                                                                                                                                 
}
查看更多
迷人小祖宗
4楼-- · 2019-06-25 12:19

Check where / is mounted.

> mount | grep ' / '
/dev/sda1 on / type ext4 (rw,errors=remount-ro)

(Actually, from your description, I guess you should look where the directory you are reading from is mounted. That is not necessarily the boot drive.)

> df -h /tmp/
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             113G   13G   95G  12% /

EDIT

You're using rootfs, which means the above won't help you. (It also means I understand even less why you need to find out where you booted from, but let's leave that aside.)

Alexander already pointed to /proc/cmdline. Just for completeness, I thought I'd offer yet another option: You could look through the boot messages:

> dmesg | grep mounted
[    1.964952] EXT4-fs (sda1): mounted filesystem with ordered data mode

(Note that in the df command above, you should replace the /tmp/ with the directory where you are reading from.)

查看更多
登录 后发表回答