Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Detect if booted drive is internal or usb drive

  1. #11
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,925

    Default Re: Detect if booted drive is internal or usb drive

    As Tsu2 already hinted to, it is unclear what the definition of some of the terms used here are.

    E.g. What is the definition of "booted drive" (as mentioned in the title):
    • the device from which the used Grub is loaded?
    • or the device from which the kernel is loaded?
    • or the device that during boot is mounted on / ? (this seems the one that many assume here as being what is asked for, but I am not sure)

    All can be on different file systems and thus on different partitions and those can again be on the same or different mass-storage devices. Go figure.

    I also wonder about the choice "from internal drive or from usb". I can understand USB vs. PCI and/or SCSI and maybe other types of bus/connection. I also can understand the contrast between internal and external (but I have no idea how a Linux system ever can decide if a mass-storage device is inside or outside the main box of a computer). As it is put here I assume it can only lead to confusuion.
    Henk van Velden

  2. #12
    Join Date
    Jan 2014
    Location
    Erlangen
    Posts
    1,275

    Default Re: Detect if booted drive is internal or usb drive

    Quote Originally Posted by malcolmlewis View Post
    Hi
    The logic seems flawed....?

    There are multiple device types as well....

    Code:
    readlink -f  /sys/block/[mm,nv,sd]*
    
    /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/nvme/nvme0/nvme0n1
    /sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
    /sys/devices/pci0000:00/0000:00:1f.2/ata2/host1/target1:0:0/1:0:0:0/block/sdb
    /sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1.5/3-1.5:1.0/host6/target6:0:0/6:0:0:0/block/sdc
    
     df -k /
    Filesystem     1K-blocks     Used Available Use% Mounted on
    /dev/nvme0n1p1  41943040 14280144  27085616  35% /
    Sure.

    Code:
    linux-udd7:~ # readlink -f $(find /sys/block -maxdepth 1 -type l)
    /sys/devices/pci0000:00/0000:00:14.0/usb4/4-6/4-6:1.0/host6/target6:0:0/6:0:0:0/block/sdd
    /sys/devices/pci0000:00/0000:00:1f.2/ata2/host1/target1:0:0/1:0:0:0/block/sdb
    /sys/devices/pci0000:00/0000:00:1f.2/ata3/host2/target2:0:0/2:0:0:0/block/sr0
    /sys/devices/pci0000:00/0000:00:1f.2/ata4/host3/target3:0:0/3:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
    linux-udd7:~ #
    AMD Athlon 4850e (2009), openSUSE 13.1, KDE 4, Intel i3-4130 (2014), i7-6700K (2016), i5-8250U (2018), openSUSE Tumbleweed, KDE Plasma 5

  3. #13
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,113

    Default Re: Detect if booted drive is internal or usb drive

    Quote Originally Posted by hcvv View Post
    As Tsu2 already hinted to, it is unclear what the definition of some of the terms used here are.

    E.g. What is the definition of "booted drive" (as mentioned in the title):
    • the device from which the used Grub is loaded?
    • or the device from which the kernel is loaded?
    • or the device that during boot is mounted on / ? (this seems the one that many assume here as being what is asked for, but I am not sure)

    All can be on different file systems and thus on different partitions and those can again be on the same or different mass-storage devices. Go figure.

    I also wonder about the choice "from internal drive or from usb". I can understand USB vs. PCI and/or SCSI and maybe other types of bus/connection. I also can understand the contrast between internal and external (but I have no idea how a Linux system ever can decide if a mass-storage device is inside or outside the main box of a computer). As it is put here I assume it can only lead to confusuion.
    Assertions completely outside my question.

    You don't read the question.
    I said :
    I have some bootable usb stick.
    Within a script, how to detect if the system booted from internal drive or from usb.
    A computer can boot from any bootable device.
    An internal drive is a drive which is inside the enclosure; and then an external drive is outside the enclosure.
    A computer can boot from an usb stick which is outside the enclosure or boot from an internal disk which is inside the enclosure.
    A script can be run from any folder.
    The same script can run from a folder on an internal disk or on an external disk.
    The same script can run on any operating system.

    Now, the computer started, and the script is running.
    The script is very stupid because it don't know from where it is running, and I must help it.

    And the malcolmlewis's answer in 43 characters ( including \n ) is brillant.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  4. #14
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,925

    Default Re: Detect if booted drive is internal or usb drive

    Quote Originally Posted by jcdole View Post
    A computer can boot from any bootable device.
    Depends on the BIOS, basicaly correct.

    Quote Originally Posted by jcdole View Post
    An internal drive is a drive which is inside the enclosure; and then an external drive is outside the enclosure.
    Same as I said in my post. But it is not something the operating system can detect. It "sees" just a device on a bus and never if that bus sneaks out of the enclosure.

    Quote Originally Posted by jcdole View Post
    A computer can boot from an usb stick which is outside the enclosure or boot from an internal disk which is inside the enclosure.
    The computer can not detect if this is a stick, or any other form (card, revolving disk, SSD).
    The computer can also boot from an USB device that is inside the enclosure.
    The computer can also boot from a PCI device outside the enclosure.
    There is no strickt connection between 'internal vs. external' and 'USB vs other bus'.

    Quote Originally Posted by jcdole View Post
    A script can be run from any folder.
    With some restrictions (mainly permissions in the path through the directories up to and including those of the script file), correct.

    Quote Originally Posted by jcdole View Post
    The same script can run from a folder on an internal disk or on an external disk.
    As the system sees no difference that is true by implication of things mentioned above.

    Quote Originally Posted by jcdole View Post
    The same script can run on any operating system.
    That is of course incorrect. Try to run a bash script on an IBM 360 operating system.

    Quote Originally Posted by jcdole View Post
    Now, the computer started, and the script is running.
    The script is very stupid because it don't know from where it is running, and I must help it.
    Don't blame the script. The operating system is there to hide such hardware trivialities from the system's users. And the running script is only a user of the system.

    BTW, I have seen this more often. Malcolm is a star in assuming the correct information that misses from a problem description. But that gift is not everybody's.
    Last edited by hcvv; 04-Nov-2019 at 04:27.
    Henk van Velden

  5. #15
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    28,090
    Blog Entries
    15

    Default Re: Detect if booted drive is internal or usb drive

    Quote Originally Posted by jcdole View Post
    Now, the computer started, and the script is running.
    The script is very stupid because it don't know from where it is running, and I must help it.
    Hi
    So in your script use the pwd (present working directory) command to tell it where it is running from as a reference? Or send it to a specific directory first to run eg /tmp
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  6. #16
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,113

    Default Re: Detect if booted drive is internal or usb drive

    Quote Originally Posted by malcolmlewis View Post
    Hi
    So in your script use the pwd (present working directory) command to tell it where it is running from as a reference? Or send it to a specific directory first to run eg /tmp
    I wrote a script to configure all computers the same way whether they are new just out of their box or whether they have been reformatted for any reason.
    This script is located on a non bootable independent flash drive.

    This script can also be used to configure a flash drive on which a linux OS has been installed.

    When the script is executed, some operations are not executed if the system has booted from a memory stick.

    Code:
        UUID=$(cat /proc/cmdline | sed 's/.*root=UUID=\(.*\)resume/\1/' | cut -d ' ' -f1)
        DEVICE_NAME=$(lsblk -l -o NAME,UUID | grep $UUID | cut -d ' ' -f1 )
        SDX_DEVICE="${DEVICE_NAME::-1}"
        BOOT=$(lsblk -do name,tran | grep $SDX_DEVICE | cut -d ' ' -f3 )
    Example :
    Code:
    XXXX-YYYYYY-USB:~ # 
    XXXX-YYYYYY-USB:~ #     UUID=$(cat /proc/cmdline | sed 's/.*root=UUID=\(.*\)resume/\1/' | cut -d ' ' -f1)
    XXXX-YYYYYY-USB:~ #     DEVICE_NAME=$(lsblk -l -o NAME,UUID | grep $UUID | cut -d ' ' -f1 )
    XXXX-YYYYYY-USB:~ #     SDX_DEVICE="${DEVICE_NAME::-1}"
    XXXX-YYYYYY-USB:~ #     BOOT=$(lsblk -do name,tran | grep $SDX_DEVICE | cut -d ' ' -f3 )
    XXXX-YYYYYY-USB:~ #     echo "The system has booted from : $BOOT"
    The system has booted from : usb
    XXXX-YYYYYY-USB:~ #
    Thank you for your comments.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •