aboutsummaryrefslogtreecommitdiff
path: root/src/recipes/use-current-kernel.sh
blob: 6673bfb81633a74d864720b894df694732dd2f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
slackware_current_kernel_source() {
  # Download kernel updates from Slackware Current for a stable release.
  #
  # For this to work, make sure the kernel packages are blacklisted in
  # /etc/slackpkg/blacklist.
  #
  #  kernel-generic.*
  #  kernel-huge.*
  #  kernel-modules.*
  #  kernel-source.*
  #
  # This script uses an unusual pattern for running commands on exit, similar to
  # Golang's defer.  These will not be run when the script is stopped with
  # Control+c.
  #
  # D="[command]; $D"; trap "$D" EXIT
  #

  local staging_dir="$1"
  local update_info="$2"

  # Get the system's Slackware version.
  source /etc/os-release

  local mirror=$(sed -n '
          # Remove leading and trailing blanks
          s/^[[:blank:]]*//
          s/[[:blank:]]*$//
          # Only one token is allowed per line
          /[[:blank:]]/d
          # A single solidus should end the URI
          s,[/]*$,/,
          # Print the lines beginning with one of the URI schemes we look for
          \@^file://@p
          \@^cdrom://@p
          \@^local://@p
          \@^https\{0,1\}://@p
          \@^ftps\{0,1\}://@p' /etc/slackpkg/mirrors)
  mirror="$(echo $mirror | sed "s/-$VERSION/-current/")"

  yes y | slackpkg -mirror="$mirror" -batch=on -default_answer=yes update
  D="yes | slackpkg -batch=on -default_answer=yes update"; trap "$D" EXIT

  local package_updates="$(mktemp /tmp/slack-autoupdate.XXXXXX)"
  D="rm -f ${package_updates}; $D"; trap "$D" EXIT

  mv /etc/slackpkg/blacklist /etc/slackpkg/blacklist.orig
  touch /etc/slackpkg/blacklist
  D="mv -f /etc/slackpkg/blacklist.orig /etc/slackpkg/blacklist; $D"; trap "$D" EXIT

  local output="$(slackpkg -batch=on -default_answer=n upgrade kernel)" || exit_code="$?"
  # slackpkg has several safe exit codes.
  if [ $exit_code -ne 0 ] && [ $exit_code -ne 20 ] && [ $exit_code -ne 100 ]; then
    exit exit_code
  fi

  echo "$output" \
    | grep "\.t.z$" \
    | tee "$package_updates"
  if [ ! -s "$package_updates" ]; then
    # No updates
    exit 0
  fi

  while read PKG; do
    PKG_URL="$(
      grep "${PKG}$" /var/lib/slackpkg/CHECKSUMS.md5 \
        | tr -s ' ' \
        | cut -f 2 -d ' '
      )" || exit "$?"

    wget \
      --directory-prefix="$STAGING_DIR" \
      "${mirror}${PKG_URL}" \
      "${mirror}${PKG_URL}.asc" \
      || exit 1
  done <"$package_updates"

  if ! gpg2 --verify-files "$STAGING_DIR"/kernel-*.asc; then
    echo "gpg verification failed"
    exit 1
  fi

  (
    # Redirect this subshell's standard output to info file.
    exec 1>>"$update_info"

    while read PKG; do
      echo " - $PKG"
    done <"$package_updates"
  )
}