fixes and updates for t10
[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 # https://wiki.archlinux.org/index.php/Solid_state_drive#Resolving_NCQ_errors
11 # evo-870 doesnt get along well with d16.
12 # 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
13
14 upgrub=true
15 if [[ $1 == no-upgrub ]]; then
16 upgrub=false
17 fi
18
19 byid=/dev/disk/by-id/ata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V
20 if [[ ! -e $byid ]]; then
21 # not plugged in we assume
22 exit 0
23 fi
24
25 dev=$(readlink $byid)
26 if [[ ! $dev ]]; then
27 exit 1
28 fi
29
30 dev=${dev##*/}
31
32 depth=$(cat /sys/block/$dev/device/queue_depth)
33 if [[ $depth != 0 ]]; then
34 if grep -qF libata.force=noncq /proc/cmdline; then
35 echo $0: warning, cant change queue_depth due to globally disabled ncq
36 else
37 echo 1 >/sys/block/$dev/device/queue_depth
38 fi
39 fi
40
41 sys=$(readlink /sys/block/$dev)
42 ata=${sys#*/*/*/*/ata}
43 ata=${ata%%/*}
44
45 arg=libata.force=${ata}.00:noncq
46
47 if ! grep "^GRUB_CMDLINE_LINUX_DEFAULT=.*[\" ]${arg//./\\.}[\" ]" /etc/default/grub; then
48 sed -ri "s/([\" ])libata.force=[^ ]+ */\1/g;s/^GRUB_CMDLINE_LINUX_DEFAULT=\"(.*)/GRUB_CMDLINE_LINUX_DEFAULT=\"$arg \1/" /etc/default/grub
49 if $upgrub; then
50 echo "$0: warning: grub updated. you may want to reboot"
51 if type -P update-grub2 &>/dev/null; then
52 update-grub2
53 else
54 update-grub
55 fi
56 fi
57 fi