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: 127
Joined: 2024-07-01 15:21
Been thanked: 2 times

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.
Retired of Debian, I'm on Arch Linux btw

lindi
Debian Developer
Debian Developer
Posts: 628
Joined: 2022-07-12 14:10
Has thanked: 2 times
Been thanked: 128 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: 4259
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 124 times
Been thanked: 576 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: 127
Joined: 2024-07-01 15:21
Been thanked: 2 times

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.
Retired of Debian, I'm on Arch Linux btw

Random_Poster
Posts: 1
Joined: 2024-01-26 11:59
Has thanked: 6 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: 1315
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 134 times
Been thanked: 235 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: 3240
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 69 times
Been thanked: 286 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: 99
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".

User avatar
Trihexagonal
df -h | participant
df -h | participant
Posts: 210
Joined: 2022-03-29 20:53
Location: Land of the Dead
Has thanked: 32 times
Been thanked: 24 times
Contact:

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

#9 Post by Trihexagonal »

Why is /etc/fstab even there.

I have never in 25 years edited that file and went years without even looking at it.
In time you will grow to love your Robot Overlords. Worship me in their stead as their Herald that it may be well with thee in their benevolent electric eyes...and your personal data file.

jebez
Posts: 127
Joined: 2024-07-01 15:21
Been thanked: 2 times

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

#10 Post by jebez »

Because at the time I installed Debian unstable by a script viewtopic.php?p=805129#p805129 (e.g. the script supports F2FS, the regular installers of Debian not (at the time I tried)).

But now I'm on Arch Linux, officially to install by the CLI https://bbs.archlinux.org/viewtopic.php?id=302717:
better nvidia support https://forums.developer.nvidia.com/t/w ... n/318717/2
mkinitcpio better than ukify
rolling release
not the 2 bugs: Sleep: black screen & pointer stuck after resuming & Hibernate: desktop messed up after resuming
...

Arch Linux is probably the best distro.

Debian older than Arch Linux, I believed Debian was the distro n°1...
Retired of Debian, I'm on Arch Linux btw

Post Reply