WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Best way to detect what device you're booting from?  (Read 2311 times)

Offline nim108

  • Full Member
  • ***
  • Posts: 107
Best way to detect what device you're booting from?
« on: September 21, 2015, 08:06:41 AM »
So long story short, I have a live CD remaster that can either be running off CD (/dev/srX) or USB stick (/dev/sdX). My application is embedded in the initrd, but I have large files on the device that my application reads off of; therefore, I need to access it. For ease of access, I simply symlink a directory in /opt/dirlink that points to the device folder I need access to. Currently, in my bootlocal.sh, I have:

Code: (bash) [Select]
if grep -qs '/mnt/sr0' /proc/mounts; then
  ln -s /mnt/sr0/dir /opt/dirlink
elif grep -qs '/mnt/sdb' /proc/mounts; then
 ln -s /mnt/sdb/dir /opt/dirlink
else
 ...
fi

Problem with this is the mount points can change depending on the hardware (i.e. user may have no HDD in the system so it'd be sda instead or user may be using an external DVD device so sr0 may not even exist). So what is the best foolproof way to do this? If I can detect what the device the user is booting from (does TC have knowledge of this in one of its hooks?), it'd be a lot easier.
« Last Edit: September 21, 2015, 08:28:21 AM by nim108 »

Offline Misalf

  • Hero Member
  • *****
  • Posts: 1702
Re: Best way to detect what device you're booting from?
« Reply #1 on: September 21, 2015, 08:46:26 AM »
Not sure if it's 100% foolproof, but I use this:
Code: [Select]
TCE_DIR="$(readlink /etc/sysconfig/tcedir)"
TCE_DIR="${TCE_DIR#/mnt/}"
TCE_DIR="${TCE_DIR%%/*}"

So you could do:
Code: [Select]
/mnt/"$TCE_DIR"/dir
Download a copy and keep it handy: Core book ;)

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: Best way to detect what device you're booting from?
« Reply #2 on: September 21, 2015, 08:48:37 AM »
Linux cannot detect what device it booted from.
Linux does not start until after the boot loader has loaded the kernal and initrds and is no longer running.
The best you can do is include a fake boot code you can process to give you a hint.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Best way to detect what device you're booting from?
« Reply #3 on: September 21, 2015, 08:50:45 AM »
It is not a must to have tcedir on the boot media. First, there is the option tce boot arg where you can set it to anywhere. Also, TC picks up the first tce dir found during startup. For example I'm booting a system from USB but using a tce dir on a hard disk partion.

It was already discussed few times here and the final conclusion is that no fool proof solution. Depending on your needs however you can find a solution which works if some special requirements met.


« Last Edit: September 21, 2015, 08:53:49 AM by bmarkus »
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline nim108

  • Full Member
  • ***
  • Posts: 107
Re: Best way to detect what device you're booting from?
« Reply #4 on: September 21, 2015, 09:05:45 AM »
Thanks guys, because my tcedir is guaranteed to be on the live CD (this would be my "special requirement" as you call it bmarkus), I can use something like Misalf's solution which will work for now.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: Best way to detect what device you're booting from?
« Reply #5 on: September 21, 2015, 10:20:53 AM »
Use a label or UUID?
The only barriers that can stop you are the ones you create yourself.

Offline nim108

  • Full Member
  • ***
  • Posts: 107
Re: Best way to detect what device you're booting from?
« Reply #6 on: September 21, 2015, 10:33:42 AM »
Use a label or UUID?
Not sure but I'll have to go with one of these other suggestions as Misalf's won't work. readlink /etc/sysconfig/tcedir shows up as /tmp/tce on my Live CD so that's a no go.

Offline nim108

  • Full Member
  • ***
  • Posts: 107
Re: Best way to detect what device you're booting from?
« Reply #7 on: September 21, 2015, 12:25:12 PM »
Since the CD/USB is guaranteed to be mounted when you start it up, for now I've resorted to using "mount" and grepping for the /mnt/path (i.e. /mnt/sr0 or /mnt/sdb). Unfortunately, there seems to be few reliable ways to do it.

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: Best way to detect what device you're booting from?
« Reply #8 on: September 21, 2015, 01:30:06 PM »
I've run into this problem on previous projects where multiple devices maybe connected to the PC each with a version of TC installed.  I resolved this issue by creating an empty file with a unique file name installed along with your remastered ISO, or tcz store.  where you install to depends on the exact info you're looking for
We then mount all
search for the unique file name and grep for the specific path information looking for
then unmount all but the device at the path found

there is no doubt now as to the source whether cd/dvd, USB, HDD or specific partition etc

Offline nim108

  • Full Member
  • ***
  • Posts: 107
Re: Best way to detect what device you're booting from?
« Reply #9 on: September 21, 2015, 08:18:02 PM »
I've run into this problem on previous projects where multiple devices maybe connected to the PC each with a version of TC installed.  I resolved this issue by creating an empty file with a unique file name installed along with your remastered ISO, or tcz store.  where you install to depends on the exact info you're looking for
We then mount all
search for the unique file name and grep for the specific path information looking for
then unmount all but the device at the path found

there is no doubt now as to the source whether cd/dvd, USB, HDD or specific partition etc
Interesting approach!