-
Notifications
You must be signed in to change notification settings - Fork 2
/
fedora-upgrade
158 lines (146 loc) · 6.08 KB
/
fedora-upgrade
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
#!/bin/sh
#
# Upgrade script to update packages to latest version, and optionally upgrade
# the entire release to a newer major version.
#
# This script will look-up the current installed and the latest available stable
# release versions. A release version number to upgrade to can be specified as
# argument, if not it defaults to latest. If current is already at the specified
# version then a simple package upgrade is performed. If on a different version
# then a major upgrade is performed.
#
# NOTE:
# - Written for WSL import of docker root filesystem of Fedora 33 and 34,
# from Fedora Container Base image (with dnf) and Fedora Container Minimal
# Base image (with microdnf).
# - Must be run by actual root user, not by use of sudo from regular user,
# (sudo is not installed by default in the Fedora Container Minimal Base image).
# - Interactive! Will prompt user before doing any changes!
# - Skips installation of documentation, by specifying --nodocs as command line
# argument. Standard base image has this configured as default with
# "tsflags=nodocs" in /etc/dns/dns.conf, but this is not supported by microdnf
# so for the minimal base image it must be added on command line.
# - Using "update" command even if has been superseded by "upgrade",
# because it wasn't until Fedora 34 that microdnf supported "upgrade" and
# in both dnf and microdnf "update" still works as an alias for "upgrade"!
# - Be careful if there are more than one major version between the current
# and the target version. You sould probably upgrade one version at a time.
# Specify version number as command line argument to control this.
# - Performs most, but not all recommended steps for a major upgrade, as that
# involves manual inspection, but usually this can be skipped or done after
# the upgrade.
# - Read the upgrade guide to see recommended steps to be performed, and if
# there are special considerations for the specific release version:
# https://docs.fedoraproject.org/en-US/quick-docs/upgrading-fedora-online/
#
# Verify running as root
if [ ${EUID:-$(id -u)} -ne 0 ]; then
echo 'Please run this script as root'
exit 1
fi
if ! cat /etc/os-release | grep -q '^ID=fedora$'; then
echo "This is not a Fedora system!"
exit 1
fi
# Figure out if we have dnf or microdnf (from the Fedora Container Minimal Base image)
if hash dnf > /dev/null 2>&1; then
dnf=dnf
elif hash microdnf > /dev/null 2>&1; then
dnf=microdnf
else
echo 'Package manager dnf or microdnf not found'
exit 1
fi
# Perform a normal package upgrade
# Alternatives: Force sync of all repositories with --refresh? Only perform upgrade-minimal?
echo "Upgrading packages..."
if ! $dnf --nodocs update; then
echo "Failed!"
exit 1
fi
# Look up current release
echo
echo "Looking up current release information..."
CURRENT_RELEASE=$(cat /etc/os-release | sed -rn 's/^VERSION_ID=(.*)$/\1/p')
[ -z "$CURRENT_RELEASE" ] && echo "ERROR: Unable to find installed release version" && exit 1
echo "Installed release: $CURRENT_RELEASE"
# Get target release
echo
echo "Deciding target release..."
TARGET_RELEASE=$1
if [ -n "$TARGET_RELEASE" ]; then
echo "Using target release from argument"
echo "Target release: $TARGET_RELEASE"
else
echo "Target release argument not specified, finding latest release..."
# Make sure wget is installed, on clean WSL install of Fedora 34 it is missing!
if ! hash wget > /dev/null 2>&1; then
echo "Installing wget, need it for requesting release info..."
if ! $dnf --nodocs install wget; then
echo "ERROR: Install command returned with error code $?"
exit 1
fi
fi
LATEST_RELEASE=$(wget --quiet --timeout=10 --output-document=- https://dl.fedoraproject.org/pub/fedora/linux/releases/ | sed --quiet --regexp-extended 's/^.*<a href="([0-9]+)\/">.*$/\1/p' | sort --numeric-sort | tail -1)
[ -z "$LATEST_RELEASE" ] && echo "ERROR: Unable to find latest release version" && exit 1
echo "Target/latest release: $LATEST_RELEASE"
TARGET_RELEASE=$LATEST_RELEASE
fi
if [ "$TARGET_RELEASE" = "$CURRENT_RELEASE" ]; then
# No major version change.
echo
echo "Packages updated, no major upgrade, still on release version $CURRENT_RELEASE"
else
echo
echo "Major upgrade from release version ${CURRENT_RELEASE} to ${TARGET_RELEASE}"
echo "Note: If more than one version difference, consider upgrading to each version"
echo "as separate upgrades instead!"
echo
read -p "Do you want to continue? [Y/n] " choice
case "$choice" in
""|y|Y ) ;;
* ) echo "Aborted";exit 1;;
esac
# Delete unused dependencies
# NOTE: The microdnf tool does not support the autoremove command.
# NOTE: Standard container images (including the minimal) has configured clean_requirements_on_remove=True
# which means every package remove in reality is an autoremove, so this should do nothing!
if [ "$dnf" != "microdnf" ]; then
echo
echo "Removing unused dependencies..."
if ! $dnf autoremove; then
echo "Failed!"
exit 1
fi
fi
echo
echo "Cleaning cache and metadata..."
if ! $dnf clean all; then
echo "Failed!"
exit 1
fi
echo
echo "Upgrading to new release..."
if [ "$dnf" = "microdnf" ]; then
if ! $dnf --nodocs --releasever=${TARGET_RELEASE} update; then
echo "Failed!"
exit 1
fi
else
if ! $dnf --nodocs --releasever=${TARGET_RELEASE} --setopt=deltarpm=false distro-sync; then
echo "Failed!"
exit 1
fi
fi
echo "Looking up new release information..."
UPGRADED_RELEASE=$(cat /etc/os-release | sed -rn 's/^VERSION_ID=(.*)$/\1/p')
if [ -n "$UPGRADED_RELEASE" ]; then
if [ "$TARGET_RELEASE" = "$UPGRADED_RELEASE" ]; then
echo "System upgraded from release ${CURRENT_RELEASE} to ${UPGRADED_RELEASE}"
else
echo "WARNING: Installed release seems to be ${UPGRADED_RELEASE} instead of expected ${TARGET_RELEASE}"
fi
else
echo "WARNING: Unable to find version of installed release"
fi
fi