From owner-freebsd-questions@freebsd.org Thu Jun 14 16:53:02 2018 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7FCB11013875 for ; Thu, 14 Jun 2018 16:53:02 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass DE-2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id EE84B74347 for ; Thu, 14 Jun 2018 16:53:01 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from r56.edvax.de ([92.195.97.38]) by mrelayeu.kundenserver.de (mreue007 [212.227.15.167]) with ESMTPA (Nemesis) id 0Ljxqo-1g4D9g2Joo-00cDTs; Thu, 14 Jun 2018 18:52:54 +0200 Date: Thu, 14 Jun 2018 18:52:54 +0200 From: Polytropon To: "Steve O'Hara-Smith" Cc: Manish Jain , FreeBSD Questions , jungle Boogie Subject: Re: How to detect single user mode in FreeBSD ? Message-Id: <20180614185254.6099d295.freebsd@edvax.de> In-Reply-To: <20180614084752.bd90d2277cdcaf099c501bdc@sohara.org> References: <89bc6774-aa0d-6704-71a1-6b8eea8ae3b5@yandex.com> <20180614084752.bd90d2277cdcaf099c501bdc@sohara.org> Reply-To: Polytropon Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:FsZ9bj5I/sfOZPs3PnYv0CNPcCmlLHq9UrEYG6cqq7JJ2mYmORq Bn9vDtrbdPNFqM9u4RhpE29m3oTZ1bIlnwK8q1SV0ez2KOqq0yPQsQiqePQETHxGhCsep3j Hm1RLkgpcqarwih57hnXEYrPKJjBL+kiIgRL8vPgPF1JIou3G5w+92suoD0KOHfylR54/MT +cVek9cYslIUC4i5E0MTQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:O2hBY5Rf2fc=:/VVNIARBqCajgrNbKmeeIi oFPBnlJjb1kMv9Nv754DGQ8PFglRu17HTr+Tkr2szfLccQslxmREIVe13o/OJETOk/Qh3okRP uSCWrBdT8efU2mGI9M8GcRn/o+ipvIu+dAEUKibysfW/tc1KkUw2QLh6va7drFVdB8Qtt9ys1 Wy4/Xuwcg1+vyRIQHh/AUV6J6O3pZV8OKdcEGdHz8JJ6x/21pkmfFRsLh6oDU5NC+gWVk074w 7tJ/Ius3nrcY0zgCWQ8ZroRrYt08x6oVNamcBYQP2NFDLc2SRwXENhZ985g/WNd2wLCYq+btZ uozMTUhulKxCuiby+FYErkBsyVONpxr5BEeJpObbgcw6LsQDa7Kx3QIGMyYqDBhPrl2Dky5py GQLM6vVYejAA71LVnvj8UfNkHKfiF4JqUTfhXN/YCES5yZnC81Kd7eaABLM8bFXvgshpovR68 OWvKH81OLixCTEDA5Q9N/cDFZtjlG+LZfsGwmUuc8jSzIKvIPlxaJhVNPSORFVfOmxRvDApBV q0pv7bmXbcRm2auSWmwYIDOeNkgtk+w4nRNP90l+YFmWVyrz34+xFdUWXD+dcQCTvsqL0bQKy 8yTy9qhz3f9XwXI1UaaMzXNyXOUxNPb19r1RugwqCIBvLVadMqgjDwV1WlOBgx17Fi0nSQf32 j1878P1PJed2r0nrOeqFDuz5RAaC+mfmj9u7fhkUEyxOFie77ctYYBdS12VmHadX0x5+879Rb pdDGekKqAxXUoBPn X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Jun 2018 16:53:02 -0000 On Thu, 14 Jun 2018 08:47:52 +0100, Steve O'Hara-Smith wrote: > On Thu, 14 Jun 2018 05:59:16 +0530 > Manish Jain wrote: > > > On 06/14/18 05:09, jungle Boogie wrote: > > > Describe the problem you want to solve, not how. > > > > The problem is this: > > > > I am writing a shell script which can run fsck on all UFS / ext2 /ext4 > > hard disk partitions listed in /etc/fstab. > > The one mounted on / is going to be a problem in Linux. > > I'm not sure I see the point in such a script though, the boot time > checks have always been adequate in my experience but hey you want it so > fine. The core idea of FreeBSD's automatic boot is that fsck will be run automatically anyway if needed. If serious problems are being encountered, the SUM "emergency" shell will be invoked, inviting the user to re-run fsck with the desired option. The choice _which_ file systems to check, with the filesystem specification needed, is obtained from /etc/fstab. So in worst case, you could do # fsck -yf manually (no script needed), if you _know_ what you're doing with this command. In worst case, don't use -y right away. Of course this implies that the needed information is already present in /etc/fstab (the FreeBSD and Linux partitions need to have a correct entry each). > > The script should be portable and be able to run no matter whether the > > OS running is FreeBSD or Linux. The only thing that matters is that the > > commands fsck_ufs / fsck.ext2 and fsck.ext4 are available. > > Fine. Doesn't Linux have a similar built-in checking mechanism? > > Ideally, the script should run only under single user mode, or else bail > > out immediately. > > Why ? It would be far more sensible for the script to check each > filesystem to see if it is mounted and run fsck if it is not. That way it > will run correctly at any time, including in single user with filesystems > mounted which is a perfectly feasible condition. It's possible to check file systems in multi-user mode as well, but as you correctly mentioned, they need to be unmounted to allow modifications (if needed). I have such a script (even though for a different purpose), it checks like this: #!/bin/sh PARTITIONS="/usr /home /export /whatever" # ... some command-line flag checking here ... for PARTITION in ${PARTITIONS}; do MOUNTED=`mount | grep "on ${PARTITION}"` if [ "${MOUNTED}" != "" ]; then if [ `id -un` = "root" ]; then # ... # do "privileged" stuff here # ... fi echo "${PARTITION} ${STATUS}" else echo "${PARTITION} currently not operational" fi done > > Linux has a clean way to find out whether the system is in single user > > mode. I would think, no matter what others on this list have said, > > sysctl under FreeBSD too should have a variable for indicating single > > user mode. But there currently is not any. > > I can't think of any time when single user mode is the correct > thing to be checking for before doing something. It is typical do resort to single-user mode in case of severe problems (or at least when expecting them); using the kernel debugger to "surprisingly" reboot the machine with unclean filesystems and then going "boot -s", followed by a problem analysis in single user mode, with the correct steps taken (file recovery, file system repair, etc.). This is convenient as it makes perfectly sure no other programs are being run that _might_ change filesystem content, which in case of an upcoming recovery is the last thing you want to happen. -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...