Skip to content

Commit

Permalink
ostree: move admindir to /etc/alternatives.admindir
Browse files Browse the repository at this point in the history
`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 <vrothberg@redhat.com>
  • Loading branch information
vrothberg committed Jul 26, 2024
1 parent 8b44d9e commit b5b76c7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ PROG = chkconfig
BINDIR = /sbin
SBINDIR = /usr/sbin
MANDIR = /usr/man
ALTDIR = /var/lib/alternatives
ALTDATADIR = /etc/alternatives
SYSTEMDUTILDIR = $(shell pkg-config --variable=systemdutildir systemd)
SUBDIRS = po
Expand Down Expand Up @@ -55,7 +54,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)

Expand Down
2 changes: 2 additions & 0 deletions alternatives.8
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ A directory, by default
containing
.BR alternatives '
state information.
/etc/.alternatives.state on OSTree-based systems.
.TP
link group
A set of related symlinks, intended to be updated as a group.
Expand Down Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,32 @@ static int listServices(const char *altDir, const char *stateDir, int flags) {
return 0;
}

int dirExists(const char *path) {
struct stat stats;
stat(path, &stats);

if (S_ISDIR(stats.st_mode))
return 1;

return 0;
}

int canUseAlternativeAdminDir() {
if (dirExists("/var/lib/alternatives")) {
return 0;
}

if (fileExists("/run/ostree-booted")) {
return 1;
}

if (isLink("/ostree")) {
return 1;
}

return 0;
}

int main(int argc, const char **argv) {
const char **nextArg;
char *end;
Expand All @@ -1304,13 +1330,18 @@ int main(int argc, const char **argv) {
struct stat sb;
struct linkSet newSet = {NULL, NULL, NULL};

if (canUseAlternativeAdminDir()) {
stateDir = "/etc/alternatives.admindir";
}

setlocale(LC_ALL, "");
bindtextdomain("chkconfig", "/usr/share/locale");
textdomain("chkconfig");

if (!argv[1])
return usage(2);

int statedir_changed = 0;
nextArg = argv + 1;
while (*nextArg) {
if (!strcmp(*nextArg, "--install")) {
Expand Down Expand Up @@ -1408,6 +1439,7 @@ int main(int argc, const char **argv) {
altDir = normalize_path_alloc(*nextArg);
nextArg++;
} else if (!strcmp(*nextArg, "--admindir")) {
statedir_changed = 1;
nextArg++;
if (!*nextArg)
usage(2);
Expand All @@ -1428,6 +1460,13 @@ int main(int argc, const char **argv) {
exit(2);
}

if ((statedir_changed == 0) && (stat(stateDir, &sb) == -1)) {
if (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);
Expand Down
5 changes: 2 additions & 3 deletions chkconfig.spec
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ mkdir -p $RPM_BUILD_ROOT/etc/chkconfig.d
%{_sysconfdir}/chkconfig.d
%{_sysconfdir}/init.d
%{_sysconfdir}/rc.d
%{_sysconfdir}/rc.d/init.d
%{_sysconfdir}/rc[0-6].d
%{_sysconfdir}/rc.d/rc[0-6].d
%{_mandir}/*/chkconfig*
%{_prefix}/lib/systemd/systemd-sysv-install

Expand All @@ -95,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 <jamacku@redhat.com> - 1.28-1
Expand Down
15 changes: 10 additions & 5 deletions po/chkconfig.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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-26 12:14+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -562,22 +562,27 @@ msgstr ""
msgid "failed to remove %s: %s\n"
msgstr ""

#: ../alternatives.c:1365
#: ../alternatives.c:1396
#, c-format
msgid "--family can't contain the symbol '@'\n"
msgstr ""

#: ../alternatives.c:1427
#: ../alternatives.c:1459
#, c-format
msgid "altdir %s invalid\n"
msgstr ""

#: ../alternatives.c:1432
#: ../alternatives.c:1465
#, c-format
msgid "creating admindir: %s\n"
msgstr ""

#: ../alternatives.c:1471
#, c-format
msgid "admindir %s invalid\n"
msgstr ""

#: ../alternatives.c:1442
#: ../alternatives.c:1481
#, c-format
msgid "alternatives version %s\n"
msgstr ""

0 comments on commit b5b76c7

Please sign in to comment.