Repairing fstab (read only /) - linux

I had a typo in my fstab and it boots to a commandline but is readonly, I know what the error is but i can't change it because it's mounted as readonly. I want to mount the filesystem and make the changes. I know I can boot a live distro and edit it that way, but i was wondering if there was an easier way to do it.
It's debian lenny by the way.

Yes, if you end up in single-user mode with a readonly root, try:
mount / -o remount,rw
Maybe a -n is necessary, maybe not. That should remount the root fs read/write (assuming there's nothing wrong with it).

I know that question is outdated, but saw it, because nowadays got in the same trouble.
I broke my fstab manually (make a typo in parameter).
That was very easy to correct fstab from readonly mode. We must mount / in read-write mode.
If /etc/fstab is correct, you can simply type:
mount -n -o remount /
But if /etc/fstab is wrong (as it was in my case), you must give the device name and possibly the type, too: e.g.
mount -n -o remount -t extX /dev/hdaX /
Where extX is your filesystem type and /dev/hdaX -- is partition you use for your root mount point.
To see all your available partitions just type ls /dev/[sh]d*.

#mount -n -o remount,rw /
if /dev/sda1 is the real device, then do:
#mount -n -o remount,rw /dev/sda1 /

#troyane saved my hassle by providing
mount -n -o remount -t extX /dev/hdaX /
I was on Orangepi3 EMMC when I had a typo in UUID of root having ext4 filesystem.
The command remounted in read-write mode and I fixed my fstab
mount -n -o remount -t ext4 /dev/mmcblk2p1 /

My kernel command line looks like this:
$ cat /proc/cmdline
root=/dev/sda4 ro
$
Tell grub that it should omit passing "ro" to the kernel when booting (pressing esc, e on the entry you want edit will allow you to edit the arguments given to the kernel), and it will mount your root file system not read only anymore. Then you can change your /etc/fstab and restart.

In my case, in grub2 after pressing "e" I have changed "ro" to "rw init=/bin/bash" then the root filesystem is mounted read and write so I could change the content of /etc/fstab file.

If you have something wrong with your /etc/fstab file. Please follow the following steps.
boot your ubuntu system or restart your computer
since you can not start your system, you will encounter some errors like
“a start job is running for dev-disk-by....
(you may need pressing F2 key to come the linux command terminal to see this)
use command vi /etc/fstab and edit your fstab file
use # to commend out some problems and add something you want put in the fstab file.
finish editing. hit shift+z and save the modification
use command reboot to restart your system
it works again
Please comment here, if you have questions

Related

How do I edit fstab in emergency mode?

I moved my EFI partition which caused me to enter emergency mode on reboot.
mount -a confirmed fstab had the the UUID of the old partition...it wasn't mounting and that caused emergency mode.
You can't do ANY of the standard remount,rw tricks that normally work... it will always give you an error message the mount doesn't work
Sure, I could start from the USB stick and edit /etc/fstab, but isn't there an easier way?
This question has been around for 10 years and most people answer with a remount as rw, but that always fails.
The clever way is simply mount the / partition on /mnt like:
mount /dev/sda1 /mnt
This mounts it read-write and you just edit /mnt/etc/fstab to change the new UUID for your partition which you can get from either blkid or ls -lha /dev/disk/by-uuid
However, the readonly filesystem will NOT see your changes, so you think you've failed. You'll look at /etc/fstab and it will appear unchanged (at least under btrfs it looks unchanged).
However, when you reboot, you are back in business.
I re-sized my EFI partition down to 260MB, but had to remove it to do that which causes the UUID to change. Just changing the /etc/fstab with the new UUID is all that you need to do so you don't run into trouble. Best to do that when you move the partition, rather than after the fact.
I have RHEL 7.9 installed and here is what I did to edit the fstab after copying from one machine to another using scp.
mount -o remount,rw /dev/sda2 #sda2 is where my root directory is located.
I was then able to open in vim and save the UUID changes I had to make. Worked like a charm.

How to change grub.conf after adding init=/bin/bash

I added init=/bin/bash in grub.conf file, so my linux system is always entering bash when booted.
Is there a way to change the grub.conf file so that I can boot Linux again.
TLDR; e, remove init=. ctrl+x
One of the options is to temporary change grub entry to fix booting problem, edit grub.conf and update-grub.
Keep shift pressed during boot (if you don't see grub menu)
Press e to edit menu item
Edit to remove init=... phrase
Press ctrl+x to boot
Now you can edit grub.conf
Run update-grub
reboot
After making changes to grub file run :
sudo update-grub
This will update your grub
TLDR; mount -o remount,rw /dev/sda
It's possible that after booting with init=/bin/bash your filesystem is in readonly state. That means you cannot modify any config files.
Try to remount it as read-write (my disk is at /dev/sda). If you don't know the name, use lsblk
mount -o remount,rw /dev/sda
update-grub will not work if /boot is not mounted
mount /boot
Now you can edit grub.conf and run update-grub

How to mount a qnx partition as read -write enabled only for executing particular lines of code?

ie the partition of interest is already mounted as read-only.the partition need to be mounted as a rw enabled partition for executing particular lines of script alone.After that the partition should go to it's previous state of read only.
Question is for QNX operating system. And correct way to remount partition as read/write can be done using below command.
mount -uw /
To remout a partition read-write:
mount /mnt/mountpoint -oremount,rw
and to remout read-only
mount /mnt/mountpoint -oremount,ro
you may be interested in remount option.
for example, this command is widely used in rooted androids.
mount -o remount,rw /system
mount -o remount,ro /system
mount(8) - Linux man page
Filesystem Independent Mount Options
remount
Attempt to remount an already-mounted filesystem. This is commonly used to change the mount flags for a filesystem, especially to make a readonly filesystem writeable. It does not change device or mount point.
The remount functionality follows the standard way how the mount command works with options from fstab. It means the mount command doesn't read fstab (or mtab) only when a device and dir are fully specified.
mount -o remount,rw /dev/foo /dir
After this call all old mount options are replaced and arbitrary stuff from fstab is ignored, except the loop= option which is internally generated and maintained by the mount command.
mount -o remount,rw /dir
After this call mount reads fstab (or mtab) and merges these options with options from command line ( -o ).

How to access fstab at reboot phase

How can I access and modify /etc/fstab during reboot (maybe accessing to recovery mode) ?
Why this question :
I have updated the virtualbox guests additions in backtrack 5 (as a Virtualbox VM).
I have modified temporary the fstab file to do that but forgotten to turn it back to initial state, like :
mount /dev/cdrom /cdrom
echo "/dev/cdrom /cdrom0 udf,iso9660 defaults,exec 0 0" >> /etc/fstab
So after the guest additions installed, reboot is blocked on mounting this unexisting point.
Thx
edit: maybe to move to serverfault ?
I see two possible solutions to your problem:
Use the recovery mode (if you don't see that in the grub list, edit the default entry and add the word "single" without quotes to the end of the kernel line)
Mount a random .iso file temporarily
Then remove the offending fstab line.
Another option is to boot a Live OS and mount the root filesystem from there. Then you will be able to edit the fstab file.
Once you have the live OS booted
fdisk -l
-find your root partition (usually in the format sda1, sdc1, sda2, etc)
mkdir /mnt/rootfs
mount /dev/sda2 /mnt/rootfs <--sda2 = root partition found in fdisk
cd /mnt/rootfs/etc
-Fstab file should be in there.

How do you force a CIFS connection to unmount

I have a CIFS share mounted on a Linux machine. The CIFS server is down, or the internet connection is down, and anything that touches the CIFS mount now takes several minutes to timeout, and is unkillable while you wait. I can't even run ls in my home directory because there is a symlink pointing inside the CIFS mount and ls tries to follow it to decide what color it should be. If I try to umount it (even with -fl), the umount process hangs just like ls does. Not even sudo kill -9 can kill it. How can I force the kernel to unmount?
I use lazy unmount: umount -l (that's a lowercase L)
Lazy unmount. Detach the filesystem
from the filesystem hierarchy now, and
cleanup all references to the
filesystem as soon as it is not busy
anymore. (Requires kernel 2.4.11 or
later.)
umount -a -t cifs -l
worked like a charm for me on CentOS 6.3. It saved me a server reboot.
On RHEL 6 this worked:
umount -f -a -t cifs -l
This works for me (Ubuntu 13.10 Desktop to an Ubuntu 14.04 Server) :-
sudo umount -f /mnt/my_share
Mounted with
sudo mount -t cifs -o username=me,password=mine //192.168.0.111/serv_share /mnt/my_share
where serv_share is that set up and pointed to in the smb.conf file.
I had this issue for a day until I found the real resolution. Instead of trying to force unmount an smb share that is hung, mount the share with the "soft" option. If a process attempts to connect to the share that is not available it will stop trying after a certain amount of time.
soft Make the mount soft. Fail file system calls after a number of seconds.
mount -t smbfs -o soft //username#server/share /users/username/smb/share
stat /users/username/smb/share/file
stat: /users/username/smb/share/file: stat: Operation timed out
May not be a real answer to your question but it is a solution to the problem
There's a -f option to umount that you can try:
umount -f /mnt/fileshare
Are you specifying the '-t cifs' option to mount? Also make sure you're not specifying the 'hard' option to mount.
You may also want to consider fusesmb, since the filesystem will be running in userspace you can kill it just like any other process.
Try umount -f /mnt/share. Works OK with NFS, never tried with cifs.
Also, take a look at autofs, it will mount the share only when accessed, and will unmount it afterworlds.
There is a good tutorial at www.howtoforge.net
I had a very similar problem with davfs. In the man page of umount.davfs, I found that the -f -l -n -r -v options are ignored by umount.davfs. To force-unmount my davfs mount, I had to use umount -i -f -l /media/davmount.
umount -f -t cifs -l /mnt &
Be careful of &, let umount run in background.
umount will detach filesystem first, so you will find nothing abount /mnt. If you run df command, then it will umount /mnt forcibly.
Approaching this problem sideways:
If you can't unmount because the filesystem is busy, is your ssh/terminal session cd'd into the mount directory, therefore making the filesystem busy?
For me, the solution was to cd into my home, then sudo umount worked flawlessly.
cd ~
umount /path/to/my/share
I would post this as a comment, but I have insufficient reputation. Hoping to spare someone else the forehead slap.
I experienced very different results regarding unmounting a dead cifs mount and found several tricks to bypass the problem temporarily.
Let's start with the mountpoint command. It can be useful to analyze the status of a mount:
mountpoint /mnt/smb_share
Usually it returns is a mountpoint or / is not a mountpoint.
But it can even return:
No such device
Transport endpoint is not connected
<nothing / stale>
For every result expect of is not a mountpoint there is a chance of unmounting.
You could try the usual way:
umount /mnt/smb_share
or force mode:
umount /mnt/smb_share -f
But often the force does not help. It simply returns the same nasty device is busy message.
Then the only option is to use the lazy mode:
umount /mnt/smb_share -l
BUT: This does not unmount anything. It only "moves" the mount to the root of the system, which can be seen as follows:
# lsof | grep mount | grep cwd
mount.cif 3125 root cwd unknown / (stat: No such device)
mount.cif 3150 root cwd unknown / (stat: No such device)
It is even noted in the documentation:
Lazy unmount. Detach the filesystem from the file hierarchy
now, and clean up all references to this filesystem as soon
as it is not busy anymore.
Now if you are unlucky, it will stay there forever. Even killing the process probably does not help:
kill -9 $pid
But why is this a problem? Because mount /mnt/smb_share does not work until the lazy unmounted path is really cleaned up by the Linux Kernel. And this is even mentioned in the documentation of umount. "lazy" should only be used to avoid a long shutdown / reboot times:
A system reboot would be expected in near future if you’re
going to use this option for network filesystem or local
filesystem with submounts. The recommended use-case for
umount -l is to prevent hangs on shutdown due to an
unreachable network share where a normal umount will hang due
to a downed server or a network partition. Remounts of the
share will not be possible.
Workarounds
Use a different SMB version
If you still have hopes that the lazy unmounted path will ever be not busy anymore and cleaned up by the Linux Kernel or you can't reboot at the moment, then you are maybe lucky and your SMB server supports different protocol versions. By that we can use the following trick:
Lets say you mounted your share as follows:
mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw
By that Linux automatically tries the maximum support SMB protocol version. Maybe 3.1. Now, you can force this version and it won't mount as expected:
mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw,vers=3.1
But then simply try a different version:
mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw,vers=3.0
or maybe 2.1:
mount.cifs //smb.server/share /mnt/smb_share -o username=smb_user,password=smb_pw,vers=2.1
Change the IP of the SMB server
If you are able to change the IP address or add a second IP to your SMB server, you can use this to mount the same server.
Dirty: Forward the traffic
Lets say the SMB server has the IP address 10.0.0.1 and the mount is really dead. Then create this iptables rule:
iptables -t nat -A OUTPUT -d 10.0.0.250 -j DNAT --to-destination 10.0.0.1
Now change your mount rule accordingly, so it mounts the samba server through IP 10.0.0.250 instead of 10.0.0.1 and voila, its mounted without server reboot. Dirty, but it works. PS This rule does not survive a reboot, so you should mount the SMB server manually and leave the /etc/fstab as usual.
More debugging
If you want to check if samba connection itself is theoretically working, you could try to list all SMB shares of the server through SMB3 as follows:
smbclient //smb.server -U "smb_user" -m SMB3 -L
or to view the content of a share with SMB1:
smbclient //smb.server -U "smb_user" -m NT1 -c ls
On RHEL 6 this worked for me also:
umount -f -a -t cifs -l FOLDER_NAME
A lazy unmount will do the job for you.
umount -l <mount path>

Resources