Why the newline a the end of /etc/fstab is required

Here you can discuss every aspect of Debian. Note: not for support requests!
Post Reply
Message
Author
jebez
Posts: 108
Joined: 2024-07-01 15:21
Been thanked: 1 time

Why the newline a the end of /etc/fstab is required

#1 Post by jebez »

https://pubs.opengroup.org/onlinepubs/9 ... tag_03_206.

My /etc/fstab without the newline at the end:

Code: Select all

/dev/disk/by-id/nvme-WDS250G3X0C-00SJG0_185250422455-part3 /efi
nvme-WDS250G3X0C-00SJG0_185250422455-part3 (the ESP, containing efi/boot/bootx64.efi) is effectively mounted in /efi (visible with Dolphin) but KDE Partition Manager crashes.

With the newline:

Code: Select all

/dev/disk/by-id/nvme-WDS250G3X0C-00SJG0_185250422455-part3 /efi

KDE Partition Manager works!

:linked:
Last edited by jebez on 2025-01-10 15:56, edited 1 time in total.
Debian unstable KDE Wayland NVIDIA-Linux-x86_64-565.77.run ukify

lindi
Debian Developer
Debian Developer
Posts: 621
Joined: 2022-07-12 14:10
Has thanked: 2 times
Been thanked: 124 times

Re: Why the newline a the end of a file is required

#2 Post by lindi »

In general text files should end with a newline, for example git warns if they don't.

Aki
Global Moderator
Global Moderator
Posts: 4166
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 121 times
Been thanked: 559 times

Re: Why the newline a the end of a file is required

#3 Post by Aki »

Hello,

The mount program is part of the mount binary package [1], which is built from the util-linux source package [2].

This is a snippet of code that does the parsing of /etc/fstab for the mount program (part of the libmount library) [3][4][5][6][7]:

Code: Select all

[..]
static int mnt_table_parse_next(struct libmnt_parser *pa,
				struct libmnt_table *tb,
				struct libmnt_fs *fs)
{
	char *s;
	int rc;

	assert(tb);
	assert(pa);
	assert(fs);

	/* read the next non-blank non-comment line */
next_line:
	do {
		if (getline(&pa->buf, &pa->bufsiz, pa->f) < 0)
			return -EINVAL;
		pa->line++;
		s = strchr(pa->buf, '\n');
		if (!s) {
			DBG(TAB, ul_debugobj(tb, "%s:%zu: no final newline",
						pa->filename, pa->line));

			/* Missing final newline?  Otherwise an extremely */
			/* long line - assume file was corrupted */
			if (feof(pa->f))
				s = memchr(pa->buf, '\0', pa->bufsiz);

		/* comments parser */
		} [..]
		if (!s)
			goto err;		
[..]
As you can see, the getline() function is used to read the line from the /etc/ftab and then the code checks (with the strchr function) for the presence of a newline character ("\n") at the end of the line read and transfered to a char array (string).

So lines in /etc/fstab should end with a newline, as per the design. Otherwise, if you are at the end of the file, a recovery attempt is done [8].

The KDE Partition Manager crash you reported is another topic that must be threated separately, if required.

Hope this helps.

--
[1] Package: mount
[2] Package: util-linux
[3] static int mnt_table_parse_next(...)
[4] static int mnt_parse_table_line(struct libmnt_fs *fs, const char *s)
[5] static inline const char *skip_separator(const char *p)
[6] char *unmangle(const char *s, const char **end)

[7] static inline const char *skip_nonspaces(const char *s)
[8] https://sources.debian.org/src/util-linux/2.40.3-1/libmount/src/tab_parse.c/?hl=102#L570

--
note: please modify the subject of the first post to recall the fstab file, i.e.:
Why the newline at the end of in /etc/fstab file is required
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀

jebez
Posts: 108
Joined: 2024-07-01 15:21
Been thanked: 1 time

Re: Why the newline a the end of /etc/fstab is required

#4 Post by jebez »

Wow Aki I didn't ask for so much! Just an example to show it's important.
Debian unstable KDE Wayland NVIDIA-Linux-x86_64-565.77.run ukify

Random_Poster
Posts: 1
Joined: 2024-01-26 11:59
Has thanked: 5 times

Re: Why the newline a the end of /etc/fstab is required

#5 Post by Random_Poster »

Thanks for these informative posts. I didn't know about this and learned something today. =)

User avatar
wizard10000
Global Moderator
Global Moderator
Posts: 1220
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 125 times
Been thanked: 219 times

Re: Why the newline a the end of /etc/fstab is required

#6 Post by wizard10000 »

I put a newline in every config file and had no idea where I got that habit. Thanks, Aki!
we see things not as they are, but as we are.
-- anais nin

CwF
Global Moderator
Global Moderator
Posts: 3192
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 65 times
Been thanked: 278 times

Re: Why the newline a the end of /etc/fstab is required

#7 Post by CwF »

@Best_Threads

I picked up the practice from tcl/tk where there are specific options in this regard. Without the /n the line isn't done yet, still waiting for more.
Mottainai

User avatar
blackbird
Posts: 98
Joined: 2023-08-17 04:42
Has thanked: 4 times
Been thanked: 24 times

Re: Why the newline a the end of /etc/fstab is required

#8 Post by blackbird »

That's realy interesting. I didn't know that missing line endings can cause errors, but I never had problems. Now I found out that vim automatically adds a newline at the last line. But it don't shows an empty new line at the end (if a manual newline is added, the file has two newline at the end).
So I tried with more editors, nano does the same, Kate adds a newline when the file is saved (and shows it). Only VSCode don't adds a newline in the default configuration, but it can be enabled in the options with "insert final newline".

Post Reply