Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know where I am booting from

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?

like image 856
CodeCruncher Avatar asked Feb 20 '23 13:02

CodeCruncher


1 Answers

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;                                                                                                                                                 
}
like image 79
alexander Avatar answered Feb 27 '23 03:02

alexander