From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id EB1161FF0E6 for ; Fri, 07 Aug 2026 13:21:31 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8885B2152F; Fri, 07 Aug 2026 13:21:31 +0200 (CEST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Fri, 07 Aug 2026 13:21:24 +0200 Message-Id: Subject: Re: [PATCH proxmox-mini-journalreader v3 1/2] add -B parameter to lists boots From: "Daniel Kral" To: "Maximiliano Sandoval" , X-Mailer: aerc 0.22.0-10-g6373ac9d2179-dirty References: <20260806140955.479029-1-m.sandoval@proxmox.com> <20260806140955.479029-2-m.sandoval@proxmox.com> In-Reply-To: <20260806140955.479029-2-m.sandoval@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1786101677427 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.264 Adjusted score from AWL reputation of From: address BITCOIN_OBFU_SUBJ 1 Bitcoin + obfuscated subject DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) PDS_BTC_ID 0.499 FP reduced Bitcoin ID RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: FL3BFZJUCXWJGCDOAHYTIHOQ5WOZ3CIU X-Message-ID-Hash: FL3BFZJUCXWJGCDOAHYTIHOQ5WOZ3CIU X-MailFrom: d.kral@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Thanks for tackling this! Works as expected, some comments inline. The style-nits are from running a clang-format on it as the rest of the code seemed like it used that already? Might be nice to add a tidy/format target, but out of scope of this series. On Thu Aug 6, 2026 at 4:09 PM CEST, Maximiliano Sandoval wrote: > The parameter will print all boots, in practice it should be piped into t= ail. > > Sample output: > > 2026-07-27 08:28 2026-07-27 15:56 3e3813556d53477f9a139a7cb2f3e8f2 7.0.14= -6-pve > 2026-07-27 15:57 2026-07-27 17:03 5a0324e1ac3f4f24907a21ab81b67384 7.0.14= -7-pve > 2026-07-28 08:26 2026-07-28 17:07 e62bc71e85274f21a1bfb954be38c071 7.0.14= -7-pve > 2026-07-29 08:43 2026-07-29 08:48 7f668e4499644ea1b8339d6b8c29d53d 7.0.14= -7-pve > 2026-07-29 08:49 2026-07-29 17:09 9ff5eb6f0485466383e0b89512f937c9 7.0.14= -8-pve > > glib is added as a dependency for simplicity of memory management. > > As per SD_JOURNAL_QUERY_UNIQUE(3), sd_journal_query_unique ignore matches= hence > why we need to iterate twice: > > Note that these functions currently are not influenced by matches > set with sd_journal_add_match() but this might change in a later > version of this software. > > Signed-off-by: Maximiliano Sandoval > --- > > Notes: > We usually require the output from `last` or similar in support to qu= ery the > previously booted kernel versions. We add this helper to avoid adding= a > dependency on either last or wtmpdb on the ISO images. > =20 > This seems quick enough for ~400 boots: > =20 > time sudo ./proxmox-mini-journalreader -B | wc -l > 417 > =20 > real 0m0,143s > user 0m0,004s > sys 0m0,004s This information should be in the patch message to give more context for what use case this was added for future readers. > =20 > Open questions: > =20 > - Should it list the latest boots first? It amounts to reversing the = sign on > boot_info_cmp. I guess going from oldest to newest is the default for the rest of proxmox-mini-journalreader (and journalctl) as well, so if we really need it the other way around adding an option for that might be better. > - We assume the first line in the journal is the "Linux version" line= . This is > true as of now in practice. One could add a match like the following: > =20 > sd_journal_add_match(j, "_TRANSPORT=3Dkernel", strlen("_TRANSPOR= T=3Dkernel")); > =20 > if this is undesirable. Looking at the relevant code [0], the Linux version line might actually not be the first line... At least cgroup_init_early() could theoretically print warnings earlier than the Linux version line, even though that is unlikely. So if we want to be very robust we would need to check the first few lines of each boot until we hit the Linux version line, though I don't think it's worth the added complexity here. [0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree= /init/main.c?h=3Dv7.1#n1038 > =20 > Differences from v2: > - Mark regex_extract_match and format_timestamp functions as static = char * > - Move the regex out of the for loop so it is only compiled once > - Add G_GNUC_UNUSED hint so that user_data does not produce an unuse= d warning. We need > that parameter as part of the expected signature > - Remove duplicated check for regex !=3D NULL > =20 > Differences from v1: > - Replace uint with size_t > - Remove the use of a second journal > - Copy only 32 chars instead of 33. We were copying \0 which is adde= d by > g_strndup anyways and 32 is the actual length of a BOOT_ID > - use strlen("BOOT_ID=3D") instead of sizeof("BOOT_ID") so it does n= ot work by accident > - Use proper signature for GCompareFunc > - Add a couple of guards for empty messages > > debian/control | 2 +- > src/Makefile | 2 +- > src/mini-journalreader.c | 152 ++++++++++++++++++++++++++++++++++++++- > 3 files changed, 153 insertions(+), 3 deletions(-) > > diff --git a/debian/control b/debian/control > index 8280f6d..d7d8cc0 100644 > --- a/debian/control > +++ b/debian/control > @@ -2,7 +2,7 @@ Source: proxmox-mini-journalreader > Section: admin > Priority: optional > Maintainer: Proxmox Support Team > -Build-Depends: debhelper-compat (=3D 13), libsystemd-dev, pkg-config, sc= doc, > +Build-Depends: debhelper-compat (=3D 13), libsystemd-dev, pkg-config, sc= doc, libglib2.0-dev, > Standards-Version: 4.6.2 > =20 > Package: proxmox-mini-journalreader > diff --git a/src/Makefile b/src/Makefile > index e64e066..c89e74a 100644 > --- a/src/Makefile > +++ b/src/Makefile > @@ -5,7 +5,7 @@ LIBEXEC_DIR ?=3D $(DESTDIR)/usr/libexec/ > MAN1_DIR ?=3D $(DESTDIR)/usr/share/man/man1 > MANPAGE ?=3D $(PROGRAM).1 > =20 > -LIBS :=3D libsystemd > +LIBS :=3D libsystemd glib-2.0 > CFLAGS +=3D -Wall -Wextra -Wl,-z,relro -g -O2 --std=3Dgnu11 > CFLAGS +=3D -fstack-protector-strong -D_FORTIFY_SOURCE=3D2 > CFLAGS +=3D $(shell pkg-config --cflags $(LIBS)) > diff --git a/src/mini-journalreader.c b/src/mini-journalreader.c > index fc78f65..8a7d473 100644 > --- a/src/mini-journalreader.c > +++ b/src/mini-journalreader.c > @@ -21,6 +21,7 @@ > =20 > #include > #include > +#include > #include > #include > #include > @@ -37,6 +38,146 @@ bool json =3D false; > bool structured =3D false; > bool first_line =3D true; > =20 > +typedef struct { > + char *boot_id; > + char *version; > + uint64_t first_timestamp; > + uint64_t last_timestamp; > +} BootInfo; > + > +static void boot_info_free(BootInfo *info) { > + g_free(info->boot_id); > + g_free(info->version); > + g_free(info); > +} nit: maybe then also add a boot_info_{create,init,...}() to put the allocs visually near the frees? > + > +static int boot_info_cmp(gconstpointer a, gconstpointer b) { > + const BootInfo *entry1 =3D a; > + const BootInfo *entry2 =3D b; > + > + if (entry1->first_timestamp < entry2->first_timestamp) return -1; > + if (entry1->first_timestamp > entry2->first_timestamp) return 1; > + return 0; style-nit: if chain without {}s > +} > + > +static char *regex_extract_match(GRegex *regex, const char *message) { > + g_autoptr(GMatchInfo) match_info =3D NULL; > + int start_pos =3D 0, end_pos =3D 0; > + > + g_return_val_if_fail(message !=3D NULL, g_strdup("unknown")); > + > + g_regex_match(regex, message, G_REGEX_MATCH_DEFAULT, &match_info); > + > + if (!g_match_info_matches(match_info)) > + return g_strdup("unknown"); style-nit: if without {} > + > + g_match_info_fetch_pos(match_info, 1, &start_pos, &end_pos); > + > + return g_strndup(message + start_pos, end_pos - start_pos); > +} > + > +static char *format_timestamp(uint64_t timestamp) { > + g_autoptr(GDateTime) time =3D NULL; > + > + if (timestamp =3D=3D 0) { > + return g_strdup ("-"); style nit: GLib-style function invocation > + } > + > + time =3D g_date_time_new_from_unix_local_usec(timestamp); > + return g_date_time_format(time, "%Y-%m-%d %H:%M"); > +} > + > +static void print_boot(BootInfo *info, G_GNUC_UNUSED gpointer user_data)= { > + g_return_if_fail(info !=3D NULL); > + > + g_autofree char *start =3D format_timestamp(info->first_timestamp); > + g_autofree char *end =3D format_timestamp(info->last_timestamp); > + g_print("%s\t%s\t%s\t%s\n", start, end, info->boot_id, info->version= ); > +} > + > +static int list_boots_and_kernels(sd_journal *j) { > + const void *data; > + const size_t prefix_len =3D strlen("_BOOT_ID=3D"); > + size_t len; > + int r; > + g_autoptr(GPtrArray) boot_ids =3D NULL; > + g_autoptr(GPtrArray) boot_array =3D NULL; > + g_autoptr(GRegex) regex =3D NULL; > + g_autoptr(GError) error =3D NULL; > + > + boot_ids =3D g_ptr_array_new_with_free_func(g_free); > + > + r =3D sd_journal_query_unique(j, "_BOOT_ID"); > + if (r < 0) { > + g_printerr("Failed to query _BOOT_ID identifier: %s\n", strerror= (-r)); > + return 1; > + } > + SD_JOURNAL_FOREACH_UNIQUE(j, data, len) { > + g_ptr_array_add(boot_ids, g_strndup((const char *)data, len)); > + } > + > + boot_array =3D g_ptr_array_new_full(boot_ids->len, (GDestroyNotify)b= oot_info_free); > + > + sd_journal_flush_matches(j); > + > + regex =3D g_regex_new("Linux version ([a-zA-Z0-9.-]+)", G_REGEX_OPTI= MIZE, G_REGEX_MATCH_DEFAULT, &error); > + if (error) { > + g_critical("regex compilation failed: %s", error->message); > + return 1; > + } > + > + for (size_t i =3D 0; i < boot_ids->len; i++) { > + uint64_t first_ts =3D 0, last_ts =3D 0; > + size_t msg_len; > + const char *message =3D NULL; > + const char *match =3D g_ptr_array_index(boot_ids, i); nit: maybe use a more telling name like `boot_id`? > + g_autofree char* version =3D NULL; style nit: placement of the * type specifier > + g_autofree char *boot_id =3D NULL; > + > + // Strip _BOOT_ID=3D from the message > + boot_id =3D g_strndup(match + prefix_len, 32); > + > + sd_journal_add_match(j, match, strlen(match)); > + > + r =3D sd_journal_seek_head(j); > + if (r >=3D 0) { > + r =3D sd_journal_next(j); > + if (r > 0) { > + sd_journal_get_realtime_usec(j, &first_ts); > + } > + } > + > + r =3D sd_journal_get_data(j, "MESSAGE", (const void **)&message,= &msg_len); > + if (r >=3D 0 && message !=3D NULL && msg_len > 0) > + version =3D regex_extract_match(regex, message); > + else > + version =3D g_strdup("unknown"); style-nit: if chain without {}s > + > + r =3D sd_journal_seek_tail(j); > + if (r >=3D 0) { > + r =3D sd_journal_previous(j); > + if (r > 0) { > + sd_journal_get_realtime_usec(j, &last_ts); > + } > + } > + > + sd_journal_flush_matches(j); > + > + BootInfo *boot_info =3D g_new0 (BootInfo, 1); style nit: GLib-style function invocation > + boot_info->boot_id =3D g_steal_pointer(&boot_id); > + boot_info->version =3D g_steal_pointer(&version); > + boot_info->first_timestamp =3D first_ts; > + boot_info->last_timestamp =3D last_ts; > + > + g_ptr_array_add(boot_array, boot_info); > + } > + > + g_ptr_array_sort_values(boot_array, (GCompareFunc)boot_info_cmp); > + g_ptr_array_foreach(boot_array, (GFunc)print_boot, NULL); misses a sd_journal_close(j) at the end, though see comment at the end > + > + return 0; > +} > + > // helper to print errors on stderr > // if we're in json mode, print closing json body if possible > static void print_error_and_exit(const char *fmt, ...) { > @@ -471,6 +612,7 @@ _Noreturn static void usage(char *error) { > " -J\t\t\tprint as json with one object of separate fields per = entry\n" > " -I\t\t\twith -J, also emit a record listing the distinct sysl= og identifiers\n" > " -U\t\t\twith -J, also emit a record listing the distinct syst= emd units\n" > + " -B\t\t\tList boots. The columns are: the date of the first en= try in the boot and last entry, the boot id, and the kernel version\n" This should at least state that it does ignore all the other parameters, so it essentially has a different functionality than the rest of the parameters. > " -h\t\t\tthis help\n" > "\n" > "Passing no range option will dump all the available journal\n" > @@ -711,11 +853,12 @@ int main(int argc, char *argv[]) { > bool kernel =3D false; > bool list_identifiers =3D false; > bool list_units =3D false; > + bool list_boots =3D false; > int c; > =20 > progname =3D argv[0]; > =20 > - while ((c =3D getopt(argc, argv, "b:e:d:n:f:t:p:i:u:jJIUkh")) !=3D -= 1) { > + while ((c =3D getopt(argc, argv, "b:e:d:n:f:t:p:i:u:jJIUkh:B")) !=3D= -1) { > switch (c) { > case 'b': > begin =3D arg_to_timestamp_usec(optarg); > @@ -762,6 +905,9 @@ int main(int argc, char *argv[]) { > case 'U': > list_units =3D true; > break; > + case 'B': > + list_boots =3D true; > + break; > case 'h': > usage(NULL); > case '?': > @@ -816,6 +962,10 @@ int main(int argc, char *argv[]) { > return 1; > } > =20 > + if (list_boots) { > + return list_boots_and_kernels(j); > + } > + As this new flag does ignore the rest of the parameters (except -d), it might be nicer to factor out the rest of the functionality into a separate function to clearly show that there are essentially two different 'modes' that proxmox-mini-journalreader can operate in. Also we get the missing sd_journal_close() for free then by not returning straight away. On another note, a few of the parameters might be nice to actually acknowledge for listing the boots and kernel versions, e.g. -n: limit to last number of boots -{b,e}: begin and end where to print boots for -{j,J}: print the output as JSON Though for the current usage -n would be the nicest so we don't have to use tail, but no hard feelings about this. > // restrict the traversal before seeking. Each filter is a separate = group, conjoined (AND) with > // the others; within a group matches are OR'd (same field, or an ex= plicit disjunction for the > // unit), so the groups must be split by a conjunction