LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 09-17-2007, 04:56 AM   #1
musther
Member
 
Registered: Sep 2007
Posts: 36

Rep: Reputation: 15
Trying to run fsck from rc0


I maintain a script which automates fsck, rather than being bugged every 30 boots or whatever, it prompts you to run fsck on shutdown. fsck isn't actually run on shutdown, but the system is rebooted, fsck run, and then powered off.

So, now I'm trying to improve the system to run fsck on shutdown - basically I need a script to run fsck in rulevel 0 before halt.

I linked a script from:
/etc/rc0.d/S89autofsck
just before halt which was S90. The script itself ran as I used echo to make the machine beep - which it did, but fsck wasn't run, or it if was, it started and the machine was still halted.

Somebody told me that the kernel is no longer accepting new processes at this stage, which makes sense, but what I need is some way to run fsck after root is unmounted, and before the system halts. Is there some way to force fsck to run or... ...anything.

Cheers
 
Old 09-17-2007, 01:13 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"what I need is some way to run fsck after root is unmounted, and before the system halts. Is there some way to force fsck to run or... ...anything."

I doubt it. Once root is unmounted then Linux has no way to access the fsck command.

"I maintain a script which automates fsck, rather than being bugged every 30 boots or whatever,"

You can use the tune2fs command to set the number of mounts between fsck runs. You can also use tune2fs to set the parameter to where fsck is never automatically invoked. See:

man tune2fs

----------------
Steve Stites

Last edited by jailbait; 09-17-2007 at 01:19 PM.
 
Old 09-17-2007, 01:18 PM   #3
musther
Member
 
Registered: Sep 2007
Posts: 36

Original Poster
Rep: Reputation: 15
Can I not remount read only?
 
Old 09-17-2007, 02:16 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by musther View Post
Can I not remount read only?
Yes, with most init systems, root may be mounted as read only, after which you may force an “fsck” on your partition.

What is your overall question?
 
Old 09-17-2007, 08:27 PM   #5
musther
Member
 
Registered: Sep 2007
Posts: 36

Original Poster
Rep: Reputation: 15
My overall question is basically; what set of commands can I build a script around, to insert somewhere in the init system, with the end result of running fsck on shutdown.

Thanks.
 
Old 09-18-2007, 03:17 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Let’s assume that your root filesystem resides on /dev/sda1. Then, you write a script like so, and make sure it’s far enough down in the sequence of shutdown scripts so that nothing is currently writing any root-filesystem file. The following script will remount the root filesystem as read-only and afterwards it will try to check it. It depends on /proc being mounted and also on grep, cut, and fsck to be in readable locations (which you might change to match your particular system).
Code:
#!/bin/sh

/bin/sync

maxtries=5
count=0
while ! /bin/mount -no remount,ro /; do
	echo "Unable to mount / as read-only, retrying in 2 seconds" >&2
	sleep 2
	if [ $count -ge $maxtries ]; then
		break;
	fi
	count=$(($count+1))
done

if /bin/grep '^/dev/root' /proc/mounts | /usr/bin/cut -d' ' -f4 | /bin/grep '^ro'; then
	/sbin/fsck -f /dev/sda1
else
	echo "Unable to run fsck since root is still mounted read-write" >&2
	sleep 5
fi

Last edited by osor; 09-20-2007 at 05:57 PM. Reason: double negative in script
 
Old 09-18-2007, 08:58 PM   #7
musther
Member
 
Registered: Sep 2007
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for that, I'll try it out shortly. I'm just not quite sure what this line is doing:

Code:
if ! /bin/grep '^/dev/root' /proc/mounts | /usr/bin/cut -d' ' -f4 | /bin/grep -v 'rw'; then
Can you explain it please, so that I can learn from and modify it?
 
Old 09-20-2007, 05:58 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by musther View Post
I'm just not quite sure what this line is doing:
Code:
if ! /bin/grep '^/dev/root' /proc/mounts | /usr/bin/cut -d' ' -f4 | /bin/grep -v 'rw'; then
Can you explain it please, so that I can learn from and modify it?
Basically, the intent of that line of code was to check whether the remount worked. Unfortunately for you, my brain seems to have been dead at the time it was written. That line not only contains an error—the negative (!) of an inverse grep (-v)—it could be much better. I have fixed the old post, and will try to explain this new line as much as possible here:
Code:
if /bin/grep '^/dev/root' /proc/mounts | /usr/bin/cut -d' ' -f4 | /bin/grep '^ro'; then
Since /etc/mtab should not have been written to if it’s on a read-only filesystem, we use properties of the /proc filesystem instead. In particular, /proc/mounts keeps a list of mounted filesystems. When that file is read, the entries are formatted as follows:
Code:
mnt_devname mnt_root sb_type[.sb_subtype] r{o,w}[,fs_info1,fs_info2,…] 0 0
Where square brackets signify optional fields and curly braces signify that either one or the other of the comma-separated values appear. For example, you might have this on a running system:
Code:
$ cat /proc/self/mounts
rootfs / rootfs rw 0 0
/dev/root / ext3 rw,noatime 0 0
proc /proc proc rw 0 0
sys /sys sysfs rw 0 0
…
At the head of the mount tree is always rootfs which is always read-writable. /dev/root (which is not necessarily an actual device node) is the root filesystem for the process you are considering. If you are in a chroot, it represents the root of that. If you are in an initrd, it represents that root (not real_root_device). For most intents and purposes (especial for the init process), it is the actual root filesystem.

So what the new line does is this: use grep to search for a line beginning with /dev/root in the file /proc/mounts. Grep will write the entire line containing the match to stdout. This is piped to cut, which extracts the fourth field delimited by spaces. The fourth field is then passed to grep, which searches for ro at the beginning. If a match is found, the exit code of the final grep indicates success, so the conditional proceeds to the then statement. Otherwise, the exit code will indicate failure, and the conditional will proceed to the else statement.
 
Old 09-20-2007, 07:57 PM   #9
musther
Member
 
Registered: Sep 2007
Posts: 36

Original Poster
Rep: Reputation: 15
Thank you, that's great!

And I've now integrated it into my project and it's working well.

Thanks again :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripts under rc0.d and rc6.d do not seem to run during shutdown.reboot oferu Linux - Newbie 4 03-26-2008 10:34 AM
how to run FSCK in boot up mokku Linux - Newbie 6 07-25-2007 09:09 PM
How run fsck? lectraplayer Linux - Newbie 1 10-05-2003 08:38 PM
How do I run fsck? RedHat 9 PionexUser Linux - General 5 09-05-2003 02:11 AM
How to run fsck? EnVoy Linux - General 2 03-20-2003 01:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration