[plug] Self encrypting drives

David Godfrey info at sbts.com.au
Wed Feb 1 22:26:33 AWST 2017


Hi Brad,


Sorry for the two errors, that's what comes of writing code without the
required infrastructure to test it against :-)
Having said that, I know the regex can't be inside single quotes, that
was simply a slip of the fingers.

The logic error on the other hand is unforgivable, it's damn obvious,
and I would have caught it if I had done some tests, but was too lazy to
whip up a test wrapper ;-)

Glad to be able to throw some useful tips out there, I personally try to
stick to pure bash where I can, piping stuff out to sed, grep, awk, cut,
tr, etc only when speed is an issue (which isn't so common a problem),
or when the quantity of code required to do it in bash gets excessive.
The main reason for that is long term maintainability, doing it in bash
is normally much easier to understand in 12 months time than some arcane
sed, grep, awk invocation.
I've seen regex's that use back references and other tricks that can
take hours to unravel, and even longer to debug when you com back to
them 12 months later.
When they were written you go "Thats awesome, I've done something nearly
impossible there" but in 12 months you curse yourself 6 ways from Sunday.

Regards
David G

On 25/01/17 15:27, Brad Campbell wrote:
> On 25/01/17 08:52, Brad Campbell wrote:
>> On 24/01/17 23:31, David Godfrey wrote:
>>> Hi Brad,
>>>
>>> I believe this didn't go through last time, something about html
>>> content.
>>> So lets try again
>>
>> Sorry, yes it did go through and I plonked your snippet into a quick
>> test file and ran it up.
>>
>> root at bkmac:/home/brad# ./test.sh
>> root at bkmac:/home/brad#
>>
>> root at bkmac:/home/brad# bash -x ./test.sh
>> + SEDU=/usr/local/bin/sedutil-cli
>> + do_start
>> + read -t5 Line
>> ++ /usr/local/bin/sedutil-cli --scan
>> + Dev=
>> + [[ '' =~ \\/dev\\/ ]]
>> + read -t5 Line
>> + Dev=Scanning
>> + [[ Scanning =~ \\/dev\\/ ]]
>> + read -t5 Line
>> + Dev=/dev/sda
>> + [[ /dev/sda =~ \\/dev\\/ ]]
>> + read -t5 Line
>> + Dev=/dev/sdb
>> + [[ /dev/sdb =~ \\/dev\\/ ]]
>> + read -t5 Line
>> + Dev=/dev/sdc
>> + [[ /dev/sdc =~ \\/dev\\/ ]]
>> + read -t5 Line
>> + Dev=/dev/sdd
>> + [[ /dev/sdd =~ \\/dev\\/ ]]
>> + read -t5 Line
>> + Dev=No
>> + [[ No =~ \\/dev\\/ ]]
>> + read -t5 Line
>>
>> I haven't had time to dig into it to see why it doesn't work though.
>>
>
> Right. Now I have.
>
> It wasn't matching the regex on /dev/ due to single quotes around it.
> Don't ask me, I've never used bash for regex before, but removing the
> quotes fixed it.
>
> It also wasn't printing the drive was already unlocked as $wasLocked
> needed negation in the test. Now it works.
>
> Thanks for the lesson in bash. Several tips in there that are
> guarantee to be useful!
>
> do_start () {
>     while read -t5 Line; do
>         Dev=${Line%% *}
>         if [[ $Dev =~ \/dev\/ ]] ; then <--- removed single quotes
>             wasLocked=false
>             while read -t5 qLine; do
>                 if [[ $qLine =~ 'Locked = Y' ]] ; then
>                     echo "$Dev Locked"
>                     if [ -z "$KEY" ] ; then
>                         KEY=`get-key-script`
>                     fi;
>                     $SEDU --setlockingrange 0 rw $KEY $Dev
>                     $SEDU --setMBRDone on $KEY $Dev
>                     /sbin/blockdev --rereadpt $Dev
>                     wasLocked=true;
>                 fi
>             done < <($SEDU --query $Dev)
>             if ! $wasLocked; then <--- NOT waslocked
>                 echo "$Dev Already Unlocked"
>             fi;
>         fi
>     done < <( $SEDU --scan )
> }
>
> Regards,
> Brad
> _______________________________________________
> PLUG discussion list: plug at plug.org.au
> http://lists.plug.org.au/mailman/listinfo/plug
> Committee e-mail: committee at plug.org.au
> PLUG Membership: http://www.plug.org.au/membership
>




More information about the plug mailing list