Overlayfs: restore removed or changed file from "lower" - linux

OpenWrt uses overlayfs
# mount | grep overlay
/dev/ubi0_2 on /overlay type ubifs (rw,noatime)
overlayfs:/overlay on / type overlay (rw,noatime,lowerdir=/,upperdir=/overlay/upper,workdir=/overlay/work)
Somehow real lowerdir is /rom
# mount | grep rom
mtd:ubi_rootfs on /rom type squashfs (ro,relatime)
How to restore file to make it the as in /rom? Remove records from overlays: whiteouts and opaque dirs.
Workaround
cp /rom/path/to/file /overlay/upper/path/to/file

Remove file from /overlay/upper, and then remount rootfs.
rm /overlay/upper/my/file
mount -o remount /

Related

Load root filesystem from USB device

I'm trying to make a fast reboot to the other Linux system. First step is kernel loading, I make it with
sudo kexec --append='$(cat /proc/cmdline)' -l new_kernel.img --reuse-cmdline
sudo kexec -e
It works fine, but loads only kernel, not entire system.
How can I mount an *.img file with OS resources, located at USB as /? Preferable during kernel loading, but afterwards mount is still suitable. *.img format is not necessary, it can be unpacked before
As stark said, pivot root() was the call I was searching for. Commands to make a USB located at /dev/sdb1 a root directory:
sudo -s
mkdir /newroot
mount /dev/sdb1 /newroot
cd /newroot
mkdir oldroot
pivot_root . oldroot/
switch_root() deletes all files at the previous root dir, also there are few other differences, this answer might be useful

What happens if you mount to a non-empty mount point with fuse?

I am new to fuse. When I try to run a FUSE client program I get this error:
fuse: mountpoint is not empty
fuse: if you are sure this is safe, use the 'nonempty' mount option
I understand that a mountpoint is the directory where you will logically attach the FUSE filesystem. What will happen if I mount to this location? What are the dangers? Is it just that the directory will be overwritten? Basically: what will happen if you mount to a non empty directory?
You need to make sure that the files on the device mounted by fuse will not have the same paths and file names as files which already existing in the nonempty mountpoint. Otherwise this would lead to confusion. If you are sure, pass -o nonempty to the mount command.
You can try what is happening using the following commands.. (Linux rocks!) .. without destroying anything..
// create 10 MB file
dd if=/dev/zero of=partition bs=1024 count=10240
// create loopdevice from that file
sudo losetup /dev/loop0 ./partition
// create filesystem on it
sudo e2mkfs.ext3 /dev/loop0
// mount the partition to temporary folder and create a file
mkdir test
sudo mount -o loop /dev/loop0 test
echo "bar" | sudo tee test/foo
# unmount the device
sudo umount /dev/loop0
# create the file again
echo "bar2" > test/foo
# now mount the device (having file with same name on it)
# and see what happens
sudo mount -o loop /dev/loop0 test
Just add -o nonempty in command line, like this:
s3fs -o nonempty <bucket-name> </mount/point/>
Apparently nothing happens, it fails in a non-destructive way and gives you a warning.
I've had this happen as well very recently. One way you can solve this is by moving all the files in the non-empty mount point to somewhere else, e.g.:
mv /nonEmptyMountPoint/* ~/Desktop/mountPointDump/
This way your mount point is now empty, and your mount command will work.
For me the error message goes away if I unmount the old mount before mounting it again:
fusermount -u /mnt/point
If it's not already mounted you get a non-critical error:
$ fusermount -u /mnt/point
fusermount: entry for /mnt/point not found in /etc/mtab
So in my script I just put unmount it before mounting it.
Just set "nonempty" as an optional value in your /etc/fstab
For example:
## mount a bucket
/usr/local/bin/s3fs#{your_bucket_name} {local_mounted_dir} fuse _netdev,url={your_bucket_endpoint_url},allow_other,nonempty 0 0
## mount a sub-directory of bucket, Do like this:
/usr/local/bin/s3fs#{your_bucket_name}:{sub_dir} {local_mounted_dir} fuse _netdev,url={your_bucket_endpoint_url},allow_other,nonempty 0 0
force it with -l
sudo umount -l ${HOME}/mount_dir

Delete mounting directory after umount -l

I'm working on Linux OpenWrt where I have to mount and umount manually USB disks when they are attached to the router.
I'm using this script: http://wiki.openwrt.org/doc/howto/writable_ntfs#with.a.custom.hotplug.script to mount and unmount automatically USB disks the problem is that it doesn't delete the mounting directory after umount -l /dev/$device. My application on the router needs to check if the USB disk is present or not by checking if /mnt/sda1 exists or not
My question is: is it dangerous to add rm -r sda1 after umount -l /dev/$device, or is there a risk that rm -r sda1 will remove files in sda1 ?
It should be safe to delete the directory after the umount if you check for the success of the umount command before but I would suggest to change you script to check if the mountpoint it's listed in the mount table instead with mount | grep sda1

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 ).

Repairing fstab (read only /)

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

Resources