a bunch of fixes and improvements
[automated-distro-installer] / fai / config / distro-install-common / devbyid
1 #!/bin/bash
2 # This file is part of Ian Kelling's automated-distro-installer
3 # Copyright (C) 2024 Ian Kelling
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # input eg: /dev/sda1 or /dev/sda
20 # output: /dev/disk/by-id/model+serial, or if no link exists, the same as input
21
22 short_dev=$1
23 if [[ ! -e $short_dev ]]; then
24 echo "devbyid: error: argument=$short_dev does not exist"
25 exit 1
26 fi
27
28 # devices are identified by model+serial num
29 # and for ssd/hdd: wwn, and for nvme: eui.
30 # model+serial gives me more info, so use that.
31 shopt -s extglob
32 for id in /dev/disk/by-id/!(nvme-eui*|wwn*); do
33 [[ -e $id ]] || break # if we matched nothing
34 if [[ $(readlink -f $id) == "$short_dev" ]]; then
35 printf '%s\n' "$id"
36 exit
37 fi
38 done
39 # a vm may not have a by-id link.
40 printf '%s\n' "$short_dev"