aboutsummaryrefslogtreecommitdiff
path: root/slackware-upgrade-system
blob: 45401f6f750e6f6b6b5f7cec756c14d4be0c4e29 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/bin/bash
# slackware-upgrade-system - full upgrade of a Slackware installation
# Copyright (C) 2019 Sergey Poznyakoff.
#
# Slackware-upgrade-system is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published 
# by the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Slackware-upgrade-system is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with slackware-upgrade-system.  If not, see
# <http://www.gnu.org/licenses/>.

set -e

# Slackware root directory
rooturl=
# Log file name
logfile=
# 'y', if dry-run mode is requested
dry_run=
# If 'y', don't ask anything, assume "y" as answer to all quieries
assume_y=
# Output verbosity: '' - normal, 'v' - verbose, 'q' - quiet
verbosity=
# If 'y', install all packages
install_all=
# Names of the series that should be installed in addition to already installed
# packages.  Ignored, if install_all is set.
install_series=
# Names of additional packages.
install_packages=
# Name of the keep-list file.
keep_file=

# Internal variables
remote=
tempdir=${TMP:-/tmp}/slackupg.$$
installed_list=$tempdir/installed.list
avail_index=$tempdir/avail.index
avail_list=$tempdir/avail.list
series_names="a
ap
d
e
f
k
l
n
t
tcl
x
xap
xfce
y
"

function tempdir_create() {
    u=$(umask)
    umask 077
    mkdir $tempdir
    umask $u
}

function tempdir_remove() {
    rm -rf $tempdir
}

function usage() {
    cat <<EOF
usage: $0 [-anqvy] [-k FILE] [-s SERIES] [DIRECTORY]
Upgrade Slackware installation

Options are:

  -a             install all series except kde*
  -h             display this help list and exit
  -k FILE        preserve packages listed in FILE
  -n             dry-run mode: do nothing, print what would have been done
  -p PACKAGE     install additional package
  -q             quiet mode: suppress all messages, except errors
  -s SERIES      additionally install all packages from SERIES
  -v             display messages about individual packages
  -y             assume "yes" to all queries

EOF
}

function getyn() {
    if [ "$assume_y" = y ]; then
	/bin/true
    else
	local dfl=$1
	shift
	case "$dfl" in
	    [yY]) dfl_s=" [Y/n]";;
	    [nN]) dfl_s=" [y/N]";;
	    *)    dfl_s=""; dlf="";;
	esac

	printf "%s$dfl_s? " "$*"
	read REPLY </dev/tty
	case "${REPLY:-$dfl}" in
	    [yY]*)  /bin/true;;
	    *)	/bin/false;;
	esac
    fi
}

function error {
    echo >&2 "$0: $*"
    if [ -n "$logfile" ]; then
	echo >$logfile "$0: $*"
    fi
}

function abend {
    error "$@"
    tempdir_remove
    exit 1
}

function package_file_name() {
    pkg=$(awk -vname=$1 '$1==name { print $2 }' $avail_index)
    if [ -z "$pkg" ]; then
	error "package $name not found in index (should not happen!)"
    else
        echo $pkg
    fi	    
}

function package_name_md5sum() {
    pkg=$(awk -vname=$1 '$1==name { print $3 }' $avail_index)
}

function check_package_md5sum() {
    if [ -n "$checksums" ]; then
	awk -vcname="$1" -vdname="$2" \
	    '$2==cname { print $1 "  " dname }' $checksums | \
	 md5sum --status --check
    fi
}

function all_package_names() {
    grep -v '.*/kde[^/]*/' $avail_index | cut -d ' ' -f 1
}

function series_package_names() {
    grep '.*/'"$1/" $avail_index | cut -d ' ' -f 1
}

function catfile() {
    echo $rooturl/${1#./}
}

function download_curl {
    curl $CURL_OPTIONS -L -sS -o$1 $2
}

function download_wget {
    if ! wget $WGET_OPTIONS -nv -o wget.log -O$1 $2; then
	grep -i "failed\|error" wget.log
	/bin/false
    fi
}

function dnfunc_init() {
    if [ -z "$dnfunc" ]; then
	if curl --version >/dev/null 2>&1; then
	    dnfunc=download_curl
	elif wget --version >/dev/null 2>&1; then
	    dnfunc=download_wget
	else
	    abend "neither curl nor wget is installed"
	fi
    fi
}

function download() {
    local name=$(basename $1)
    local url=$(catfile $1)
    if $dnfunc $name $url; then
        echo $name
    fi
}

function getfile() {
    local name=$(if [ -n "$remote" ]; then
	            download $1
                 else
	            catfile $1
                 fi)
    
    if [ -n "$2" ]; then
	ascname=$(if [ -n "$remote" ]; then
	            download $1.asc
                  else
	            catfile $1.asc
                  fi)
	if [ -n "$ascname" ] \
	    && ${GPG:-gpg} --verify $ascname $name 2>/dev/null; then
  	    :
        else
	    error "gpg verification failed for $name"
	    return
    	fi
    fi

    if [ -n "$checksums" ] && ! check_package_md5sum $1 $name; then
	error "ERROR: $1: checksum failed"
	name=
    fi
    echo $name
}

function dropfile() {
    if [ -n "$remote" ]; then
	rm $1
    fi
}    

function upgrade_package() {
    local name=$1
    shift
    if [ -n "$dry_run" ]; then
	echo "upgradepkg $@ $name"
    else
	file=$(getfile $name)
	if [ -n "$file" ]; then
	    if [ "$verbosity" = 'v' ]; then
		upgradepkg "$@" $file | tee -a $logfile | sed -n -e '/^| Upgrading/s/^| //p'
	    else
		upgradepkg "$@" $file >> $logfile
	    fi
	    dropfile $file
	fi
    fi
}

function version_gt() {
    test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"
}

while getopts "ahknp:qs:vy" OPTION
do
    case $OPTION in
	a) install_all=y;;
	h) usage
	   exit 0;;
	k) keep_file=$OPTARG;;
	n) dry_run=y;;
	p) install_packages="$install_packages $OPTARG";;
	q) verbosity=q;;
	v) verbosity=v;;
	s) install_series="$install_series $OPTARG";;
	y) assume_y=y;;
	*) usage >&2
	   exit 1
    esac
done

shift $(($OPTIND - 1))
case $# in
    0) ;;
    1) rooturl=$1;;
    *) error "unexpected arguments"
       usage >&2
       exit 1
esac

if [ $(id -u) != "0" ]; then
    abend "must be root"
fi

# Sanity check
if [ ! -s /etc/os-release ]; then
    abend "/etc/os-release doesn't exist"
fi

. /etc/os-release

if [ "$ID" != "slackware" ]; then
    abend "this doesn't seem to be a Slackware installation"
fi

case "$(uname -m)" in
    i?86)   ARCH=;;
    x86_64) ARCH=64;;
    *)      abend "architecture $(uname -m) is not yet supported"
esac

tempdir_create
cd $tempdir

mirrors_url=https://mirrors.slackware.com/slackware
if [ -z "$rooturl" ]; then
    dnfunc_init
    if ! $dnfunc index.html $mirrors_url; then
	abend "exiting"
	exit 1
    fi
    version_rx=$(echo $VERSION | sed -e 's/\./\\./g')
    new_version=$(cat index.html |\
      tr '<' '\n' |\
      sed -n -r \
	  -e 's/.*^a href="slackware'$ARCH'-([[:digit:].]+)\/?".*/\1/p'|\
      sed -n -e "/$version_rx/{" -en -ep -e '}' )
    if [ -n "$new_version" ]; then
	rooturl="$mirrors_url/slackware$ARCH-$new_version"
	error "info: using $rooturl as distribution top-level URL"
    else
	abend "can't find distribution newer than $VERSION; please supply URL if you have any"
    fi
fi    

# Check if rooturl is local or remote
case $rooturl in
    /*) unset remote
	;;
    http://*|https://*|ftp://*|ftps://*)
	dnfunc_init
	remote=1
	;;
    *)  abend "root directory must be absolute file name or URL"
esac

if [ -z "$remote" ]; then
    # Check if rooturl exists and contains the necessary files and directories
    if [ ! -d $rooturl ]; then
	abend "$rooturl does not exist"
    fi
fi

# Check if rooturl contains all we need
error "info: verifying distribution..."

#
# Download CHECKSUMS.md5. So far it is the only file that is gpg-checked.
# For the rest we rely on MD5 sums.
checksums=$(getfile CHECKSUMS.md5 gpg)
if [ -z "$checksums" ]; then
    abend "CHECKSUMS.md5 not found in $rooturl"
fi

announce=$(tail +13 $checksums | \
       sed -n -r\
           -e 's/^[0-9a-fA-F]+[[:space:]]+(\.\/ANNOUNCE\.[[:digit:]_]+)$/\1/p')
if [ -z "$announce" ]; then
    abend "ANNOUNCE not found in $rooturl"
fi    
file=$(getfile $announce)
if [ -z "$file" ]; then
    abend "file $announce not found in $rooturl"
fi    

newversion=$(echo "$announce" | sed -e 's/\.\/ANNOUNCE\.//' -e 's/_/./g')
if [ -z "$newversion" ]; then
    abend "cannot determine new version"
fi

# Create list and index of available files
tail +13 $checksums | \
 sed -r \
     -n \
     -e 's/^[0-9a-fA-F]+[[:space:]]+(\.\/slackware(64)?\/.*\/(.*)-[^-]+-(i386|x86(_64)?|arm|noarch|fw)-[[:digit:]]+(_.*)?\.t.z)$/\3 \1/p' | \
 tee $avail_index | awk '{print $1}' | sort > $avail_list

# Initialize log file name
logstem=/var/log/slackware-upgrade-system-$VERSION-$newversion
logfile=$logstem.log

# Check if pkgdir exists and contains the necessary files and directories
for series in $series_names
do
    n=$(sed -n -r -e 's/^[^[:space:]]+[[:space:]]+(\.\/slackware(64)?\/'$series'\/.*\.t.z)$/\1/p' $avail_index | head -1)
    if [ -z "$n" ]; then
	abend "no files in series $series"
    fi
    file=$(getfile $n)
    if [ -z "$file" ]; then
	abend "exiting"
    fi
    dropfile $file
done    

# Build a list of installed packages
ls /var/log/packages |\
 sed -r -e 's/-[^-]+-(i386|x86(_64)?|arm|noarch|fw)-[[:digit:]]+(_.*)?//' |\
 sort > $installed_list

# Build a list of packages to install
(if [ -n "$install_all" ]; then
    all_package_names
else    
    comm -1 -2 $installed_list $avail_list
fi    
for s in $install_series
do
    series_package_names $s
done
if [ -n "$install_packages" ]; then
    echo $install_packages
fi) | sort -u  > candidates

# Build a list of packages that should be removed after install
remove_list=$logstem.removed

comm -2 -3 $installed_list candidates > $remove_list.$$
if [ -s "$keep_file" ]; then
    grep -v '^#' $keep_file | \
	tr -s '\n' | sort | comm -2 -3 $remove_list.$$ - > $remove_list
    rm $remove_list.$$
else
    mv $remove_list.$$ $remove_list
fi

# Disable interrupts during critical section
trap '' HUP INT QUIT ABRT

if [ -s $remove_list -a "$verbosity" != 'q' ]; then
    (cat <<EOF
INFO: The following packages will be removed.  You will have to manually
restore them after the upgrade.  The package names are preserved for you
in the file $remove_list:

EOF
cat $remove_list) | ${PAGER:-more}
fi

if ! version_gt $newversion $VERSION; then
    if ! getyn n "Distribution version $newversion is same or lower than current version $VERSION. Downgrade"; then
	tempdir_remove
	echo Exiting
	exit 0
    fi
fi

if ! getyn n "Ready for upgrade from $VERSION to $newversion. Continue"; then
    tempdir_remove
    echo "Exiting"
    exit 0
fi

# Upgrade the glibc shared libraries.
echo "$0: Upgrading shared libraries"
upgrade_package $(package_file_name glibc-solibs)

# Upgrade the package utilities and related tools.
echo "$0: Upgrading package utilities"
for pkg in pkgtools tar xz findutils
do
    upgrade_package $(package_file_name $pkg)
done

# Upgrade the rest of packages
egrep -v '^(glibc-solibs|pkgtools|tar|xz|findutils)$' candidates |\
while read name
do
    package_file_name $name
done | sort -u | \
while read pkg
do
    s=$(basename ${pkg%/*})
    if [ "$prev" != "$s" ]; then
	echo "$0: installing selected files from series $s"
	prev=$s
    fi
    upgrade_package $pkg --install-new
done

if [ -s $remove_list ]; then
    echo "$0: removing packages"
    if [ -n "$dry_run" ]; then
	:
    elif [ "$verbosity" = 'v' ]; then
	removepkg $(cat $remove_list) | tee -a $logfile | grep '^Removing package'
    else
	removepkg $(cat $remove_list) >> $logfile
    fi
    echo "$0: see $remove_list for the list of packages that have been removed"
fi

echo "$0: upgrade finished; see $logfile for details"

tempdir_remove

conffiles=$logstem.conffiles
find /etc /usr/lib*/ /usr/share/vim -name "*.new" 2>/dev/null | \
    sort > $conffiles
if [ ! -s "$conffiles" ]; then
    rm $conffiles
fi

if [ "$verbosity" != 'q' ]; then
    echo "IMPORTANT!"
    if [ -s "$conffiles" ]; then
	cat <<EOF
Some of your config files may need fixing.  Please check the files from this
list and compare them with their counterparts without the ".new" suffix.
Remove the "*.new" files when finished.  The list of files follows:

EOF
	cat $conffiles
	cat <<EOF

For your convenience, this list is preserved in the file $conffiles.

EOF
    fi
    cat <<EOF
*Before* attempting to reboot your system, please make sure that the
bootloader has been updated for the new kernel!

Good luck!
EOF
fi

Return to:

Send suggestions and report system problems to the System administrator.