Statistics
| Branch: | Revision:

root / part / vhdpartx @ abdb293f

History | View | Annotate | Download (2 kB)

1
#!/bin/sh
2

    
3
set -e
4

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

    
8
die()
9
{
10
    echo "$@"
11
    exit 1
12
}
13

    
14
usage()
15
{
16
    echo "usage: $0 [-a | -d | -l] vhd [lib]"
17
    echo "-a add partition mappings"
18
    echo "-d del partition mappings"
19
    echo "-l list partition mappings"
20
    exit 1
21
}
22

    
23
parse_args()
24
{
25
    part_util=$PARTUTIL
26

    
27
    while [ $# -ge 1 ]; do
28
	case $1 in
29
	    -a) add="TRUE" && count="1$count";;
30
	    -d) del="TRUE" && count="1$count";;
31
	    -l) list="TRUE" && count="1$count";;
32
	    *) if [ -z "$vhd" ]; then vhd=$1;
33
	       elif [ -z "$lib" ]; then lib=$1;
34
	       else usage;
35
	       fi;;
36
	esac
37
	shift
38
    done
39

    
40
    [[ -z "$lib" ]] && lib=$LIBVHDIO
41
    [[ -z "$vhd" || "$count" != "1" ]] && usage
42
    return 0
43
}
44

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

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

    
53
    partitions=4
54
    while [[ "$partitions" != "0" ]]; do
55
	local hit=$(echo $data | grep "${vhd}$partitions")
56
	[[ -n "$hit" ]] && break
57
	let partitions=$partitions-1
58
    done
59
}
60

    
61
part_util_read_partitions()
62
{
63
    partitions=$(LD_PRELOAD=$lib $part_util -c -i $vhd)
64
}
65

    
66
list_mappings()
67
{
68
    local parts=1
69
    while [[ $parts -le $partitions ]]; do
70
	echo ${vhd}$parts
71
	let parts=$parts+1
72
    done
73
}
74

    
75
add_mappings()
76
{
77
    local parts=1
78
    local path=$(realpath $vhd)
79
    while [[ $parts -le $partitions ]]; do
80
	[[ -e ${path}${parts} ]] || ln -s $(basename $path) ${path}$parts
81
	let parts=$parts+1
82
    done
83
}
84

    
85
del_mappings()
86
{
87
    local parts=1
88
    while [[ $parts -le $partitions ]]; do
89
	[[ -L ${vhd}$parts ]] && rm -f ${vhd}$parts
90
	let parts=$parts+1
91
    done
92
}
93

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

    
100
    part_util_read_partitions
101

    
102
    [[ -n "$add" ]] && add_mappings
103
    [[ -n "$del" ]] && del_mappings
104
    [[ -n "$list" ]] && list_mappings
105

    
106
    return 0
107
}
108

    
109
main $@