Access a File as a Device?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Access a File as a Device?
# 1  
Old 03-17-2012
Access a File as a Device?

I backed up my 320GB hard drive to a file with dd:

Code:
dd if=/dev/sda of=dev_sda.17-Mar-2012 bs=1048576

The main idea was to be able to be able to completely replace my hard drive from this backup if necessary, but I'd also like to be able to restore individual files. I realize I could use this dd technique to backup each partition separately (there are six partitions on there), and then use the loopback device to mount those individual files, but I'd like to just have a single backup of the entire disk that can be used to both replace the disk and to restore individual files.

So, I'm imagining making this file available as some new device file, say, /dev/sdax, and then seeing /dev/sdax1, /dev/sdax5, /dev/sdax6, etc, and then being able to mount each of those. Or, how else can I get at the individual files in that 320GB backup file?
# 2  
Old 03-17-2012
What's your Operating System? Syntax for creating loop devices changes over Operating Systems (Linux uses losetup while Solaris uses lofiadm).

Anyway, assuming you are using Linux, you may execute the following command to mount the filesystem image:

Code:
# first check which loop device is free; it shows the first free loop device 
losetup -f

# connect the loop device (say loop0) to the image
losetup /dev/loop0 /path/to/dev_sda.17-Mar-2012

mount /dev/loop0 /mnt

# when you are done, unmount the filesystem and detach the loop device

umount /mnt
losetup -d /dev/loop0

Or, you can do this with one command:

Code:
mount -t ext3 -o loop /path/to/dev_sda.17-Mar-2012 /mnt

# 3  
Old 03-17-2012
Quote:
What's your Operating System?
Linux.

Code:
 mount -t ext3 -o loop /path/to/dev_sda.17-Mar-2012 /mnt

I've tried that (actually, -t ext4), but:

Code:
mount: wrong fs type, bad option, bad superblock on /dev/loop0,
       missing codepage or helper program, or other error

I also tried without -t. I think the reason is that I used dd to copy the entire disk, not individual partitions. That is, I used "if=/dev/sda" as opposed to "if=/dev/sda1". /dev/sda itself doesn't have a filesytem, right?

I could copy at the partition level, but I want to copy at the disk level because I want to get the boot sector so I can use this backup to replace the entire hard drive, but I also want to use this backup to restore individual files.
# 4  
Old 03-18-2012
You will have to use either dd to extract data from master image file (from to per partition) or use mount's offset option.

Tools are : sfdisk,parted or fdisk ran against full image (to get information about the it), mount and/or dd.

Find examples online.

Regards
Peasant.
# 5  
Old 03-20-2012
Quote:
Originally Posted by Peasant
use mount's offset option.
...
parted ... ran against full image (to get information about it)
That sounds promising. So:

Code:
# losetup /dev/loop0 /path/to/dev_sda.17-Mar-2012
# parted /dev/loop0     
...
(parted) p                                                                
Model:  (file)
Disk /dev/loop0: 320GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
 1      1049kB  16.0GB  16.0GB  primary   ext4            boot
 2      16.0GB  320GB   304GB   extended
 5      16.0GB  80.0GB  64.0GB  logical   ext4
 6      80.0GB  144GB   64.0GB  logical   ext4
 7      144GB   160GB   16.0GB  logical   linux-swap(v1)
 8      160GB   176GB   16.0GB  logical   ext4
 9      176GB   320GB   144GB   logical   ext4

Now, I guess I just can't get the offset right. Trying to mount partition number 5:

Code:
# losetup -o 16GB /dev/loop1 /path/to/dev_sda.17-Mar-2012
# mount /dev/loop1 /mnt                                                                               
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've tried "16GiB" and "16G" also.
# 6  
Old 03-20-2012
You don't need to use losetup to use loop devices anymore, just -o loop.

A wild guess of 16GB isn't going to have the precision necessary to hit the beginning of a partition. You should be giving it a length, too. fdisk -ul to make it display in 512-byte blocks, and then use those to figure out the exact offset.
# 7  
Old 03-20-2012
Here's an example using an old disk image I happened to have around:

Code:
$ /sbin/fdisk -ul gorgon-aug-7.img
You must set cylinders.
You can do this from the extra functions menu.

Disk gorgon-aug-7.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

           Device Boot      Start         End      Blocks   Id  System
gorgon-aug-7.img1   *          63       98783       49360+  83  Linux
gorgon-aug-7.img2           98784     1099727      500472   82  Linux swap / Solaris
Partition 2 has different physical/logical endings:
     phys=(1023, 15, 63) logical=(1090, 15, 63)
gorgon-aug-7.img3        14772240    39102335    12165048    5  Extended
Partition 3 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 15, 63) logical=(14655, 0, 1)
Partition 3 has different physical/logical endings:
     phys=(1023, 15, 63) logical=(38791, 15, 63)
gorgon-aug-7.img4         1099728    14772239     6836256   83  Linux
Partition 4 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 15, 63) logical=(1091, 0, 1)
Partition 4 has different physical/logical endings:
     phys=(1023, 15, 63) logical=(14654, 15, 63)
gorgon-aug-7.img5        14772303    16237871      732784+  83  Linux
gorgon-aug-7.img6        16237935    39102335    11432200+  83  Linux

Partition table entries are not in disk order

# Partition 1 starts at 63.  In bytes that's 63*512 = 32256 bytes.
# Partition 1 ends at 98783, so is 98783-63 long, i.e. 50544640 bytes.
$ sudo mount -o loop,offset=32256,sizelimit=50544640 gorgon-aug-7.img mnt
$ ls mnt
System.map-2.6.34-gentoo-r1  grub        vmlinuz-2.6.34-gentoo-r1
boot                         lost+found
config-2.6.34-gentoo-r1      memtest86
$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Failed to open tape device /dev/rmt/0mn:Device busy (errno = 16)

Hi, Unable to make tape backup, please help. /opt/ignite/bin/make_tape_recovery -a /dev/rmt/?mn -I -v -m tar -x inc_entire=vg00 * Creating local directories for configuration files and archive. ======= 04/25/16 16:28:08 IST Started /opt/ignite/bin/make_tape_recovery. (Mon... (4 Replies)
Discussion started by: anuragr
4 Replies

2. UNIX for Dummies Questions & Answers

Exclusive access for few IPs to NTP device

How to provide a client exclusive access to the NTP device or NTP server. Example: 1. Configured md5 authentication for a subnet added below restriction line to the subnet as below in ntp.conf file. Also configured the keys and md5 authentication working . restrict 192.168.1.0 mask... (1 Reply)
Discussion started by: iqtan
1 Replies

3. Ubuntu

Cannot access or boot encrypted drive (gave up waiting for root device...)

I cannot access or boot from my C drive. I'm running Zorin 9 and the drive is a Samsung SSD. The disk was encrypted on install, and that has not given me any problems before. When I start the system it gets to the memory test page, and does not then load the password prompt, which it used to.... (1 Reply)
Discussion started by: David4321
1 Replies

4. Cybersecurity

Can't access my device DJI Phantom 3 which uses UNIX. Need Help please!

Equipment: DJI Phantom 3 I have the root and passwords access, but I cannot find out how to access the equipment. There is a USB port going to a miniUSB that connects to the equipment, but on Windows is detecting the connection as being a Serial Port (COM3). I need some help in order to gain... (5 Replies)
Discussion started by: nobr3ga
5 Replies

5. UNIX for Advanced & Expert Users

How the user process can access the character device loaded by my module

I am trying to load into the kernel a system-call dynamically (without restarting the kernel and compailing it) in an attempt to (once in kernel mode) write to user process's memory. (I know there is a way to do this with the ptrace interface but it is not an option.) I know the only way to... (1 Reply)
Discussion started by: hopelessProgram
1 Replies

6. OS X (Apple)

Not mounted, no-driver USB device in terminal (how to access?)

hi, i am on a quest to access and even mount if possible a drive on os x. there is no driver for the device, but it lists fine in the system profiler. can i access its location from the terminal? how? here is what i get on the system profiler: Speed: Up to 480 Mb/sec Manufacturer: SAMSUNG ... (3 Replies)
Discussion started by: sontarieh
3 Replies

7. Homework & Coursework Questions

The pseudo-device provides a “backdoor” for gaining root access for a particular user.

Problem statement. In this part of the assignment, delegates will create a pseudo-device and write a device driver for it. The pseudo-device provides a “backdoor” for gaining root access for a particular user. Instead of compiling the device driver into the kernel, delegate will create a module.... (1 Reply)
Discussion started by: nyjilgeorge1
1 Replies

8. UNIX for Dummies Questions & Answers

controll access to a device

Hello everyone, I write a program (Linux & Solaris) that will run as non-root user, but the program must have rw access to a device /dev/ipmi (on linux) or /dev/bmc (on solaris). What is the standard way of granting such access? Linux: chmod on /dev/ipmi ? suid root my program? Solaris:... (1 Reply)
Discussion started by: Pavel.Bures
1 Replies

9. Linux

attempt to access beyond end of device

Hi, we have running 8 box sles 9 cluster and on an nfs filesystem we have the problem which is grepped from /var/log/messages. Jun 8 13:40:46 qnclpx02 kernel: attempt to access beyond end of device Jun 8 13:40:46 qnclpx02 kernel: sdat: rw=0, want=8894615912, limit=314572800 Is there... (1 Reply)
Discussion started by: ortsvorsteher
1 Replies

10. Linux

Non exclusive sound device access!!

Hi, I was wondering if any of you guys know of way to make applications that use sound device on linux to access it in a "non-exclusive manner", the aim is to be able to use more than one application that requires the sound device. Thanks (0 Replies)
Discussion started by: andryk
0 Replies
Login or Register to Ask a Question