diff --git a/unattended-upgrades/kylin-unattended-upgrade b/unattended-upgrades/kylin-unattended-upgrade index 9ae3dc9..f829136 100644 --- a/unattended-upgrades/kylin-unattended-upgrade +++ b/unattended-upgrades/kylin-unattended-upgrade @@ -325,7 +325,46 @@ logged_msgs = set() # type: AbstractSet[str] NEVER_PIN = -32768 +class InhibitShutdownLock(): + + def __init__(self): + self.inhibit_lock = None + #安装时禁止关机 进行加锁 + def lock(self, caller='Kylin System Updater'): + """ + Send a dbus signal to logind to not suspend the system, it will be + released when the return value drops out of scope + """ + try: + from gi.repository import Gio, GLib + connection = Gio.bus_get_sync(Gio.BusType.SYSTEM) + + var, fdlist = connection.call_with_unix_fd_list_sync( + 'org.freedesktop.login1', '/org/freedesktop/login1', + 'org.freedesktop.login1.Manager', 'Inhibit', + GLib.Variant('(ssss)', + ('shutdown', + caller, 'Installing Packages', + 'block')), + None, 0, -1, None, None) + self.inhibit_lock = Gio.UnixInputStream(fd=fdlist.steal_fds()[var[0]]) + logging.info("Shutdown Has been locked...") + except Exception as e: + logging.error(e) + + #解锁禁止关机 + def unlock(self): + try: + if self.inhibit_lock != None: + self.inhibit_lock.close() + self.inhibit_lock == None + logging.info("Shutdown Has been unlocked...") + else: + logging.info("Not locked and Quitting ...") + except Exception as e: + logging.error("unlock failed." + str(e)) + class LoggingDateTime: """The date/time representation for the dpkg log file timestamps""" LOG_DATE_TIME_FMT = "%Y-%m-%d %H:%M:%S" @@ -1473,8 +1512,8 @@ def cache_commit(cache, # type: apt.Cache if iprogress is None: iprogress = LogInstallProgress(logfile_dpkg, verbose,progress_log=PROGRESS_LOG) try: - with Unlocked(): - res = cache.commit(fetch_progress=None,install_progress=iprogress,allow_unauthenticated=True) + # with Unlocked(): + res = cache.commit(fetch_progress=None,install_progress=iprogress,allow_unauthenticated=True) #cache.open() except SystemError as e: error = e @@ -2918,14 +2957,14 @@ def run(options, # type: Options kylin_system_updater.RunMainloop() ''' # check and get lock - # try: - # apt_pkg.pkgsystem_lock() - # except SystemError: - # logging.error(_("Lock could not be acquired (another package " - # "manager running?)")) - # #print(_("Cache lock can not be acquired, exiting")) - # return UnattendedUpgradesResult( - # False, _("Lock could not be acquired")) + try: + apt_pkg.pkgsystem_lock() + except SystemError: + logging.error(_("Lock could not be acquired (another package " + "manager running?)")) + #print(_("Cache lock can not be acquired, exiting")) + return UnattendedUpgradesResult( + False, _("Lock could not be acquired")) # check if the journal is dirty and if so, take emergceny action # the alternative is to leave the system potentially unsecure until @@ -3048,13 +3087,25 @@ def run(options, # type: Options if kylin_system_updater.GetConfigValue('InstallMode','shutdown_install'): pass else: + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) logging.info("system updater need to run shutdown install quiting...") return UnattendedUpgradesResult(False,_("system updater install override")) if fetcher_statistics.local_pkg_amount == 0: logging.warning("no local pkgs to install") + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) return UnattendedUpgradesResult(True,_("no local pkgs to install")) elif fetcher_statistics.remote_pkg_amount >0: logging.warning("there're pkgs to download") + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) return UnattendedUpgradesResult(False,_("there're pkgs to download")) else: #only write the pkg list when dpkg journal is clean @@ -3089,12 +3140,13 @@ def run(options, # type: Options with open(PROGRESS_LOG,'w+') as f: f.write('0') subprocess.Popen('dbus-send --system --type=signal / com.kylin.install.notification.InstallStart',shell=True) - if LockedPreventShutdown(): - pass - else: - logging.error("cannot get shutdown lock,exiting...") - WriteValueToFile(UNATTENDED_UPGRADE_CONFIG_FILE_PATH,"UNATTENDED_UPGRADE","autoupdate_run_status","idle") - sys.exit(1) + inhibitshutdownlock.lock() + # if LockedPreventShutdown(): + # pass + # else: + # logging.error("cannot get shutdown lock,exiting...") + # WriteValueToFile(UNATTENDED_UPGRADE_CONFIG_FILE_PATH,"UNATTENDED_UPGRADE","autoupdate_run_status","idle") + # sys.exit(1) logging.debug("InstCount=%i DelCount=%i BrokenCount=%i" % (cache._depcache.inst_count, @@ -3106,7 +3158,8 @@ def run(options, # type: Options options, logfile_dpkg) - unLockedEnableShutdown() + # unLockedEnableShutdown() + inhibitshutdownlock.unlock() subprocess.Popen('dbus-send --system --type=signal / com.kylin.install.notification.InstallFinish',shell=True) if pkg_install_success: clean_downloaded_packages(fetcher) @@ -3118,6 +3171,10 @@ def run(options, # type: Options else: install_result = "partial_install" logging.debug("install result:%s"%install_result) + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) return UnattendedUpgradesResult(pkg_install_success, install_result, pkgs) @@ -3178,10 +3235,22 @@ def run(options, # type: Options pass else: pass + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) return UnattendedUpgradesResult(True,_("all pkgs downloaded")) else: + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) return UnattendedUpgradesResult(False,_("some pkgs incompletely fetched")) else: + try: + apt_pkg.pkgsystem_unlock() + except SystemError: + logging.error(_("lock release failed")) logging.debug(_("option is not install-only or download-only")) try: res = fetcher.run() @@ -3623,6 +3692,7 @@ if __name__ == "__main__": kylin_system_updater.ConnectToSignals() kylin_system_updater.UpdateDetect() kylin_system_updater.RunMainloop() + inhibitshutdownlock = InhibitShutdownLock() if options.download_only: WriteValueToFile(UNATTENDED_UPGRADE_CONFIG_FILE_PATH,"UNATTENDED_UPGRADE","autoupdate_run_status","download") elif options.install_only: diff --git a/unattended-upgrades/kylin-unattended-upgrade-shutdown b/unattended-upgrades/kylin-unattended-upgrade-shutdown index 5ec07e3..e7ed7a9 100644 --- a/unattended-upgrades/kylin-unattended-upgrade-shutdown +++ b/unattended-upgrades/kylin-unattended-upgrade-shutdown @@ -132,6 +132,16 @@ def reload_options_config(): apt_pkg.config["Dir::Etc::sourceparts"]="" apt_pkg.init_system() +def get_mavis_capacity(): + batterycapacity = 100 + try: + with open("/sys/class/power_supply/BAT1/capacity", 'r') as f: + batterycapacity = int(f.readline()) + except: + logging.error("no battery device") + return batterycapacity + + def is_dpkg_journal_dirty(): # type: () -> bool """ @@ -778,7 +788,9 @@ class UnattendedUpgradesShutdown(): self.install_mode = ReadValueFromFile(UNATTENDED_UPGRADE_POLICY_FILE_PATH,POLICY_CONF_SECTION_AUTO_UPGRADE_POLICY,AUTO_UPGRADE_POLICY_OPTION_INSTALL_MODE) autoUpgrade = ReadValueFromFile(UNATTENDED_UPGRADE_POLICY_FILE_PATH, POLICY_CONF_SECTION_AUTO_UPGRADE_POLICY, AUTO_UPGRADE_POLICY_OPTION_AUTOUPGRADE) if os_release_info['PROJECT_CODENAME'] == 'V10SP1-edu' and os_release_info['SUB_PROJECT_CODENAME']=='mavis': - if os.path.exists(PKGS_TO_INSTALL_FLAG_FILE): + bat = get_mavis_capacity() + logging.info("battery capacity:%d"%bat) + if os.path.exists(PKGS_TO_INSTALL_FLAG_FILE) and bat >=25: logging.info("mavis shutdown install") do_plymouth_splash() self.start_iterations()