summaryrefslogtreecommitdiffstats
path: root/rescue/Flash/scripts/rescue_common
blob: 77435e6da10fe0bc5fa9f9499b38090db2cb60d0 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash

function detect_version() {
    version=""
    if has_label Share; then
        version="1.0"
    elif has_label MDVUSBROOT; then
        version="1.05"
    elif has_label Mdv_Flash; then
        version="2.0"
    elif has_label MDV_FLASH; then
        version="2.05"
    fi
    usbroot_device=$_device
    source_version_variables

    [ -n "$version" ]
}

function source_version_variables {
    local _version
    _version=$1
    [ -z "$_version" ] && _version=$version
    if [ "$_version" = "1.0" ]; then
        label=MDVUSBROOT
        boot_rel="/boot"
        loopbacks_rel="/loopbacks"
    elif [ "$_version" = "1.05" ]; then
        label=MDVUSBROOT
        boot_rel="/.boot"
        loopbacks_rel="/.loopbacks"
    elif [ "$_version" = "2.0" ]; then
        label=Mdv_Flash
        boot_rel="/.boot"
        loopbacks_rel="/.loopbacks"
    elif [ "$_version" = "2.05" ]; then
        label=MDV_FLASH
        boot_rel="/.boot"
        loopbacks_rel="/.loopbacks"
    fi
}

function has_label() {
    local _label _tmpdir
    _label=$1

    _tmpdir="/tmp/$$"

    mkdir -p $_tmpdir

    nash --force > /dev/null 2>&1 <<EOF
mount -t vfat LABEL=$_label $_tmpdir
EOF

    rc=$?

    _device=`perl -ne 'my @l = split; $l[1] eq "'$_tmpdir'" and print "$l[0]\n"' /proc/mounts`
    [ -z "$_device" ] && rc=1

    umount $_tmpdir 2> /dev/null
    rmdir $_tmpdir 2> /dev/null

    return $rc
}

function check_vendor() {

    # list of supported vendors/models

    # mandriva flash 1.0 (france)
    vendors[0]="13fe"
    models[0]="1a00"
    # mandriva flash 1.0 (brazil)
    vendors[1]="0930"
    models[1]="653e"
    # mandriva flash 4GB (france)
    vendors[2]="13fe"
    models[2]="1d00"

    rc=1

    mount -t usbfs none /proc/bus/usb 2> /dev/null

    i=0
    while [ ! -z "${vendors[$i]}" ]; do

        grep "P:" /proc/bus/usb/devices | grep -q \
                "Vendor=${vendors[$i]} ProdID=${models[$i]}"
        if [ $? -eq 0 ]; then
            rc=0
            break
        fi

        let i++
    done

    umount /proc/bus/usb 2> /dev/null

    return $rc
}

function insert_pendrive() {

    _tmpdir="/tmp/rescue-temp"
    mkdir -p $_tmpdir

    modprobe usb-storage > /dev/null 2>&1
    modprobe vfat > /dev/null 2>&1

    echo
    echo -n 'Please insert your Mandriva Flash pen drive and press ENTER when ready: '
    read READY
    echo
    echo -n 'Detecting pen drive: '

    for ((i=0; $i <= 15 ; i++)); do
        detect_version && nash --force > /dev/null 2>&1 <<EOF
mount -t vfat LABEL=$label $_tmpdir
EOF
        if [ $? -ne 0 ]; then
            echo -n '.'
            sleep 1
        else
            echo -n ' found!'

            if ! check_vendor; then
                echo
                echo "This is not an official Mandriva Flash key!"
                echo
            fi

            cat /proc/mounts | grep $_tmpdir | cut -d' ' -f1 | \
                sed 's/[0-9]*$//g' > /tmp/rescue-device
            umount $_tmpdir
            rmdir $_tmpdir 2> /dev/null

            return 0
        fi
    done

    rmdir $_tmpdir 2> /dev/null

    echo
    echo
    echo "Couldn't detect Mandriva Flash pen drive!"
    echo

    return 1
}

function mount_usbroot() {

    nash --force > /dev/null 2>&1 <<EOF
mount -t vfat LABEL=$label $1
EOF
    if [ $? -ne 0 ]; then
        echo "Error mounting device labeled $label"
        return 1
    fi

    return 0
}

function mount_sharedroot() {

    nash --force > /dev/null 2>&1 <<EOF
mount -t vfat LABEL=Share $1
EOF
    if [ $? -ne 0 ]; then
        echo "Error mounting device labeled Share"
        return 1
    fi

    return 0
}

function progress() {

    echo -ne '\b|'
    usleep 100000
    echo -ne '\b/'
    usleep 100000
    echo -ne '\b-'
    usleep 100000
    echo -ne '\b\\'
    usleep 100000
}

function lumount() {
    local dir=$1
    loop=$(perl -ne '@l = split; if ($l[1] eq "'$dir'") { print $l[0]; exit }' /proc/mounts)
    umount $dir
    losetup -d $loop
}

function set_sfs_loop() {
    local dir=$1
    sfs_loop_std="$dir$loopbacks_rel/distrib.sqfs"
    sfs_loop_lzma="$dir$loopbacks_rel/distrib-lzma.sqfs"
    if [ -e $sfs_loop_lzma ]; then
	sfs_loop=$sfs_loop_lzma
    else
	sfs_loop=$sfs_loop_std
    fi
}