Access a File as a Device?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Access a File as a Device?
# 8  
Old 03-20-2012
Download the mmls tool from TSK (The Sleuth Kit) and run it against your disk image. It will output the starting and ending sectors of each partition and its length. You can then use dd to carve out each partition from the disk image using a block size of 512, the skip option to skip forward to the start of particular partition and count option to specify the number of blocks to copy (length).
# 9  
Old 04-09-2012
I had the same question, and was surprised not to have found any explanation on the web, so thanks to Corona688 for revealing it has a very simple answer.

As a result I have put together a quick script to automate the process. It is a bit rough and the images I have only contain one partition so I have not been able to test it with anything beyond the first partition. If there is a problem it will be with the larger numbers exceeding the shell's maximum integer size (which is also why I could not do the calculations in awk, as it only seems to support signed longints).

It works fine for me though with single partition USB stick images up to 16GB. I have called it mount-img and the usage is:

Code:
mount-img <image-file> <partition-number> <mount-point>

Code:
#!/bin/sh

eval MI_IMAGE="$1"

if [ ! -e "$MI_IMAGE" ]; then
   echo "${0##*/}: No such file or directory"
   exit
elif [ -d "$MI_IMAGE" ]; then
   echo "$1: Is a directory"
   exit
elif [ ! "$2" ]; then
   echo "${0##*/}: partition number not specified"
   exit
elif [ ! "$2" -ge "1" 2>/dev/null ]; then
   echo "${0##*/}: invalid partition number"
   exit
fi

MI_PART=$(( $2 + 1 ))
MI_INFO=`/sbin/fdisk -ul "$MI_IMAGE" 2>/dev/null | grep -E "Sector size \(logical/physical\):|^$MI_IMAGE"`
MI_SECTOR="${MI_INFO#*: }"
MI_SECTOR="${MI_SECTOR%% bytes*}"

if [ "$MI_PART" -gt "`echo "$MI_INFO" | wc -l`" ]; then
   echo "${0##*/}: partition number too high"
   exit
elif [ ! "$3" ]; then
   echo "${0##*/}: mount point not specified"
   exit
elif [ ! -e "$3" ]; then
   echo "${0##*/}: mount point $3 does not exist"
   exit
elif [ ! -d "$3" ]; then
   echo "${0##*/}: mount point $3 is not a directory"
   exit
fi

MI_OPTS=`echo "$MI_INFO" | awk -v l="$MI_PART" 'NR==l {printf "%s/%s",$(NF-4),$(NF-3)}'`
MI_OFFSET=$(( ${MI_OPTS%/*} * $MI_SECTOR  ))
MI_LIMIT=$(( (${MI_OPTS#*/} - ${MI_OPTS%/*}) * $MI_SECTOR  ))
mount -o loop,offset=$MI_OFFSET,sizelimit=$MI_LIMIT "$1" "$3"

This User Gave Thanks to mij For This Post:
# 10  
Old 04-09-2012
I'm glad my post was useful, and your script looks interesting. Though:

I'm not sure these structures:
Code:
elif [ ! "$3" ]; then

...do what you think they do. I'm not even sure what they're supposed to do. If you're trying to check if the string's blank, that would be -z

Also, a plain Bourne shell will never work with this code. Use #!/bin/bash or #!/bin/ksh to show which you designed it with
# 11  
Old 04-09-2012
As an FYI for future readers. This script will only work for disks with MBR labels. Will not work with GPT, VTOC or other disk labels.
# 12  
Old 04-10-2012
Thanks fmurphy for pointing out that important caveat. It could be easily adapted to use parted, but I do not have it installed on my current system. I have used it in a similar script in the past to find the offset for mounting HFS partitions in a GPT.

Quote:
Originally Posted by Corona688
I'm not sure these structures:
Code:
elif [ ! "$3" ]; then

...do what you think they do. I'm not even sure what they're supposed to do. If you're trying to check if the string's blank, that would be -z
The string is treated as an expression and so returns false if empty and true otherwise. I am not sure if there is any benefit of the -z and -n tests over this form, but I have always used this way as I find it more readable as it corresponds with other languages.

Quote:
Originally Posted by Corona688
Also, a plain Bourne shell will never work with this code. Use #!/bin/bash or #!/bin/ksh to show which you designed it with
You have got me curious as it works for me and I thought I had a proper version of sh. I am used to having to use different code for sh and bash scripts, but like to use sh where possible.

Going to check the version I discovered my sh is an alias to dash. I have no idea what the differences are between the two, but annoyingly it seems I have been writing for that (and systems where sh points to bash) all along. So on anything else it is best to change the shebang to bash.
# 13  
Old 04-10-2012
Dash is a no-thrills POSIX compliant shell, so it is highly recommendable to test script with this. If it runs in dash, you can be reasonably assured it will probably also run on bash, ksh93 or /bin/sh on any modern system. A well known exception is Solaris, which still uses a classic Bourne shell as /bin/sh, but it has a POSIX compliant /usr/xpg4/bin/sh that can be used instead..
On Linuces other than Ubuntu /bin/sh points to /bin/bash and this means that /bin/bash --posix is run, so bash runs in an almost-compliant (but not entirely) POSIX mode..
# 14  
Old 04-11-2012
Quote:
Originally Posted by mij
You have got me curious as it works for me and I thought I had a proper version of sh.
There is almost no such thing as "proper sh". On some systems, sh gets you ksh. On some systems, sh gets you bash. On Solaris, sh gets you something so old and mouldy it's not even POSIX. If you write a plain sh script it should work in almost anything, but it's all too easy to use non-sh features. Frankly, it's tempting, because some of them are just so convenient.

Like the math you've been using, $(( X + 5 )) sort of syntax. That's a bash/ksh thing and not available in pure POSIX bourne shells.

I'd recommend making this script BASH-only anyway, because some of the sector numbers etc. are going to be extremely high numbers, even beyond the 32-bit limit, and the only shell I know of which can tolerate these for integer math is BASH, and only relatively new BASH at that.

Quote:
The string is treated as an expression and so returns false if empty and true otherwise. I am not sure if there is any benefit of the -z and -n tests over this form, but I have always used this way as I find it more readable as it corresponds with other languages.
The benefit is that, if someone plugs strange values into it, it won't blow up or respond inappropriately. Leaving it open-ended like that means could feed something strange into it and cause syntax errors. Always tell [ ] what you want to do with the data inside them.

Last edited by Corona688; 04-11-2012 at 01:55 PM..
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