qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Heike C. Zimmerer" <usenet04q4@gmx.net>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] Re: Mounting qemu virtual disk images from host linux os
Date: Tue, 18 Jan 2005 00:13:56 +0100	[thread overview]
Message-ID: <cshgs2$sd8$1@sea.gmane.org> (raw)
In-Reply-To: 41EC3687.20805@gsinet.net

Tom Sandholm <sandholm@gsinet.net> writes:

[ mounting disk partitions ]

You might want to try the following script I'm using for mounting
entire disk images.  I wouldn't say it's really finished, but it works
for me so far so I hope it might be of some use.

==========================================================================
~/bin/mount-hd-image
==========================================================================
#!/bin/sh
#
# Time-stamp: <2005-01-10 10:50:16 hcz>

pname=${0##*/}
usage() {
    iconv -f utf-8 <<EOF
Syntax: $pname [-t type] raw_image mountpoint
Function:  mount all partitions of a complete hd image
           optionally create a new one
opts:
        -c        create image if it doesn't exist
        -f        run fdisk on the image
        -m        make a filesystem on it
        -v        debug level (may be given multiple times)
        -x        Script debug (det -x)
EOF
}


if [ -z "$nopt" ]; then
    black=$(tput oc)$(tput sgr0) # Reset color + attribs
    bold=$(tput bold)
    red=$(tput setaf 1)
    green=$(tput setaf 2)
    blue=$(tput setaf 4)
    cyan=$(tput setaf 6)
fi 2>/dev/null

d_echo(){
    # $1: minimum debug level req'd for display
    # $2 ..: message
    if [ "$vopt" -ge "$1" ]; then
	shift
	echo "$@"
    fi
}

muss(){
    res=0
    d_echo 1 -e "$cyan$*$black"
    "$@" || abort "$*"
}

kann(){
    d_echo 1 -e "$cyan$*$black"
    "$@"
}

kann0(){
    d_echo 1 -e "$cyan$*$black"
    "$@" >/dev/null 2>&1
}

abort(){
    [ $# -gt 0 ] && echo -n "$pname: "
    for i in "$@"; do echo >&2 "$i"; done
    echo "$pname: aborted."
    exit 2
}

###################################################

vopt=0
dopt=/home/hcz/qemu
while getopts xvh-:fmc: argv; do
    case $argv in
        c) eval ${argv}opt="\$OPTARG";;
        f|m) eval ${argv}opt=\$argv;;
        v) eval ': $(('$argv'opt++))';;
        x) set -x;;
        *) usage; exit 0;;
    esac
done
shift $((OPTIND-1))

if [ $# -lt 2 ]; then
    abort "Not enough arguments"
    exit 1
fi

src="$1"
dest="$2"
shift; shift
mkdir -p "${dest##*/}" || abort "Can't create $dest"
uid=$(id -u)
gid=$(id -g)
losetup=/sbin/losetup
#types="vfat captive-ntfs ntfs"

if [ -n "$copt" ]; then
    if [ -f "$src" ]; then
        read -p "$src already exists.  Overwrite (Y/N*): "
        echo $REPLY | egrep -i '^ *y' || abort
    fi
    muss qemu-img create -f raw "$src" $copt
fi

if [ -n "$copt$fopt" ]; then
    muss /sbin/fdisk -C $(( $(stat "$src" -c "%s") / 255 / 63 /512)) "$src"
fi

x=$(/sbin/fdisk -ul "$src" 2>/dev/null)
[ -z "$x" ] && abort "Can't 'fdisk -ul $src'"
 echo $x | grep -q "This doesn't look like a partition table" &&
  abort "You selected a partition, but there is no valid partition table"
echo "Mounting harddisk image '$src' ..."
# win-b1              63    20964824    10482381    7  HPFS/NTFS
#      ()1            ()2   (                       ){3}  ()3
echo "$x" | grep -v 'Extended$' |
 sed -ne "s#^${src}p\?\([0-9]\+\)[ *]*\([0-9]*\) \+\([0-9]\++\? \+\)\{3\}\(.\+\)#\1 \2 \3 \4#p" |
 while read partno start id system; do
    d_echo 2 "p:$partno start:$start id:$id sys:$system"
    case $system in
        Extended) continue;;
        FAT*) types="msdos vfat"; mkfs=(/sbin/mkfs.msdos -F32);;
        HPFS/NTFS) types="captive-ntfs ntfs";;
        *) types=auto;;
    esac
    echo -n "Patition $partno: trying "
    mkdir -p "$dest/$partno"
    for type in $types; do
        echo -n "$type "
        res=99
        loop_dev=$($losetup -f) 
        [ -z "$loop_dev" ] && abort "Can't find a loop device"
        muss $losetup -o $((start * 512)) $loop_dev "$src"
        if [ -n "$mopt" ]; then
            [ -n "$mkfs" ] ||
               echo "Don't know how to make a $system file system"
            muss "${mkfs[@]}" $loop_dev
            # muss $losetup -d $loop_dev
        fi
        if kann0 sudo mount -o uid=$uid,gid=$gid,users \
                  -t  $type "$loop_dev" "$dest/$partno"; then
            kann0 sudo chmod a+xrw "$2"
            echo "... $type: ok."
            res=0
            break
        fi
    done
    [ "$res" -eq 0 ] || abort "Error mounting partition $partno"
done

  reply	other threads:[~2005-01-17 23:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-16 23:33 [Qemu-devel] Mounting qemu virtual disk images from host linux os Tom Sandholm
2005-01-17  0:42 ` Magnus Damm
2005-01-17  1:51   ` Tom Sandholm
2005-01-17  3:31     ` Mark Williamson
2005-01-17 15:26       ` Jim C. Brown
2005-01-17 21:59         ` Tom Sandholm
2005-01-17  4:01     ` Bartosz Fabianowski
2005-01-17 14:24       ` Jim C. Brown
2005-01-17 14:22   ` Jim C. Brown
2005-01-17 14:26 ` Jim C. Brown
2005-01-17 22:04   ` Tom Sandholm
2005-01-17 23:13     ` Heike C. Zimmerer [this message]
     [not found]     ` <87r7kjejij.fsf@tazzelwurm.hczim.de>
2005-01-17 23:50       ` [Qemu-devel] " Heike C. Zimmerer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='cshgs2$sd8$1@sea.gmane.org' \
    --to=usenet04q4@gmx.net \
    --cc=hcz@hczim.de \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).