File: vhdpartx

package info (click to toggle)
blktap 2.0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,640 kB
  • ctags: 3,795
  • sloc: ansic: 34,134; sh: 1,138; makefile: 224
file content (109 lines) | stat: -rwxr-xr-x 2,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh

set -e

PARTUTIL=/usr/sbin/part-util
LIBVHDIO=/usr/lib/libvhdio.so.1.0

die()
{
    echo "$@"
    exit 1
}

usage()
{
    echo "usage: $0 [-a | -d | -l] vhd [lib]"
    echo "-a add partition mappings"
    echo "-d del partition mappings"
    echo "-l list partition mappings"
    exit 1
}

parse_args()
{
    part_util=$PARTUTIL

    while [ $# -ge 1 ]; do
	case $1 in
	    -a) add="TRUE" && count="1$count";;
	    -d) del="TRUE" && count="1$count";;
	    -l) list="TRUE" && count="1$count";;
	    *) if [ -z "$vhd" ]; then vhd=$1;
	       elif [ -z "$lib" ]; then lib=$1;
	       else usage;
	       fi;;
	esac
	shift
    done

    [ -z "$lib" ] && lib=$LIBVHDIO
    [ -z "$vhd" ] || [ "$count" != "1" ] && usage
    return 0
}

# screen-scraping of fdisk... not used
fdisk_read_partitions()
{
    local data=$(LD_PRELOAD=$lib fdisk -l $vhd)

    local none=$(echo $data | grep "This doesn't look like a partition table")
    [ -n "$none" ] && partitions=0 && return 0

    partitions=4
    while [ "$partitions" != "0" ]; do
	local hit=$(echo $data | grep "${vhd}$partitions")
	[ -n "$hit" ] && break
	partitions=$(($partitions - 1 ))
    done
}

part_util_read_partitions()
{
    partitions=$(LD_PRELOAD=$lib $part_util -c -i $vhd)
}

list_mappings()
{
    local parts=1
    while [ $parts -le $partitions ]; do
	echo ${vhd}$parts
	parts=$(($parts + 1 ))
    done
}

add_mappings()
{
    local parts=1
    local path=$(realpath $vhd)
    while [ $parts -le $partitions ]; do
	[ -e ${path}${parts} ] || ln -s $(basename $path) ${path}$parts
	parts=$(($parts + 1 ))
    done
}

del_mappings()
{
    local parts=1
    while [ $parts -le $partitions ]; do
	[ -L ${vhd}$parts ] && rm -f ${vhd}$parts
	parts=$(($parts + 1 ))
    done
}

main()
{
    parse_args $@
    [ -x $part_util ] || die "can't find part-util"
    [ -r $vhd && -r $lib ] || die "can't find vhd or lib"

    part_util_read_partitions

    [ -n "$add" ] && add_mappings
    [ -n "$del" ] && del_mappings
    [ -n "$list" ] && list_mappings

    return 0
}

main $@