From c000aa0201186b62804234b7478cb74c0642760b Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 24 Jul 2024 11:07:41 +0200 Subject: [PATCH] ostree: move admindir to /etc/alternatives.admindir `ostree container commit` wipes /var and thereby erases the data in /var/lib/alternatives; the directory used to store configs/symlinks of alternatives. /var is not a good place for storing such configs since it won't receive updates on bootc images either. We need to move to another location. Hence, use /etc/alternatives.admindir when running on an ostree-based system unless /var/lib/alternatives is already present; a user may have worked around the problem. This way we enable alternatives to work on ostree-based systems without breaking backwards compat. Fixes: #9 Signed-off-by: Valentin Rothberg --- Makefile | 1 - alternatives.8 | 2 ++ alternatives.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- chkconfig.spec | 5 +++-- po/chkconfig.pot | 17 +++++++++++------ 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 99f9166a..ace74aa6 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,6 @@ install: [ -d $(DESTDIR)/$(SBINDIR) ] || mkdir -p $(DESTDIR)/$(SBINDIR) [ -d $(DESTDIR)/$(MANDIR) ] || mkdir -p $(DESTDIR)/$(MANDIR) [ -d $(DESTDIR)/$(MANDIR)/man8 ] || mkdir -p $(DESTDIR)/$(MANDIR)/man8 - [ -d $(DESTDIR)/$(ALTDIR) ] || mkdir -p -m 755 $(DESTDIR)/$(ALTDIR) [ -d $(DESTDIR)/$(ALTDATADIR) ] || mkdir -p -m 755 $(DESTDIR)/$(ALTDATADIR) [ -d $(DESTDIR)/$(SYSTEMDUTILDIR) ] || mkdir -p -m 755 $(DESTDIR)/$(SYSTEMDUTILDIR) diff --git a/alternatives.8 b/alternatives.8 index d3bbf6bd..a04785ee 100644 --- a/alternatives.8 +++ b/alternatives.8 @@ -214,6 +214,7 @@ A directory, by default containing .BR alternatives ' state information. +/etc/alternatives.admindir on OSTree-based systems. .TP link group A set of related symlinks, intended to be updated as a group. @@ -416,6 +417,7 @@ option. .TP .I /var/lib/alternatives/ The default administration directory. +/etc/alternatives.admindir on OSTree-based systems. Can be overridden by the .B --admindir option. diff --git a/alternatives.c b/alternatives.c index d15eb2e7..b3a7ad60 100644 --- a/alternatives.c +++ b/alternatives.c @@ -1290,6 +1290,33 @@ static int listServices(const char *altDir, const char *stateDir, int flags) { return 0; } +int dirExists(const char *path) { + struct stat sb; + + if (stat(path, &sb)) { + if (errno == ENOENT) { + return 0; + } + } + + if (!S_ISDIR(sb.st_mode) || access(path, F_OK)) { + fprintf(stderr, _("admindir %s invalid\n"), path); + exit(2); + } + + return 1; +} + +int canUseAlternativeAdminDirUnderOSTree() { + if (dirExists("/var/lib/alternatives")) { return 0; } + + if (fileExists("/run/ostree-booted") || isLink("/ostree")) { + return 1; + } + + return 0; +} + int main(int argc, const char **argv) { const char **nextArg; char *end; @@ -1300,10 +1327,11 @@ int main(int argc, const char **argv) { struct alternative newAlt = {-1, {NULL, NULL, NULL}, NULL, NULL, 0, NULL}; int flags = 0; char *altDir = "/etc/alternatives"; - char *stateDir = "/var/lib/alternatives"; + char *stateDir= NULL; struct stat sb; struct linkSet newSet = {NULL, NULL, NULL}; + setlocale(LC_ALL, ""); bindtextdomain("chkconfig", "/usr/share/locale"); textdomain("chkconfig"); @@ -1311,7 +1339,7 @@ int main(int argc, const char **argv) { if (!argv[1]) return usage(2); - nextArg = argv + 1; + nextArg = argv + 1= NULL; while (*nextArg) { if (!strcmp(*nextArg, "--install")) { if (mode != MODE_UNKNOWN && mode != MODE_FOLLOWER) @@ -1428,6 +1456,19 @@ int main(int argc, const char **argv) { exit(2); } + if ((stateDir == NULL)) { + if (canUseAlternativeAdminDirUnderOSTree()) { + stateDir = "/etc/alternatives.admindir"; + } else { + stateDir = "/var/lib/alternatives"; + } + + if ((dirExists(stateDir) == 0) && (mkdir(stateDir, 0755) == -1)) { + fprintf(stderr, _("creating admindir: %s\n"), strerror(errno)); + exit(2); + } + } + if (stat(stateDir, &sb) || !S_ISDIR(sb.st_mode) || access(stateDir, F_OK)) { fprintf(stderr, _("admindir %s invalid\n"), stateDir); exit(2); diff --git a/chkconfig.spec b/chkconfig.spec index 05494177..62a94270 100644 --- a/chkconfig.spec +++ b/chkconfig.spec @@ -19,7 +19,7 @@ Provides: /sbin/chkconfig %description Chkconfig is a basic system utility. It updates and queries runlevel information for system services. Chkconfig manipulates the numerous -symbolic links in /etc/rc.d, to relieve system administrators of some +symbolic links in /etc/rc.d, to relieve system administrators of some of the drudgery of manually editing the symbolic links. %package -n ntsysv @@ -93,11 +93,12 @@ mkdir -p $RPM_BUILD_ROOT/etc/chkconfig.d %files -n alternatives %license COPYING %dir /etc/alternatives +%ghost /etc/alternatives.admindir +%ghost /var/lib/alternatives %{_sbindir}/update-alternatives %{_sbindir}/alternatives %{_mandir}/*/update-alternatives* %{_mandir}/*/alternatives* -%dir /var/lib/alternatives %changelog * Fri Jun 21 2024 Jan Macku - 1.28-1 diff --git a/po/chkconfig.pot b/po/chkconfig.pot index 7af9386d..853f4e52 100644 --- a/po/chkconfig.pot +++ b/po/chkconfig.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-07-26 11:46+0200\n" +"POT-Creation-Date: 2024-07-31 14:15+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -562,22 +562,27 @@ msgstr "" msgid "failed to remove %s: %s\n" msgstr "" -#: ../alternatives.c:1365 +#: ../alternatives.c:1303 ../alternatives.c:1473 +#, c-format +msgid "admindir %s invalid\n" +msgstr "" + +#: ../alternatives.c:1391 #, c-format msgid "--family can't contain the symbol '@'\n" msgstr "" -#: ../alternatives.c:1427 +#: ../alternatives.c:1455 #, c-format msgid "altdir %s invalid\n" msgstr "" -#: ../alternatives.c:1432 +#: ../alternatives.c:1467 #, c-format -msgid "admindir %s invalid\n" +msgid "creating admindir: %s\n" msgstr "" -#: ../alternatives.c:1442 +#: ../alternatives.c:1483 #, c-format msgid "alternatives version %s\n" msgstr ""