On Sun, 15 Mar 2015 21:27:41 -0600 Abel Abraham Camarillo Ojeda 
<[email protected]> wrote:
> On Sun, Mar 15, 2015 at 5:45 PM, Theo de Raadt <[email protected]> 
> wrote:
> 
> > DUID support was written so that we could solve a problem, without
> > a question.  This is a mop-up operation.  The question being posed
> > is not "shall we leave the non-DUID question", but "what DUID support
> > gaps still remain, so that we can finish those".
> >
> 
> 
> The only thing I'd like to have is a command or easy way
> to convert a duid to a /dev/sd0a name to use current - or future -
> utilities that don't support DUID like badblocks from e2fsprogs
> in ports...
> 
> I know it can be done via the C api (opendev(3)?), and using a program
> to get the name first is subject to some races...
> 
> $ badblocks `duid2dev 9d45a80cb6151768.c`
> 
> but obviously this has nothing to do with the options
> in the installer...
> 

i would like something like that too.. so here it is:

first 'lsdisks', a wrapper around hw.disknames to make it human readable:

lsdisks:
#!/bin/ksh

_order="\1 \2"
[ "$1" = -r ] && _order="\2 \1"

sysctl hw.disknames \
  | sed -e 's/.*=//' -e 's/:,/:0000000000000000,/g' \
  | tr ',:' '\n ' \
  | sed -e 's/^.\{3\} / &/' -e "s/^\(.[^ ]*\) \(.*\)/$_order/"


$ lsdisks
 wd0 41b2508dfc016300
 wd1 af0d83d46bb9d50b
 cd0 0000000000000000
 fd0 0000000000000000
 sd0 77080671f0866cf5
 sd1 0b00356d4a1fb02d
vnd0 580308d2b000b7f1
vnd1 76d1d4260a10fbb7
$ lsdisks -r
41b2508dfc016300  wd0
af0d83d46bb9d50b  wd1
0000000000000000  cd0
0000000000000000  fd0
77080671f0866cf5  sd0
0b00356d4a1fb02d  sd1
580308d2b000b7f1 vnd0
76d1d4260a10fbb7 vnd1


and now 'duid2dev', which will print out any device starting with a given
partial duid:

duid2dev:
#!/bin/ksh

_duid=$1
if [ ${#_duid} -eq 0 ];then
  echo "ERR no duid given" >&2
  return 1
fi
if ! echo "$_duid" | grep -q '^[0-9a-f]*$';then
  echo "ERR invalid duid '$_duid'" >&2
  return 1
fi
if ! sysctl hw.disknames | grep -q ":$_duid";then
  echo "ERR duid '$_duid' not found" >&2
  return 1
fi

lsdisks -r | grep ^$1 | sed 's/.* //'


$ duid2dev af0
wd1
$ duid2dev 76d1d4260a
vnd1
$ duid2dev 7
sd0
vnd1


and i also wrote up an 'fstab2dev' which converts all duids to devices.

fstab2dev:
#!/bin/ksh

_fstab=/etc/fstab
[ ${#1} -gt 0 ] && _fstab=$1
cat $_fstab | while read _line;do
  if [[ "$_line" != /* ]];then
    _duid=${_line%%.*}
    _line=${_line#*.}
    echo -n "/dev/$(duid2dev $_duid)"
  fi
  echo "$_line"
done


$ cat /etc/fstab
0b00356d4a1fb02d.a / ffs rw 1 1
0b00356d4a1fb02d.b none swap sw 0 0
77080671f0866cf5.a /mnt ffs rw 1 1
$ fstab2dev
/dev/sd1a / ffs rw 1 1
/dev/sd1b none swap sw 0 0
/dev/sd0a /mnt ffs rw 1 1


i also found an older script i wrote 'fstab_add' that takes a device and mount
point and converts it into a duid fstab entry:

#!/bin/ksh
USAGE="$0 fsdev mntpath [fstab]"
[[ "$1" = -h ]] && { echo "USAGE $USAGE"; return 0; }

isemptyv() {  eval [ \${#$1} -eq 0 ];  }

_fsdev=$1
_mnt=$2
_fstab=$3
if isemptyv _fsdev;then
  echo "ERR no fs device given" >&2
  return 1
fi
if [[ "$_fsdev" != [ws]d[0-9][a-p] ]];then
  echo "ERR bad fs device '$_fsdev'" >&2
  return 1
fi
if isemptyv _mnt;then
  echo "ERR no mnt point given" >&2
  return 1
fi
isemptyv _fstab && _fstab=/etc/fstab
_disk=${_fsdev%?}
_part=${_fsdev#???}

_duid=$(disklabel $_disk 2>/dev/null | sed -n 's/^duid: //p')
if isemptyv _duid;then
  echo "ERR could not get duid for disk '$_disk'" >&2
  return 1
fi
if [[ $_duid = 0000000000000000 ]];then
  echo "ERR null duid" >&2
  return 1
fi
if ! echo "$_duid" | grep '^[0-9a-f]\{16\}$';then
  echo "ERR invalid duid '$_duid'" >&2
  return 1
fi
echo "$_duid.$_part $_mnt ffs rw 1 1" >>$_fstab


# fstab_add wd1a /mnt2 fstab.test
# cat fstab.test
af0d83d46bb9d50b.a /mnt2 ffs rw 1 1

Reply via email to