Movatterモバイル変換


[0]ホーム

URL:


YOMON8.NET

Ubuntuのunattended-upgradesを調べてみた

この記事をはてなブックマークに追加

Ubuntuバージョン

以下のバージョンで動きを確認しました。

$ uname-v#71~20.04.1-Ubuntu SMP Thu Jul 15 17:46:08 UTC 2021

パッケージ情報

パッケージ情報です。

GitHub

github.com

apt info

$ apt info-a unattended-upgradesPackage: unattended-upgradesVersion:2.3ubuntu0.1Priority: optionalSection: adminOrigin: UbuntuMaintainer: Ubuntu Developers<ubuntu-devel-discuss@lists.ubuntu.com>Original-Maintainer: Michael Vogt<mvo@debian.org>Bugs: https://bugs.launchpad.net/ubuntu/+filebugInstalled-Size:451 kBDepends: debconf(>=0.5) | debconf-2.0, debconf, python3, python3-apt(>=1.9.6~), python3-dbus, python3-distro-info, ucf, lsb-release, lsb-base, xz-utilsRecommends: systemd-sysv | cron | cron-daemon | anacronSuggests: bsd-mailx, default-mta | mail-transport-agent, needrestart, powermgmt-base, python3-giTask: server, ubuntu-desktop-minimal, ubuntu-desktop, cloud-image, kubuntu-desktop, xubuntu-core, xubuntu-desktop, lubuntu-desktop, ubuntustudio-desktop-core, ubuntustudio-desktop, ubuntukylin-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktopDownload-Size:48.7 kBAPT-Manual-Installed: yesAPT-Sources: http://jp.archive.ubuntu.com/ubuntu focal-updates/main amd64 PackagesDescription: セキュリティ更新の自動インストール 本パッケージによりセキュリティ更新を自動かつ無人でダウンロードおよび インストールすることができます。設定された APT ソースに由来するインストール 済みパッケージのみを取り扱い、設定ファイル変更に関する dpkg プロンプトを チェックします。 . 本スクリプトは APT:Periodic:Unattended-Upgrade オプションのバックエンド です。

スケジューリング

anacron (今は使われていない)

WEBで調べているとanacronで実行されているように書かれている記事もあり、実際に以下のファイルが登録されています。

/etc/cron.daily/apt-compat

ただ、中身を見るとわかるのですが、最初にsystemd存在確認が走り、systemdのあるシステムの場合は空振りするようになっています。

$ cat /etc/cron.daily/apt-compat | head-n13#!/bin/shset-e# Systemd systems use a systemd timer unit which is preferable to# run. We want to randomize the apt update and unattended-upgrade# runs as much as possible to avoid hitting the mirrors all at the# same time. The systemd time is better at this than the fixed# cron.daily timeif [-d /run/systemd/system];thenexit0fi

systemdによるスケジュール

anacronのコメントに書いてある通り、現在はSystemdでスケジュールされているようです。自動更新のスケジュールは以下のコマンドで確認できます。

スケジュール確認

systemctl list-timers コマンドを使うとTimerの一覧を確認できます。unattended-upgrade に関連するのはapt-dailyapt-daily-upgrade の二つです。

$ systemctl list-timers--allNEXT                        LEFT          LAST                        PASSED        UNIT                         ACTIVATES# 省略Tue 2021-07-27 04:59:09 JST 5h 56min left Mon 2021-07-26 07:49:08 JST 15h ago       apt-daily.timer              apt-daily.serviceTue 2021-07-27 06:28:11 JST 7h left       Mon 2021-07-26 06:34:08 JST 16h ago       apt-daily-upgrade.timer      apt-daily-upgrade.service# 省略
スケジュール設定(Systemd Timerファイル)

スケジュールが設定されているtimerファイルの中身は以下のようになります。

$ cat /etc/systemd/system/timers.target.wants/apt-daily.timer[Unit]Description=Daily apt download activities[Timer]OnCalendar=*-*-*6,18:00RandomizedDelaySec=12hPersistent=true[Install]WantedBy=timers.target
$ cat /etc/systemd/system/timers.target.wants/apt-daily-upgrade.timer[Unit]Description=Daily apt upgrade and clean activitiesAfter=apt-daily.timer[Timer]OnCalendar=*-*-* 6:00RandomizedDelaySec=60mPersistent=true[Install]WantedBy=timers.target
スケジュール設定

スケジュール設定はsystemdのTimer定義から行います。

以下からOnCalendarRandomizedDelaySec を変更してから、systemctl daemon-reload を実行することで設定が反映されます。

処理概要

systemd Serviceファイル

systemdのサービスファイルです。

ExecStartPre=-/usr/lib/apt/apt-helper wait-online にてオンラインになるのを待ってから、/usr/lib/apt/apt.systemd.daily をそれぞれinstallupdate の引数付きで呼んでいます。

$ cat /lib/systemd/system/apt-daily-upgrade.service[Unit]Description=Daily apt upgrade and clean activitiesDocumentation=man:apt(8)ConditionACPower=trueAfter=apt-daily.service network.target network-online.target systemd-networkd.service NetworkManager.service connman.service[Service]Type=oneshotExecStartPre=-/usr/lib/apt/apt-helper wait-onlineExecStart=/usr/lib/apt/apt.systemd.daily installKillMode=processTimeoutStopSec=900
$ cat /lib/systemd/system/apt-daily.service[Unit]Description=Daily apt download activitiesDocumentation=man:apt(8)ConditionACPower=trueAfter=network.target network-online.target systemd-networkd.service NetworkManager.service connman.service[Service]Type=oneshotExecStartPre=-/usr/lib/apt/apt-helper wait-onlineExecStart=/usr/lib/apt/apt.systemd.daily update
apt.systemd.dailyスクリプト

/usr/lib/apt/apt.systemd.daily はシェルスクリプトです。サービスの名前や引数からは直感的に何しているかわからなかったので、主なコマンドを並べてみました。何となく何しているのかわかると思います。

主なコマンド以外だと/var/cache/apt のディレクトリ下の/var/cache/apt/archive/var/cache/apt/backup などを使った過去データのバックアップや世代管理なども行っています。

ServiceTimerExecStart主なコマンド
apt-daily-upgrade.serviceapt-daily-upgrade.timer/usr/lib/apt/apt.systemd.daily install-unattended-upgrade
-apt-get clean
-apt-get autoclean
apt-daily.serviceapt-daily.timer/usr/lib/apt/apt.systemd.daily update-apt-get update
-apt-get dist-upgrade -d (-d: only download)
-unattended-upgrade --download-only

パラメータ

unattended-upgradeですがパラメータで挙動を調整することが可能です。

現在のパラメータ値を確認する

まずパラメータ値を確認するには以下のコマンドを実行します。

$ apt-config dump

パラメータの設定場所

以下にパラメータの設定が集まっています。

$ls-1 /etc/apt/apt.conf.d/00aptitude00trustcdrom01-vendor-ubuntu01autoremove01autoremove-kernels10periodic15update-stamp20archive20auto-upgrades20dbus20packagekit20snapd.conf50appstream50command-not-found50unattended-upgrades60icons60icons-hidpi70debconf99update-notifier

unattended-upgradeの中身の挙動制御パラメータ

特に/etc/apt/apt.conf.d/50unattended-upgradesunattended-upgrades 自体の挙動を制御するパラメータ群となります。

/usr/bin/unattended-upgrade が`Pythonスクリプトなので、中身見ながら説明書いてみます。

パラメータデフォルト説明
Unattended-Upgrade::Allowed-Origins- ${distro_id}:${distro_codename}
-${distro_id}:${distro_codename}-security
- ${distro_id}ESMApps:${distro_codename}-apps-security
- ${distro_id}ESM:${distro_codename}-infra-security

[マクロ変数に入る値の例]
${distro_id} = "ubuntu"
${distro_codename} = "focal"
許可されるパッケージ取得元
Unattended-Upgrade::Package-Blacklist-更新除外するパッケージ
Unattended-Upgrade::DevReleaseautodevel releaseを自動更新するか(true/false/auto)
Unattended-Upgrade::AutoFixInterruptedDpkgtruedpkg --force-confold --configure -a の自動実行を行うか
Unattended-Upgrade::InstallOnShutdownfalseShutdown時に更新のインストールを行うか
Unattended-Upgrade::Mail/usr/bin/mail/usr/sbin/sendmail などで通知メールを送信するか
Unattended-Upgrade::MailReporton-changeメールを送信する条件:"always", "only-on-error" or "on-change"
Unattended-Upgrade::Remove-Unused-Kernel-Packagestrue利用されていないカーネル関連パッケージの削除
Unattended-Upgrade::Remove-New-Unused-Dependenciestrue更新後の利用されていないパッケージの削除
Unattended-Upgrade::Automatic-Rebootfalse/var/run/reboot-requiredが存在する場合に自動でOS再起動
Unattended-Upgrade::Automatic-Reboot-WithUserstrueUnattended-Upgrade::Automatic-Reboot が有効で再起動を実施する際に、ログオンユーザチェックを行うか。チェック時にログオンユーザがいる場合は再起動を行わない。(falseの場合にチェックを行う)
Unattended-Upgrade::Automatic-Reboot-Timenow02:00 など時間を設定することで、/sbin/shutdown -r pram として実行される
Acquire::http::Dl-Limit70(kb/sec)利用帯域制限
Unattended-Upgrade::SyslogEnablefalseSyslogへの出力
Unattended-Upgrade::SyslogFacilitydaemonSyslogのファシリティ
Unattended-Upgrade::OnlyOnACPowertrue/usr/sbin/on_ac_power コマンドの結果を元にAC Powerでない場合は処理を中断するか
Unattended-Upgrade::Skip-Updates-On-Metered-ConnectionstrueMetered ConnectionはCellar等の従量課金などの接続を指すようで、以下のコードで判断されます。
from gi.repository.Gio import NetworkMonitor
NetworkMonitor.get_network_metered(NetworkMonitor.get_default())
Unattended-Upgrade::VerbosefalseログレベルのVerboseモード
Unattended-Upgrade::Debugfalsedebugログの出力
Unattended-Upgrade::Allow-downgradefalseパッケージのダウングレードの許可

unattended-upgradeの起動パラメータ

systemdから起動時に呼ばれる/usr/lib/apt/apt.systemd.daily に設定可能なパラメータを以下に整理してみました。

パラメータデフォルト説明
Dir/ ルートディレクトリ
Dir::Cachevar/cache/apt/キャッシュディレクトリ
Dir::Cache::Archivesarchives/アーカイブディレクトリ
Dir::Cache::Backupbackup/バックアップディレクトリ
APT::Periodic::Enable10 に設定すると処理がスキップされる(自動更新が止まる)
APT::Periodic::BackupArchiveInterval0 backupフォルダにCacheのバックアップを行うか
APT::Periodic::BackupLevel3バックアップ世代管理パラメータ
APT::Periodic::MaxAge
APT::Periodic::MaxAge(old, deprecated)
0バックアップ世代管理パラメータ
APT::Periodic::MaxSize
APT::Periodic::MaxSize(old, deprecated)
0バックアップ世代管理パラメータ
APT::Periodic::MinAge
APT::Periodic::MinAge(old, deprecated)
2バックアップ世代管理パラメータ
APT::Periodic::Update-Package-Lists0apt-get update を何日おきに実行するか(0は無効)
APT::Periodic::Download-Upgradeable-Packages0apt-get upgrade --download-only を何日おきに実行するか(0は無効)
APT::Periodic::Download-Upgradeable-Packages-Debdelta1debdelta-upgrade でupdateをダウンロードする(0は無効)
APT::Periodic::Unattended-Upgrade0unattended-upgrade でセキュリティアップグレードを何日おきに実行するか(0は無効)
APT::Periodic::AutocleanInterval0apt-get autoclean を何日おきに実行するか(0は無効)
APT::Periodic::CleanInterval0apt-get clean を何日おきに実行するか(0は無効)
APT::Periodic::Verbose0rootユーザへのメール通知
0 : no report
1: progress report
2: command output
3: trace on

設定ファイル

通常の使い方の場合の主な設定ファイルは以下の2つです。

$ cat /etc/apt/apt.conf.d/20auto-upgradesAPT::Periodic::Update-Package-Lists"1";APT::Periodic::Unattended-Upgrade"1";
/etc/apt/apt.conf.d/50unattended-upgrades

以下のように最後に読み込まれるファイルを作成して上書きすることもできます。

/etc/apt/apt.conf.d/99custom

設定後は思った通りの設定になっているかapt-config dump で確認します。

その他

unattended-upgrade-shutdownサービス

unattended-upgradeが走っている時にシャットダウンのシグナルを検知した時などのハンドリングを行うサービスです。以下のようにSystemdで定義されています。

$ cat /etc/systemd/system/multi-user.target.wants/unattended-upgrades.service[Unit]Description=Unattended Upgrades ShutdownAfter=network.target local-fs.target systemd-logind.serviceRequiresMountsFor=/run /var/log /var/run /var/lib /bootDocumentation=man:unattended-upgrade(8)[Service]ExecStart=/usr/share/unattended-upgrades/unattended-upgrade-shutdown--wait-for-signalKillMode=processTimeoutStopSec=1800[Install]WantedBy=multi-user.target

ExecStart見るとわかる通り、実行されている/usr/share/unattended-upgrades/unattended-upgrade-shutdown はPythonスクリプトなので中身を見ると何しているかわかりやすいです。

ログの場所

デフォルトでは以下にログが出力されます。

$ls-1 /var/log/unattended-upgradesunattended-upgrades-dpkg.logunattended-upgrades-dpkg.log.1.gzunattended-upgrades-shutdown.logunattended-upgrades-shutdown.log.1.gzunattended-upgrades.logunattended-upgrades.log.1.gz

マニュアル実行

# -d を付けてログ出力ありで実行$ sudo unattended-upgrade-d# --dry-runもあります$ sudo unattended-upgrade-d--dry-run
検索
このブログについて
id:yomon8id:yomon8はてなブログPro

自分用ノートとして↑の検索窓を使うため、好きな技術ネタを好き勝手書いてます。

@LinkedIn

@Qiita

@GitHub

カテゴリー

引用をストックしました

引用するにはまずログインしてください

引用をストックできませんでした。再度お試しください

限定公開記事のため引用できません。

読者です読者をやめる読者になる読者になる

[8]ページ先頭

©2009-2025 Movatter.jp