LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 06-21-2007, 10:01 AM   #1
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Rep: Reputation: 15
Which device did I boot from?


Hello all, I am booting a 2.6 kernel from a USB drive and would like to determine the boot device major and minor numbers or device name from an init script. I'd like to get this information at run-time from the kernel if possible.

Does anyone know how this can be done?

Thanks!
 
Old 06-21-2007, 12:30 PM   #2
LightningCrash
LQ Newbie
 
Registered: Jun 2007
Posts: 23

Rep: Reputation: 15
Code:
mount|grep " / "|cut -f 1 -d " "
whaddayathink?

but then again i might not understand what you're asking for....
 
Old 06-21-2007, 01:12 PM   #3
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
Think LightningCrash has given you the command to show the root partition name. Say this is sda1 then the command
Code:
ls -l /dev/sda1
would display the major and the minor numbers (marked red and blue below
Code:
[root@localhost saikee]# ls -l /dev/sda1
brw-r----- 1 root disk 8, 1 2007-06-21 09:32 /dev/sda1
 
Old 06-21-2007, 01:19 PM   #4
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Original Poster
Rep: Reputation: 15
I'm actually trying to determine the boot device from within initramfs while executing my init script. No devices have been mounted yet, so I can't do what Lightning has prescribed.

I was hoping there was some file within /proc that I could parse to get this information.

There is the file 'cmdline' which could show the command line options if any such as "root=/dev/sdb1". However, this is not a fool-proof solution. I was hoping for something better...
 
Old 06-21-2007, 01:28 PM   #5
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
Look if your distro is booted by Lilo the the root of the Linux is in the "root=" or "boot=" statement inside /etc/lilo.conf.

For Grub it has a "root" statement telling us the partition number j and the disk number i in the (hdi,j) notation. This is the partition where it gets the kernel from but the root "/" may be in a different partition (as often in Red Hat family distros) but the information is always showed as a parameter "root=" in the kernel statement.

Red Hat family like to address the partition reference by a libel.

Ubuntu family distros started to use the UID notation too.

However the actual boot loader is always residing in the partition specified by the "root" statement of /boot/grub/menu.lst as far as Grub is concerned.
 
Old 06-21-2007, 01:32 PM   #6
LightningCrash
LQ Newbie
 
Registered: Jun 2007
Posts: 23

Rep: Reputation: 15
Code:
$ cat /proc/partitions
major minor  #blocks  name

   8     0   78125000 sda
   8     1   75095811 sda1
   8     2          1 sda2
   8     5    3028221 sda5
 254     0   75095811 dm-0
 254     1    3028221 dm-1

Code:
$cat /proc/partitions|sed -n '3p'
   8     0   78125000 sda
8 is major, 0 is minor, 78125000 is blocks, sda is the device name
don't know if this works for unmounted devices, though


of course, all of this could be completely off-base

Last edited by LightningCrash; 06-21-2007 at 01:33 PM.
 
Old 06-21-2007, 01:35 PM   #7
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Original Poster
Rep: Reputation: 15
The problem I have is that I am trying to boot from a USB drive which uses the GRUB boot loader to load a custom built kernel.

It turns out that the system I am booting on, will likely also have a IDE disk drive that also happens to have GRUB installed.

So as far as GRUB is concerned, I can't really tell the difference were I to browse the files found in /boot/grub.

I'm wanting to mount the correct boot device so that I can detect, partition and format other disks found in the system so that I may install Linux on to the other disk.

I'm trying to avoid re-partitioning and formatting my boot device!
 
Old 06-21-2007, 01:39 PM   #8
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Original Poster
Rep: Reputation: 15
The $ cat /proc/partitions will definitely work for unmounted devices, in fact I have been doing this to see what all devices are available. However, it does not reveal which device is the boot device.
 
Old 06-21-2007, 02:19 PM   #9
LightningCrash
LQ Newbie
 
Registered: Jun 2007
Posts: 23

Rep: Reputation: 15
Code:
cat /proc/self/mountstats|grep " / "|grep /dev|cut -f 2 -d " "

there's a way to only do one grep, but i can't do regexps to save my life
 
Old 06-21-2007, 02:48 PM   #10
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Original Poster
Rep: Reputation: 15
Unfortunately, nothing has been mounted yet since I'm running from initramfs which is sort of like a ram disk managed by the kernel. Hence, nothing will be shown yet in mountstats.

The /proc/partitions file definitely shows all the disk devices and their partitions at this time. I wish it also specified which device the kernel was booted from.
 
Old 06-21-2007, 02:48 PM   #11
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
I boot Grub in floppies, internal hard disks, CDs, pen drives, USB hard disks etc and never need to dig that deep.

If you boot up the pen drive and press "c" you should get a Grub prompt.

In a Grub Prompt you can ask Grub to display the partitions of every disk by command
Code:
geometry (hd0)
geometry (hd1)
geometry (hd2)
etc
You can ask Grub to tell you in what partition the menu.lst is available by
Code:
find menu.lst
If Grub tells you it is in (hd0,0) you can demand to see it at the terminal by command
Code:
cat (hd0,0)/boot/grub/menu.lst
You can proceed to boot it by
Code:
configfile root (hd0,0)/boot/grub/menu.lst
if that doesn't boot you can look at the menu.lst (by Cat command) and type in line by line to see at what point Grub has an issue.

As far as I am aware there is no PC system Grub cannot boot in a Grub prompt.
 
Old 06-21-2007, 03:01 PM   #12
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Original Poster
Rep: Reputation: 15
I'm not having any difficulty booting the Linux kernel from GRUB at all. It boots just fine. The problem is that once booted, I can't tell which device I booted from. My system will have multiple devices present. It's important that I know which one I booted from because I do not want to re-install Linux on top of my boot device.
 
Old 06-21-2007, 03:24 PM   #13
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 52
Why don't you want to use /proc/cmdline?
This might need cleaning up but should work from an init script also:
Code:
mount -t proc proc /proc
BOOTROOT=`awk '{print $1}' /proc/cmdline|cut -c 11-14`
grep $BOOTROOT /proc/partitions |awk '{print $1, $2}'
No need to mount proc if it's already mounted of course.
 
Old 06-21-2007, 03:54 PM   #14
Mahonri
Member
 
Registered: May 2007
Location: Austin, Texas, USA
Distribution: Debian Etch
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by makuyl
Why don't you want to use /proc/cmdline?
I'd rather not specify the boot device explicitly on the command line because there is always the chance that what's specified on the command line may not necessarily be the device which Linux is booted from.

For example, imagine a system that has multiple USB ports with drives attached. On system A the boot device could be mapped as /dev/sdc1 and another USB drive mapped as /dev/sdd1. On system B, it could be the opposite of this mapping. If I explicitly specify a device name on the Linux command line, it may not always be correct.

I'd like for the Linux install to detect the boot device name rather than be told what it is from the command line. It seems like it would be more robust / less error prone if this were possible.

It may not be...

Last edited by Mahonri; 06-21-2007 at 03:55 PM.
 
Old 06-22-2007, 06:35 AM   #15
makuyl
Senior Member
 
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 52
Now I get it. This is done by knoppix among others. One of the first few lines when booting a knoppix live-cd is something like "Accessing KNOPPIX at /dev/hdd..". IIRC it's the linuxrc file inside minirt.gz.
You would need to boot a knoppix cd and study the linuxrc file.
gunzip /boot/isolinux/minirt.gz
make a mountpoint and mount it with: mount -o loop /path/minirt /your_mountpoint
It's a while since I played with knoppix, so check the paths. /boot could be /cdrom/KNOPPIX/boot or some such.
Knoppix sources are found at http://debian-knoppix.alioth.debian.org/
 
  


Reply

Tags
boot, device, disk, startup


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
boot cd tranfer boot to usb device?? mutsu Linux - General 0 08-17-2006 12:45 PM
Kind souls: help getting Mandriva install 2 boot? edit boot.ini/grub /boot device or NoMoreReinstallMS Linux - Newbie 1 08-09-2006 04:34 AM
UL 1.0 GRUB: could not find device for /boot: not found or not a block device cma Linux - General 4 12-12-2005 03:35 AM
need to access /boot/grub/menu.lst and /boot/grub/device.map neouto Linux - Newbie 8 09-04-2005 11:45 AM
what is a boot device and where can i get one? computergeek200 General 5 03-15-2004 12:35 PM


All times are GMT -5. The time now is 04:37 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration