From 74b1d267bf78723f045625f24807f98f4a1a51d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=A9=B7=E5=A9=B7zhangtingting=40kylinos=2Ecn?= Date: Fri, 12 Jul 2024 10:27:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E7=BD=B2=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E9=99=90=E5=88=B6=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian/changelog | 7 ++ debian/ostree-aux.conf | 4 ++ debian/ostree.install | 3 +- src/libostree/ostree-sysroot-deploy.c | 97 +++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 debian/ostree-aux.conf diff --git a/debian/changelog b/debian/changelog index c981ce4..7f7b64d 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +ostree (2024.5-1ok1.10) nile; urgency=medium + + * I9TKDM - 历史版本中存在的版本数量多于5个 + * Decoupling deployment quantity limit + + -- zhangtingting Fri, 12 Jul 2024 10:24:56 +0800 + ostree (2024.5-1ok1.9) nile; urgency=medium * delete ost-update.config diff --git a/debian/ostree-aux.conf b/debian/ostree-aux.conf new file mode 100644 index 0000000..d2241b7 --- /dev/null +++ b/debian/ostree-aux.conf @@ -0,0 +1,4 @@ +###Used to store secondary configurations related to ostree + +[DEPLOYMENT] +max_num=5 diff --git a/debian/ostree.install b/debian/ostree.install index 64ad095..faf4eaa 100644 --- a/debian/ostree.install +++ b/debian/ostree.install @@ -4,4 +4,5 @@ usr/bin/rofiles-fuse usr/share/bash-completion/completions/ostree usr/share/ostree/trusted.gpg.d debian/openkylin.conf etc/ostree/remotes.d/ -debian/openkylin-kaiming.conf etc/ostree/remotes.d/ \ No newline at end of file +debian/openkylin-kaiming.conf etc/ostree/remotes.d/ +debian/ostree-aux.conf etc/ostree/ diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c index 671375c..b40dbdb 100644 --- a/src/libostree/ostree-sysroot-deploy.c +++ b/src/libostree/ostree-sysroot-deploy.c @@ -30,6 +30,10 @@ #include #include #include +#include +#include +#include + #ifdef HAVE_LIBMOUNT #include #endif @@ -47,6 +51,11 @@ #include "ostree.h" #include "otcore.h" +#define SECONDARY_CONF "/etc/ostree/ostree-aux.conf" +#define GROUP_NAME_DEPLOYMENT "DEPLOYMENT" +#define MAX_NUM "max_num" + + // The path to the systemd tmpfiles.d directory. #define USRLIB_TMPFILES "usr/lib/tmpfiles.d" @@ -2563,6 +2572,91 @@ write_deployments_bootswap (OstreeSysroot *self, GPtrArray *new_deployments, return TRUE; } +/* Get the maximum quantity value in the configuration file */ +static +int get_max_num_deployment(void) +{ + int max_deploy_num = 5; + GError* error = NULL; + GKeyFile *config = g_key_file_new(); + if(!g_key_file_load_from_file(config, SECONDARY_CONF, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) + { + g_debug("File load exception:%s",error->message); + g_key_file_free(config); + g_error_free(error); + return max_deploy_num; + } + int num_value = g_key_file_get_int64(config, GROUP_NAME_DEPLOYMENT, MAX_NUM, &error); + if(num_value >= 1) + { + max_deploy_num = num_value; + } + g_key_file_free(config); + g_error_free(error); + return max_deploy_num; +} + +/* Calculate the number of deployments that need to be deleted */ +static int +get_ostree_deployments_undeploy_num(OstreeSysroot *self) +{ + g_autoptr(GPtrArray) deployments = ostree_sysroot_get_deployments(self); + int deploy_num = 0; + + for (guint i = 0; i < deployments->len; i++) + { + OstreeDeployment *deployment = deployments->pdata[i]; + if(ostree_deployment_get_unlocked(deployment) != OSTREE_DEPLOYMENT_UNLOCKED_HOTFIX && !ostree_deployment_is_pinned(deployment)) + deploy_num++; + } + + if(deploy_num > get_max_num_deployment()) + return deploy_num - get_max_num_deployment(); + + return 0; +} + + +/* Delete the longest out of quantity deployment + * from back to front */ +static void +ostree_delete_deployment(OstreeSysroot *self) +{ + int num = get_ostree_deployments_undeploy_num(self); + + g_debug("The maximum number of deployments set in the configuration file has been exceeded, and [%d] deployments need to be deleted",num); + g_autoptr (GPtrArray) current_deployments = ostree_sysroot_get_deployments (self); + for (guint i = current_deployments->len - 1; i >= 0 && num > 0; i--) + { + OstreeDeployment *deployment = current_deployments->pdata[i]; + + if(ostree_deployment_get_unlocked(deployment) == OSTREE_DEPLOYMENT_UNLOCKED_HOTFIX || ostree_deployment_is_pinned(deployment)) + continue; + + int deploy_index = ostree_deployment_get_index(deployment); + g_debug("The index of the deployment that is currently being deleted is %d (%s.%d)",deploy_index, + ostree_deployment_get_csum(deployment),ostree_deployment_get_deployserial(deployment)); + + //Determine whether it is a deployment currently in use + if (deployment == ostree_sysroot_get_booted_deployment(self)) + { + g_debug("Cannot undeploy currently booted deployment %d",deploy_index); + continue; + } + + g_ptr_array_remove_index(current_deployments, deploy_index); + + g_print("Deleted deployment %s.%d\n", ostree_deployment_get_csum(deployment), + ostree_deployment_get_deployserial(deployment)); + num--; + } + + ostree_sysroot_write_deployments(self, current_deployments, NULL, NULL); + + if (!ostree_sysroot_cleanup(self, NULL, NULL)) + g_debug("Performing final cleanup"); +} + /* Actions taken after writing deployments is complete */ static gboolean write_deployments_finish (OstreeSysroot *self, GCancellable *cancellable, GError **error) @@ -2577,6 +2671,9 @@ write_deployments_finish (OstreeSysroot *self, GCancellable *cancellable, GError if (!cleanup_legacy_current_symlinks (self, cancellable, error)) return FALSE; + /*Deployment completed, read configuration file, delete redundant deployments*/ + ostree_delete_deployment(self); + return TRUE; } -- Gitee