static usb ethnet addresses
[automated-distro-installer] / fai / config / files / usr / bin / myncq / kd
1 #!/bin/bash
2
3 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
4 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
5 set -eE -o pipefail
6 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR
7
8 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
9
10
11 # todo: we should make this be a service which sets the /sys/ value
12 # before the mount target or something like that, because if you hot
13 # plug a drive in, its ata number will change on reboot, meaning you
14 # have to remember to run this again and then reboot again.
15
16 # example from t8 kernel
17 # Sep 01 14:35:01 watson kernel: ata7.00: status: { DRDY }
18 # Sep 01 14:35:01 watson kernel: ata7.00: failed command: WRITE FPDMA QUEUED
19 # Sep 01 14:35:01 watson kernel: ata7.00: cmd 61/08:c0:b8:bf:ff/00:00:01:00:00/40 tag 24 ncq 4096 out
20
21
22 # https://wiki.archlinux.org/index.php/Solid_state_drive#Resolving_NCQ_errors
23 # evo-870 doesnt get along well with d16.
24 # Dmesg gives us an ata number we could disable specifically on the command line, but I've had that number change on me between oses, so reenabling ncq
25
26 debug=false
27 if [[ $1 == debug ]]; then
28 debug=true
29 fi
30
31 upgrub=true
32 if [[ $1 == no-upgrub ]]; then
33 upgrub=false
34 fi
35
36 regex='^.*-part[0-9]*$'
37 for path in /dev/disk/by-id/ata-Samsung_SSD_870*; do
38 if [[ ! $path =~ $regex ]]; then
39 byid=$path
40 break
41 fi
42 done
43
44 if [[ ! -e $byid ]]; then
45 if $debug; then echo "samsung 870 not plugged in or not found"; fi
46 exit 0
47 fi
48
49 dev=$(readlink $byid)
50 if [[ ! $dev ]]; then
51 exit 1
52 fi
53
54 dev=${dev##*/}
55
56 depth=$(cat /sys/block/$dev/device/queue_depth)
57 if [[ $depth != 0 ]]; then
58 if grep -qF libata.force=noncq /proc/cmdline; then
59 echo $0: warning, cant change queue_depth due to globally disabled ncq
60 else
61 if $debug; then
62 echo "updating to 1 if not: cat /sys/block/$dev/device/queue_depth"
63 cat /sys/block/$dev/device/queue_depth
64 fi
65 echo 1 >/sys/block/$dev/device/queue_depth
66 fi
67 fi
68
69 sys=$(readlink /sys/block/$dev)
70 ata=${sys#*/*/*/*/ata}
71 ata=${ata%%/*}
72
73 arg=libata.force=${ata}.00:noncq
74
75 if ! grep "^GRUB_CMDLINE_LINUX_DEFAULT=.*[\" ]${arg//./\\.}[\" ]" /etc/default/grub; then
76 sed -ri "s/([\" ])libata.force=[^ ]+ */\1/g;s/^GRUB_CMDLINE_LINUX_DEFAULT=\"(.*)/GRUB_CMDLINE_LINUX_DEFAULT=\"$arg \1/" /etc/default/grub
77 if $upgrub; then
78 echo "$0: warning: grub updated. you may want to reboot"
79 if type -P update-grub2 &>/dev/null; then
80 update-grub2
81 else
82 update-grub
83 fi
84 fi
85 fi