Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Detect if booted drive is internal or usb drive

  1. #1
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,099

    Default Detect if booted drive is internal or usb drive

    Hello.

    I have some bootable usb stick.
    Within a script, how to detect if the system booted from internal drive or from usb.

    The number of sda, sdb, ... sdx internal device are unknown.

    Any help is welcome.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS g750JZ Optimus KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,484
    Blog Entries
    15

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

    Hi
    Are you after the actual boot method/partition or what it booted, as in the root device?

    You can see the root device via;

    Code:
    cat /proc/cmdline
    Depends on the setup as these days it references UUID, the following needs to be run as root user which will give references;

    Code:
    blkid
    lsblk -o NAME,UUID
    The boot method could be legacy/mbr or uefi, is this what you are after?
    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!

  3. #3
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,678
    Blog Entries
    2

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

    The question you're asking has been asked since USB connected drives first appeared because systems have been built using USB in place of other (like SATA) connections.

    AFAIK, at the hardware level it's nearly impossible to detect any difference.
    But, it's possible to pass some hints to the system like mounting through fstab, in that case the system probably still can't know for sure the drive is internal or external but will know that the drive is to be mounted automatically during boot.

    Complicating a bit further, there may also be more than one way to mount a drive after detection.

    I guess the big question from a scripting perspective is why you would need to know whether the drive is mounted internally or externally?
    It's more common to do things like detect whether the drive is found.

    Or, by "internal drive" are you assuming that the drive is something other than USB?

    AFAIK,
    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  4. #4
    Join Date
    Sep 2012
    Posts
    5,279

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

    Quote Originally Posted by jcdole View Post
    how to detect if the system booted from internal drive or from usb
    Code:
    $ (cd /sys/block/sdb/device; pwd -P )
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-2/2-2:1.0/host2/target2:0:0/2:0:0:0

  5. #5
    Join Date
    Jan 2014
    Location
    Erlangen
    Posts
    1,151

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

    Quote Originally Posted by arvidjaar View Post
    Code:
    $ (cd /sys/block/sdb/device; pwd -P )
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-2/2-2:1.0/host2/target2:0:0/2:0:0:0
    Code:
    erlangen:~ # readlink -f  /sys/block/sd*
    /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:1f.2/ata4/host3/target3:0:0/3:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-10/2-10:1.0/host6/target6:0:0/6:0:0:0/block/sdd
    erlangen:~ #
    AMD Athlon 4850e (2009), openSUSE 13.1, KDE 4, Intel i3-4130 (2014), i7-6700K (2016), i5-8250U (2018), openSUSE Tumbleweed, KDE Plasma 5

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

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

    Quote Originally Posted by malcolmlewis View Post
    Hi

    Code:
    cat /proc/cmdline
    Code:
    blkid
    lsblk -o NAME,UUID
    The boot method could be legacy/mbr or uefi, is this what you are after?
    Yes.
    Great .


    Of course I know how much internal drive there are on the current computer.
    And I know that there is a swap partition named 'LNX_SWAP_USB' on each opensuse bootable usb disk.

    Code:
    ASUS-G750JZ:~ # cat /proc/cmdline
    BOOT_IMAGE=/boot/vmlinuz-4.12.14-lp151.28.20-default root=UUID=144da1f6-e4e7-402a-b33c-537dd00171c6 resume=/dev/disk/by-label/LNX_SWAP_USB ipv6.disable=1 plymouth.enable=0 mitigations=auto
    ASUS-G750JZ:~ # blkid | grep '144da1f6-e4e7-402a-b33c-537dd00171c6'
    /dev/sdc4: LABEL="LNX_SLASH_USB" UUID="144da1f6-e4e7-402a-b33c-537dd00171c6" TYPE="ext4" PARTLABEL="slash" PARTUUID="461a7323-a511-41e3-b586-c1aa794e9695"
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS g750JZ Optimus KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  7. #7
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,099

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

    Quote Originally Posted by arvidjaar View Post
    Code:
    $ (cd /sys/block/sdb/device; pwd -P )
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-2/2-2:1.0/host2/target2:0:0/2:0:0:0
    Quote Originally Posted by karlmistelberger View Post
    Code:
    erlangen:~ # readlink -f  /sys/block/sd*
    /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:1f.2/ata4/host3/target3:0:0/3:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-10/2-10:1.0/host6/target6:0:0/6:0:0:0/block/sdd
    erlangen:~ #
    Code:
    ASUS-G750JZ:~ # (cd /sys/block/sdb/device; pwd -P )
    /sys/devices/pci0000:00/0000:00:1f.2/ata5/host4/target4:0:0/4:0:0:0
    ASUS-G750JZ:~ # readlink -f  /sys/block/sd*
    /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/ata5/host4/target4:0:0/4:0:0:0/block/sdb
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-5/2-5:1.0/host6/target6:0:0/6:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/host7/target7:0:0/7:0:0:0/block/sdd
    ASUS-G750JZ:~ #

    This system is booted from internal disk ( sda or sdb ) or booted from external usb disk ( sdc or sdd ) ?

    Seems not helping.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS g750JZ Optimus KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  8. #8
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,099

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

    Thank you every body for helping.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS g750JZ Optimus KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  9. #9
    Join Date
    Jan 2014
    Location
    Erlangen
    Posts
    1,151

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

    Quote Originally Posted by jcdole View Post
    Code:
    ASUS-G750JZ:~ # (cd /sys/block/sdb/device; pwd -P )
    /sys/devices/pci0000:00/0000:00:1f.2/ata5/host4/target4:0:0/4:0:0:0
    ASUS-G750JZ:~ # readlink -f  /sys/block/sd*
    /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/ata5/host4/target4:0:0/4:0:0:0/block/sdb
    /sys/devices/pci0000:00/0000:00:14.0/usb2/2-5/2-5:1.0/host6/target6:0:0/6:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4:1.0/host7/target7:0:0/7:0:0:0/block/sdd
    ASUS-G750JZ:~ #

    This system is booted from internal disk ( sda or sdb ) or booted from external usb disk ( sdc or sdd ) ?

    Seems not helping.
    Try again:

    Code:
    erlangen:~ # readlink -f  /sys/block/sd*
    /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:1f.2/ata4/host3/target3:0:0/3:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:14.0/usb4/4-6/4-6:1.0/host7/target7:0:0/7:0:0:0/block/sde
    erlangen:~ # df /
    Filesystem     1K-blocks     Used Available Use% Mounted on
    /dev/sda2       30832548 18414784  10828516  63% /
    erlangen:~ #
    AMD Athlon 4850e (2009), openSUSE 13.1, KDE 4, Intel i3-4130 (2014), i7-6700K (2016), i5-8250U (2018), openSUSE Tumbleweed, KDE Plasma 5

  10. #10
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,484
    Blog Entries
    15

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

    Quote Originally Posted by karlmistelberger View Post
    Try again:

    Code:
    erlangen:~ # readlink -f  /sys/block/sd*
    /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:1f.2/ata4/host3/target3:0:0/3:0:0:0/block/sdc
    /sys/devices/pci0000:00/0000:00:14.0/usb4/4-6/4-6:1.0/host7/target7:0:0/7:0:0:0/block/sde
    erlangen:~ # df /
    Filesystem     1K-blocks     Used Available Use% Mounted on
    /dev/sda2       30832548 18414784  10828516  63% /
    erlangen:~ #
    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% /
    Question, what block device did I boot from?
    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!

Page 1 of 2 12 LastLast

Posting Permissions

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