mount image containing single partition - linux

I made an image of a single partition from a disk containing multiple partitions using the following:
#ddrescue -d -r3 /dev/sdb3 sdb3.img sdb3.logfile
However, when I try to mount the image using the following:
mount -o loop,ro sdb3.img /media/mymount
I get the following error message:
Failed to read last sector (81919999): Invalid argument
HINTS: Either the volume is a RAID/LDM but it wasn't setup yet,
or it was not setup correctly (e.g. by not using mdadm --build ...),
or a wrong device is tried to be mounted,
or the partition table is corrupt (partition is smaller than NTFS),
or the NTFS boot sector is corrupt (NTFS size is not valid).
Failed to mount '/dev/loop0': Invalid argument
The device '/dev/loop0' doesn't seem to have a valid NTFS.
Maybe the wrong device is used? Or the whole disk instead of a
partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?
Any help to resolve the issue would be appreciated.
Regards

To mount a partition from an image file you can use kpartx.
To list the mappings the tool detects in the image file:
kpartx -l sdb3.img
Then to add the mappings:
kpartx -a sdb3.img
The partitions will then appear as devices under /dev/mapper, they can then be mounted normally.
See man kpartx and this blog.

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.

Recover Hibernated partition

I accidently formatted my ntfs windows partition with "mkfs.ext4".
I was able to recover it with testdisk but it seems that the windows partition was hibernated,
so whenever i tried to open windows it starts repairing disk errors which was taking too long,
so i manually chkdsk ,to which after some time it started telling -"unreadable sector........
which also took very long so i shut it down.
In kali linux whenever i tried to mount it with "mount /dev/sda3 /mnt -t ntfs -r"
it mounts but many of the folders are empty including windows,program files,Users.
I am new to linux,can you tell me steps to recover my files if possible windows...
Thanks in Advance.
Use sudo ntfsfix /dev/sdXY where XY is the partition name. ex:sda4. use gparted to find partition name. Then mount. It may help.
First check to see it the partition is mounted, it maybe mounted as Read-only. Then issue the mount command with the options.
sudo mount -t ntfs-3g -o remove_hiberfile /dev/yourWindowsPartition /media/yourUser/WindowsPartitionName

List all mounts in Linux

I know mount and df lists all mounts. But I would like to list the mounts which failed to establish on boot, too. How do I get them?
There is no such command, since there is no list of "attempted mounts". You can compare the current mount list (/etc/mtab) to the list of shares registered to be mounted though (/etc/fstab).
Alternatively you could try to grep through the system log files to find failed mount attempts.
You can use mount -a to mount all the mount points defined in the fstab.
If there is some kind of error mounting, you will get some warning. If the mount point is already mounted successfully, the command will do nothing with that mountpoint.
Mount errors should appear in dmesg.

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

KVM Virtual machine running Windows XP: How to get files from guest to host?

I'm running Ubuntu 10.04, and on it, kvm/qemu. I created a storage device with the 'raw' format and installed XP on it, so I assume the file has ntfs format. I have a file on the XP virtual machine that I want on the host. It's 2gigs, so I can't just use a zip drive or burn it to CD.
I tried mounting the file (winxp.img) using losetup:
$ sudo losetup /dev/loop1 winxp.img
$ sudo losetup -a
/dev/loop1: [0801]:40637460 (/home/robert/kvm/images/winxp.img)
$ sudo mount -t ntfs /dev/loop1 /home/robert/kvm/images/tmp
NTFS signature is missing.
Failed to mount '/dev/loop1': Invalid argument
The device '/dev/loop1' doesn't seem to have a valid NTFS.
Maybe the wrong device is used? Or the whole disk instead of a
partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?
I thought that would work. It didn't. Does anyone have another idea?
winxp.img and loop1 is not a single partition (which can be mounted), it is image of full hard disk with own partition table.
You should read partition table from loop1 with fdisk; compute offset of first partition and do:
sudo mount -o offset=N -t ntfs /dev/loop1 /home/robert/kvm/images/tmp
where N is offset in bytes.
Telepathic mode on
N is 32256
Telepathic mode off
and finally, google mode on (I'll google "offset 32256"):
http://en.wikibooks.org/wiki/QEMU/Images#Mounting_an_image_on_the_host
Linux and other Unix-like hosts can mount images created with the raw format type using a loopback device. From a root login (or using sudo), mount a loopback with an offset of 32,256.
mount -o loop,offset=32256 /path/to/image.img /mnt/mountpoint
In my opinion the generic and correct way is via libguestfs http://libguestfs.org/
If you master it, you can open every virtual image in any format and get files or even make snapshots

Resources