Bifurcado de ZeTian/do_anduinos_distupgrade.sh

Última atividade 2 weeks ago

Revisão c3f24da6f0a9e4d8ba139c5c07223ff79084d445

do_anduinos_distupgrade.sh Bruto
1#!/bin/bash
2
3#=================================================
4# AnduinOS Upgrade Script
5#=================================================
6# This script upgrades AnduinOS from 1.3.7 (plucky)
7# to 1.4.1 (questing).
8#
9# Usage:
10# ./do_anduinos_distupgrade.sh
11# (Script will auto-elevate to root/sudo)
12#=================================================
13
14set -e
15set -o pipefail
16set -u
17
18Green="\033[32m"
19Red="\033[31m"
20Yellow="\033[33m"
21Blue="\033[36m"
22Font="\033[0m"
23OK="${Green}[ OK ]${Font}"
24ERROR="${Red}[FAILED]${Font}"
25WARNING="${Yellow}[ WARN ]${Font}"
26
27# Backup Configuration
28BACKUP_ROOT="/var/backups/anduinos-upgrade"
29BACKUP_DIR="$BACKUP_ROOT/backup_$(date +%Y%m%d_%H%M%S)"
30PPA_BACKUP_DIR="$BACKUP_DIR/ppa"
31UBUNTU_SOURCE_BACKUP="$BACKUP_DIR/ubuntu_sources"
32
33# --- Helper Functions ---
34
35function print_ok() {
36 echo -e "${OK} ${Blue} $1 ${Font}"
37}
38
39function print_error() {
40 echo -e "${ERROR} ${Red} $1 ${Font}"
41}
42
43function print_warn() {
44 echo -e "${WARNING} ${Yellow} $1 ${Font}"
45}
46
47# --- Privilege Check ---
48
49function ensure_root() {
50 if [ "$EUID" -ne 0 ]; then
51 print_warn "This script requires root privileges."
52 print_ok "Attempting to escalate privileges via sudo..."
53
54 if ! command -v sudo &> /dev/null; then
55 print_error "sudo is not installed. Please run this script as root."
56 exit 1
57 fi
58
59 exec sudo "$0" "$@"
60 exit 0
61 fi
62}
63
64# --- Core Logic ---
65
66function configure_unattended() {
67 print_ok "Configuring system for unattended upgrades (avoiding prompts)..."
68
69 # 1. Install debconf-utils if missing (needed to pre-seed answers)
70 if ! command -v debconf-set-selections &> /dev/null; then
71 apt-get install -y debconf-utils
72 fi
73
74 # 2. Pre-answer "Yes" to "Restart services during package upgrades without asking?"
75 # This kills the specific dialog you saw (libc6/libpam)
76 echo '* libraries/restart-without-asking boolean true' | debconf-set-selections
77
78 # 3. Configure needrestart to be automatic (fixes the purple screen prompt on newer Ubuntu)
79 # 'a' = automatically restart services
80 export NEEDRESTART_MODE=a
81 export DEBIAN_FRONTEND=noninteractive
82
83 judge "Configure unattended mode"
84}
85
86function rollback_on_error() {
87 print_error "An error occurred during the upgrade process"
88 print_warn "Starting rollback procedure..."
89
90 if [ -f "$UBUNTU_SOURCE_BACKUP/ubuntu.sources" ]; then
91 cp "$UBUNTU_SOURCE_BACKUP/ubuntu.sources" /etc/apt/sources.list.d/
92 print_ok "Restored ubuntu.sources"
93 fi
94
95 if [ -f "$UBUNTU_SOURCE_BACKUP/sources.list" ]; then
96 cp "$UBUNTU_SOURCE_BACKUP/sources.list" /etc/apt/
97 print_ok "Restored sources.list"
98 fi
99
100 if [ -d "$PPA_BACKUP_DIR" ] && [ "$(ls -A $PPA_BACKUP_DIR)" ]; then
101 cp "$PPA_BACKUP_DIR"/* /etc/apt/sources.list.d/ || true
102 print_ok "Restored PPA sources"
103 fi
104
105 rm -f /etc/apt/apt.conf.d/99-local-versions
106
107 print_ok "Running apt update to restore repository state..."
108 apt update || true
109
110 print_warn "Rollback completed. Backup preserved in: $BACKUP_DIR"
111 exit 1
112}
113
114function judge() {
115 if [[ 0 -eq $? ]]; then
116 print_ok "$1 succeeded"
117 sleep 0.2
118 else
119 print_error "$1 failed"
120 rollback_on_error
121 fi
122}
123
124function check_disk_space() {
125 print_ok "Checking available disk space..."
126 mkdir -p "$BACKUP_DIR"
127 local root_space=$(df / | awk 'NR==2 {print $4}')
128 local root_space_mb=$((root_space / 1024))
129 local required_space=2048
130
131 print_ok "Available space in /: ${root_space_mb}MB"
132
133 if [ "$root_space_mb" -lt "$required_space" ]; then
134 print_error "Insufficient disk space in /. Required: ${required_space}MB"
135 exit 1
136 fi
137 print_ok "Disk space check passed"
138}
139
140function update_system() {
141 print_ok "Running apt update and upgrade..."
142 apt update
143 judge "apt update"
144
145 # Pass environment variables explicitly to be safe
146 DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a apt upgrade -y
147 judge "apt upgrade"
148}
149
150function backup_ubuntu_sources() {
151 print_ok "Backing up Ubuntu official sources..."
152 mkdir -p "$UBUNTU_SOURCE_BACKUP"
153
154 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
155 cp /etc/apt/sources.list.d/ubuntu.sources "$UBUNTU_SOURCE_BACKUP/"
156 fi
157
158 if [ -f "/etc/apt/sources.list" ] && [ -s "/etc/apt/sources.list" ]; then
159 cp /etc/apt/sources.list "$UBUNTU_SOURCE_BACKUP/"
160 fi
161 judge "Backup Ubuntu sources"
162}
163
164function backup_and_remove_ppa() {
165 print_ok "Backing up and temporarily removing PPA sources..."
166 mkdir -p "$PPA_BACKUP_DIR"
167
168 if [ -d "/etc/apt/sources.list.d" ]; then
169 for file in /etc/apt/sources.list.d/*; do
170 if [ -f "$file" ] && [ "$(basename "$file")" != "ubuntu.sources" ]; then
171 mv "$file" "$PPA_BACKUP_DIR/"
172 fi
173 done
174 fi
175 judge "Backup and remove PPA sources"
176}
177
178function detect_apt_format() {
179 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
180 return 0
181 elif [ -f "/etc/apt/sources.list" ] && [ -s "/etc/apt/sources.list" ]; then
182 return 1
183 else
184 print_error "Cannot detect APT source format"
185 rollback_on_error
186 fi
187}
188
189function convert_old_to_new_format() {
190 print_ok "Converting old format to new DEB822 format..."
191 cp /etc/apt/sources.list /etc/apt/sources.list.backup
192
193 MIRROR_URL=$(grep -E "^deb " /etc/apt/sources.list | grep -v "security" | head -1 | awk '{print $2}')
194 [ -z "$MIRROR_URL" ] && MIRROR_URL="http://archive.ubuntu.com/ubuntu/"
195
196 SECURITY_URL=$(grep -E "^deb " /etc/apt/sources.list | grep "security" | head -1 | awk '{print $2}')
197 [ -z "$SECURITY_URL" ] && SECURITY_URL="http://security.ubuntu.com/ubuntu/"
198
199 bash -c "cat > /etc/apt/sources.list.d/ubuntu.sources" <<EOF
200Types: deb
201URIs: ${MIRROR_URL}
202Suites: plucky plucky-updates plucky-backports
203Components: main restricted universe multiverse
204Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
205
206Types: deb
207URIs: ${SECURITY_URL}
208Suites: plucky-security
209Components: main restricted universe multiverse
210Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
211EOF
212
213 bash -c 'echo "# This file is deprecated. See /etc/apt/sources.list.d/ubuntu.sources" > /etc/apt/sources.list'
214 judge "Convert to new format"
215}
216
217function replace_plucky_with_questing() {
218 print_ok "Replacing plucky with questing..."
219 sed -i 's/plucky/questing/g' /etc/apt/sources.list.d/ubuntu.sources
220 judge "Replace plucky with questing"
221
222 print_ok "Updating package lists..."
223 apt update
224 judge "apt update with questing"
225}
226
227function install_coreutils_uutils() {
228 print_ok "Installing coreutils-from-uutils..."
229 DEBIAN_FRONTEND=noninteractive apt install -y coreutils-from-uutils
230 judge "Install coreutils-from-uutils"
231}
232
233function run_dist_upgrade() {
234 print_ok "Simulating upgrade..."
235 apt -s dist-upgrade > /dev/null
236 judge "apt simulation"
237
238 print_ok "Running full distribution upgrade..."
239
240 # Create local config to force defaults for config files
241 bash -c 'cat > /etc/apt/apt.conf.d/99-local-versions <<EOF
242Dpkg::Options {
243 "--force-confdef";
244 "--force-confold";
245}
246EOF'
247
248 # The Big Command:
249 # DEBIAN_FRONTEND=noninteractive: Hides standard dialogs
250 # NEEDRESTART_MODE=a: Automatically restarts services without asking (Fixes your issue)
251 bash -c 'DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a APT_LISTCHANGES_FRONTEND=none \
252 apt-get -y dist-upgrade \
253 -o Dpkg::Options::="--force-confdef" \
254 -o Dpkg::Options::="--force-confold"'
255
256 judge "apt dist-upgrade"
257 rm -f /etc/apt/apt.conf.d/99-local-versions
258}
259
260function update_release_files() {
261 print_ok "Updating release info..."
262 if [ -f "/etc/os-release" ]; then
263 bash -c "cat > /etc/os-release" <<EOF
264PRETTY_NAME="AnduinOS 1.4.1"
265NAME="AnduinOS"
266VERSION_ID="1.4.1"
267VERSION="1.4.1 (questing)"
268VERSION_CODENAME=questing
269ID=ubuntu
270ID_LIKE=debian
271HOME_URL="https://www.anduinos.com/"
272SUPPORT_URL="https://github.com/Anduin2017/AnduinOS/discussions"
273BUG_REPORT_URL="https://github.com/Anduin2017/AnduinOS/issues"
274PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
275UBUNTU_CODENAME=questing
276EOF
277 fi
278
279 if [ -f "/etc/lsb-release" ]; then
280 bash -c "cat > /etc/lsb-release" <<EOF
281DISTRIB_ID=AnduinOS
282DISTRIB_RELEASE=1.4.1
283DISTRIB_CODENAME=questing
284DISTRIB_DESCRIPTION="AnduinOS 1.4.1"
285EOF
286 fi
287 judge "Update release files"
288}
289
290function restore_ppa_sources() {
291 print_ok "Restoring PPA sources..."
292 if [ -d "$PPA_BACKUP_DIR" ] && [ "$(ls -A $PPA_BACKUP_DIR)" ]; then
293 mv "$PPA_BACKUP_DIR"/* /etc/apt/sources.list.d/
294 apt update
295 fi
296}
297
298function main() {
299 ensure_root
300
301 print_ok "Starting AnduinOS upgrade process..."
302
303 # Interactive check for the human, but not for the machine
304 if [ -t 0 ]; then
305 read -p "Upgrade from plucky (1.3.7) to questing (1.4.1)? (y/N): " confirm
306 if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
307 exit 1
308 fi
309 fi
310
311 # --- NEW STEP: Pre-configure silencing ---
312 configure_unattended
313 # -----------------------------------------
314
315 check_disk_space
316 update_system
317 backup_ubuntu_sources
318 backup_and_remove_ppa
319
320 if ! detect_apt_format; then
321 convert_old_to_new_format
322 fi
323
324 replace_plucky_with_questing
325 install_coreutils_uutils
326 run_dist_upgrade
327 update_release_files
328 restore_ppa_sources
329
330 print_ok "Upgrade completed successfully!"
331 print_warn "Please reboot your system."
332}
333
334main