Mounting block device linux

How can I mount a partition from dd-created image of a block device (e.g. HDD) under Linux?

How would I, if it is possible, mount /dev/sda1 from the image so that I’m able to read the contents? It’s not an option to clone the HDD again, I know how to do it if I had only cloned the single partition by itself. I hope it’s still possible with the current image.

7 Answers 7

Nowadays, there is a better way, no need to use offsets or kpartx anymore:

losetup --partscan --find --show disk.img mount /dev/loop0p1 /mnt 

to free up loop0, use after umount:

Having used kpartx first, which mounts the partitions like /dev/mapper/loop3p1 , I just want to point out that losetup creates the devices like /dev/loop0p1 . The answer notes that, but I read over it probably 10 times. :/

losetup —partscan —find —show disk.img might give you /dev/loop1 , in which case you’ll need mount /dev/loop1p1 /mnt .

I ran into this problem today and wanted to update the answers just as a reminder for myself. Instead of calculating the offset on your own, you can use a tool that provides you with mountable devices from a dd image: kpartx

In the given case, it would need something like

sudo kpartx -a image750.img sudo mount /dev/mapper/loop1p1 /mount/point -o loop,ro 

where loop1p1 stands for the first partition, loop1p2 for the second, etc.

You’ve got the first part: fdisk -l to find the start offset. Take that number, multiply by 512, and you’ll get the offset option to mount. So, for sda1 in your case, 5 * 512 = 2560. Then run the mount:

mount -o loop,offset=2560 -t auto /path/to/image.dd /mount/point 
sudo mkdir /path/to/dir/ mount -o loop example.img /path/to/dir/ 

The above should mount it under that directory.

Loopmounting is only part of the answer.

Look at http://wiki.edseek.com/guide:mount_loopback#accessing_specific_partitions_in_the_image for help on specifying the partition. I think mount -o loop,offset=32256 /path/to/image750.img /mnt will work for you. but you really should read the mentioned tutorial.

the offset looks wrong; that corresponds to a partition start of 63 (63 * 512 = 32256). the offset in baumgart’s answer looks more correct for this situation. the link is a good reference, but it’d be a better answer if you took the time to summarize the steps or commands needed for this procedure. thanks!

losetup -P automation

Method mentioned by https://superuser.com/a/684707/128124 (added in util-linux v2.21, added Ubuntu 16.04) , here are functions to automate it further. Usage:

$ los my.img /dev/loop0 /mnt/loop0p1 /mnt/loop0p2 $ ls /mnt/loop0p1 /whatever /files /youhave /there $ sudo losetup -l NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO /dev/loop1 0 0 0 0 /full/path/to/my.img $ # Cleanup. $ losd 0 $ ls /mnt/loop0p1 $ ls /dev | grep loop0 loop0 
los() ( img="$1" dev="$(sudo losetup --show -f -P "$img")" echo "$dev" for part in "$dev"?*; do if [ "$part" = "$p*" ]; then part="$" fi dst="/mnt/$(basename "$part")" echo "$dst" sudo mkdir -p "$dst" sudo mount "$part" "$dst" done ) losd() ( dev="/dev/loop$1" for part in "$dev"?*; do if [ "$part" = "$p*" ]; then part="$" fi dst="/mnt/$(basename "$part")" sudo umount "$dst" done sudo losetup -d "$dev" ) 

loop module max_part config

Читайте также:  One click install linux

Decent method before util-linux v2.21.

loop is a kernel module, built into the kernel in Ubuntu 14.04.

If you configure it right, Linux automatically splits up the devices for you.

cat /sys/module/loop/parameters/max_part 

says how many partitions loop devices can generate.

It is 0 by default on Ubuntu 14.04 which is why no auto-splitting happens.

To change it, we can either add:

to a file in /etc/modprobe , or:

GRUB_CMDLINE_LINUX="loop.max_part=31" 

to /etc/default/grub and then sudo update-grub .

After a reboot, when you do:

sudo losetup -f --show my.img 

it mounts the image to a /dev/loopX device, and automatically mounts the partitions to /dev/loopXpY devices.

So this is the most convenient method if you are willing to reboot.

Источник

Use A File As A Linux Block Device

Just like when creating a SWAP file, you can create a file on a disk and present it as a block device. The block device would have a maximum file size of the backing file, and (as long as it’s not in use) be moved around like a normal file. For example, I could create a 1GB file on the filesystem and make Linux treat the file as a disk mounted in /dev/. And guess what – that’s what we’re going to do.

Create a file and filesystem to use as a block device

First off, use dd to create a 1GB file on an existing disk that we’ll use for our storage device:

dd if=/dev/zero of=/root/diskimage bs=1M count=1024

Then ‘format’ the file to give it the structure of a filesystem. For this example we’re going to use ext4 but you could choose any filesystem that meets your needs.

You’ll be promoted with Proceed anyway?. Type y and press return to proceed with the process.

mke2fs 1.42.5 (29-Jul-2012) /root/diskimage is not a block special device. Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done

Mounting a loop device

Before mounting the file we need to check that there is a free /dev/loopX loopback device that we can use to represent our new block device.

Run the below command, and if there is any output then check if it’s one of your loop devices, which will more than likely reference /dev/loop as the mounted device. If you do have a reference to our loop device then see the below section on Unmounting a loop device, or choose a number higher than the highest listed loop device, for example: usually there are several loop devices, starting with loop0 and going up in value to loop1, loop2, and so on.

cat /proc/mounts | grep /dev/loop

Once you have the file that you’d like to mount and a free loop device then you can go ahead and mount the file as a block device. You have two options:

  1. Mount the file as a block device only
  2. Mount the file as a block device and mount the filesystem of it on a local mount point (eg. /mnt/mymountpoint).
Читайте также:  Pdf extract one page linux

For option 1; to only mount the file as a device in /dev/, run the below command and change /root/diskimage to the path of the file you’d like to mount. loop0 can also be incremented as explained above.

losetup /dev/loop0 /root/diskimage

If you’d like this to be remounted after a machine reboot then add the above line to the rc.local file.

losetup /dev/loop0 /root/diskimage

For option 2; to mount the file and the filesystem on it, use the mount command. You must have already created the mount point locally before running the command, as you would when mounting a disk or NFS share.

Then run the mount command and specify the loop device, the path of the file and the path to mount the filesystem on:

mount -o loop=/dev/loop0 /root/diskimage /mnt/mymountpoint

To check the file has been mounted you can use the df command:

df -h | grep mymountpoint /dev/loop0 976M 1.3M 924M 1% /mnt/mymountpoint

Unmounting a loop device

If you’ve mounted the filesystem on the block device using the mount command then make sure it’s unmounted before proceeding.

To then free the loop0 device (or which ever loop device you’ve used) you’ll need the losetup command with the d switch.

Источник

Mounting Block Devices

This pages discuses the advanced details and underlying operation. For general usage, see fstab.

Overview

The mounting of block devices is handled by the block-mount source package, which contains the block-mount and block-hotplug packages. block-mount contains the code that does the actual mounting, and the mounting via /etc/init.d/fstab (i.e. on boot rather than when device is hotplugged), and block-hotplug takes care of mounting devices when the device is recognized by the system (.e.g. when modules are loaded and the partition detected).

block-mount (binary package)

The block-mount binary package (i.e. the one you actually install, rather than the source package containing block-mount and block-hotplug ), contains three library scripts (in addition to /etc/init.d/fstab and the sample config file /etc/config/fstab ). These three scripts are: block.sh , mount.sh , and fsck.sh .

As of r26314 block-extroot and block-hotplug have been merged with block-mount . That means that once you install block-mount the scripts for extroot mounting and hotplug mounting are installed. With r36988 the original package block-mount was removed. Technically, the new package ubox replaced its functionality. For Fstab configuration, the new block-mount package now contains the executable block which facilitates this. You can run block . See Fstab configuration.

With the new block mount mechanism you can run block info to get the same output that blkid delivered (however it only returns info for filesystems it supports). You can do “block mount” to mount all devices (same as what /etc/init.d/fstab restart used to do. If you run “ block detect ” you will get a sample uci file for the currently attached block devices. That way you can do “ block detect | uci import fstab ” to store it

block info cannot detect btrfs (added r43868), xfs , jfs, ntfs, exfat, and some other FS. Use manual scripting to mount them.

root@OpenWrt:~# blkid /dev/sda1: TYPE="ext2" /dev/sda2: UUID="890c87d4-e276-4fb0-a34a-296db408d792" TYPE="ext4" /dev/sdb1: LABEL="OPENWRT-BTRFS" UUID="2412e056-a1d8-4710-bf0e-d54b8ff0662f" UUID_SUB="edd04b0f-ccf6-4978-9d76-1fa17921fe58" TYPE="btrfs" root@OpenWrt:~# block info /dev/sda1: VERSION="1.0" TYPE="ext2" /dev/sda2: UUID="890c87d4-e276-4fb0-a34a-296db408d792" VERSION="1.0" TYPE="ext4"

The new block-mount in Barrier Breaker

Usage: block

/dev/mtdblock2: UUID="0906f1b4-51688c99-666b11b5-71d70575" VERSION="4.0" TYPE="squashfs" /dev/mtdblock3: TYPE="jffs2" /dev/sda1: UUID="e81a771e-249f-4f9e-ab30-b2fb73789744" LABEL="overlay" NAME="EXT_JOURNAL" VERSION="1.0" TYPE="ext4" /dev/sda2: UUID="090b67fa-afbb-4771-8efd-7a515c742c18" LABEL="swap" VERSION="2" TYPE="swap" /dev/sda5: UUID="91f1-f7ed" LABEL="TRANSPORT" VERSION="FAT32" TYPE="vfat" /dev/sda6: UUID="b01791a5-647a-4ab0-9adf-5b626ee5407c" LABEL="daten" NAME="EXT_JOURNAL" VERSION="1.0" TYPE="ext4" /dev/sda7: UUID="9f822714-fb75-40c3-9382-f1df42343229" LABEL="rest" NAME="EXT_JOURNAL" VERSION="1.0" TYPE="ext4"
config 'global' option anon_swap '0' option anon_mount '0' option auto_swap '1' option auto_mount '1' option delay_root '5' option check_fs '0' config 'mount' option target '/mnt/sda1' option uuid 'e81a771e-249f-4f9e-ab30-b2fb73789744' option enabled '0' config 'swap' option uuid '090b67fa-afbb-4771-8efd-7a515c742c18' option enabled '0' config 'mount' option target '/mnt/sda5' option uuid '91f1-f7ed' option enabled '0' config 'mount' option target '/mnt/sda6' option uuid 'b01791a5-647a-4ab0-9adf-5b626ee5407c' option enabled '0' config 'mount' option target '/mnt/sda7' option uuid '9f822714-fb75-40c3-9382-f1df42343229' option enabled '0'

you can do “ block detect | uci import fstab ” to store it as a sample config file (already with UUID )

working/not working in Barrier Breaker as of 2015/01/30

info detect on boot on plug mount/umount 1) needs and
ext4 kmod-fs-ext4 libext2fs, kmod-fs-autofs4
swap ? ? ? . swap-utils
vfat kmod-fs-vfat kmod-nls-base, kmod-nls-cp437, kmod-nls-iso8859-1, kmod-nls-utf8
btrfs ✘ 2) kmod-fs-btrfs btrfs-progs

block-hotplug (binary package)

Block hotplug consists of three scripts, 10-swap , 20-fsck , and 40-mount . When a block devices is added these scripts are executed in the order listed. So, first the device is checked for being a swap section, or to attempt to mount as swap, if it is not a defined section for swap or mount (this is known as anon_swap or anonymous swap). Then 20-fsck checks if the device is listed as enabled_fsck and if so, attempts to check/repair the filesystem, and, finally, we check if the device should be mounted, either named, or anonymously (i.e. not listed in any section).

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website. OK More information about cookies

Self-registration in the wiki has been disabled.
If you want to contribute to the OpenWrt wiki, please post HERE in the forum or ask on IRC for access.

Except where otherwise noted, content on this wiki is licensed under the following license:
CC Attribution-Share Alike 4.0 International

Источник

Оцените статью
Adblock
detector