summaryrefslogtreecommitdiff
path: root/draft/other-tools/fog/fog.download
blob: 7190ebbf1e825886c3821b37fdb54bab925a181b (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash
. /usr/share/fog/lib/funcs.sh
. /bin/fog.donate
. /bin/fog.checkin
. /bin/fog.mount
. /bin/fog.checkmount
. /bin/fog.checkimgvar
hd=""
disks=""
disk=""
parts=""
part=""
imagePath="/images/$img"
origmac=$mac
mac=$(getMACAddresses | base64)
. /bin/fog.inventory "true"
mac="$origmac"
origmac=""
layPartSize="-1s"
fog.statusreporter "$mac" "$web" & disown
statusReporter="$!"
fixed_size_partitions=""
echo " * Using Image: $img"
preparePartitions() {
    echo " * Preparing Partition layout"
    case $imgType in
        [Nn])
            case $osid in
                4|50|51)
                    [[ ! -d $imagePath ]] && handleError "Unable to locate image store ($0)\n   Args Passed: $*"
                    prepareResizeDownloadPartitions "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
                    ;;
                [1-2])
                    [[ ! -f $imagePath && ! -f $imagePath/$img && ! -d $imagePath ]] && handleError "Unable to locate image store ($0)\n   Args Passed: $*"
                    [[ -d $imagePath && -f $imagePath/$img ]] && imagePath="$imagePath/$img"
                    if [[ -d $imagePath ]]; then
                        prepareResizeDownloadPartitions "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
                    else
                        startsector="63s"
                        restorePartitionTablesAndBootLoaders "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
                        dots "Removing partition"
                        parted -s $hd rm 1 >/dev/null 2>&1
                        if [[ ! $? -eq 0 ]]; then
                            echo "Failed"
                            debugPause
                            handleError "Fatal Error: Could not remove old partition ($0)\n   Args Passed: $*"
                        fi
                        echo "Done"
                        debugPause
                        echo " * Attempting to expand/fill partitions"
                        dots "Recreating partition"
                        parted -s $hd mkpart primary ntfs 63s -- $layPartSize >/dev/null 2>&1
                        if [[ ! $? -eq 0 ]]; then
                            echo "Failed"
                            debugPause
                            handleError "Could not create partition to fill disk ($0)\n   Args Passed: $*"
                        fi
                        echo "Done"
                        debugPause
                        dots "Setting boot partition"
                        parted -s $hd set 1 boot on >/dev/null 2>&1
                        if [[ ! $? -eq 0 ]]; then
                            echo "Failed"
                            debugPause
                            handleError "Fatal Error: Could not make partition bootable ($0)\n   Args Passed: $*"
                        fi
                        runPartprobe "$hd"
                        echo "Done"
                        debugPause
                        getPartitions "$hd"
                        for part in $parts; do
                            [[ -e $part ]] && break
                        done
                    fi
                    ;;
                [5-7]|9)
                    [[ ! -d $imagePath && ! -f $imagePath/sys.img.000 ]] && handleError "Unable to locate image store ($0)\n   Args Passed: $*"
                    if [[ ! -f $imagePath/sys.img.000 ]]; then
                        prepareResizeDownloadPartitions "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
                    else
                        echo " * Using legacy style partition setup"
                        win7partcnt=1
                        dots "Windows Boot Partition Exists"
                        if [[ ! -f $imagePath/gpt.bak && ! -f $imagePath/rec1.img.000 && ! -f $imagePath/rec.img.000 ]]; then
                            echo "No"
                        else
                            echo "Yes"
                            [[ -f $imagePath/rec.img.000 ]] && win7partcnt=2
                            [[ -f $imagePath/rec.img.001 || -f $imagePath/gpt.bak ]] && win7partcnt=3
                        fi
                        debugPause
                        echo " * Attempting to expand/fill partitions"
                        do_fill=0
                        fillDiskWithPartitionsIsOK "$hd" "$imagePath" 1
                        case $do_fill in
                            1)
                                fillDiskWithPartitions "$hd" "$imagePath" 1
                                echo "Done"
                                ;;
                            *)
                                startsector="2048s"
                                restorePartitionTablesAndBootLoaders "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
                                case $win7partcnt in
                                    1)
                                        dots "Removing partition"
                                        parted -s $hd rm 1 >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not remove old partition ($0)\n   Args Passed: $*"
                                        fi
                                        echo "Done"
                                        debugPause
                                        dots "Creating main partition"
                                        parted -s $hd mkpart primary ntfs $startsector -- $layPartSize >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not recreate first partition ($0)\n   Args Passed: $*"
                                        fi
                                        echo "Done"
                                        debugPause
                                        dots "Setting boot partition"
                                        parted -s $hd set 1 boot on >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not make partition bootable ($0)\n   Args Passed: $*"
                                        fi
                                        echo "Done"
                                        ;;
                                    2)
                                        dots "Removing main partition"
                                        parted -s $hd rm 1 >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not remove old main partition ($0)\n    Args Passed: $*"
                                        fi
                                        echo "Done"
                                        debugPause
                                        dots "Recreating recovery partition"
                                        parted -s $hd mkpart primary ntfs $startsector 206847s >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not create recovery partition ($0)\n    Args Passed: $*"
                                        fi
                                        echo "Done"
                                        debugPause
                                        dots "Recreating main partition"
                                        parted -s $hd mkpart primary ntfs $defaultpart2start -- $layPartSize >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not recreate main partition ($0)\n   Args Passed: $*"
                                        fi
                                        echo "Done"
                                        debugPause
                                        dots "Setting boot partition"
                                        parted -s $hd set 1 boot on >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not make partition bootable ($0)\nArgsPassed: $*"
                                        fi
                                        echo "Done"
                                        ;;
                                    3)
                                        dots "Removing partition data"
                                        sgdisk -Z $hd >/dev/null 2>&1
                                        sgdisk -gel $imagePath/gpt.bak $hd >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not recreate partitions ($0)\n   Args Passed: $*"
                                        fi
                                        echo "Done"
                                        debugPause
                                        dots "Recreating sized out partitions"
                                        sgdisk -x 3:$(sgdisk -E $hd) $hd >/dev/null 2>&1
                                        if [[ ! $? -eq 0 ]]; then
                                            echo "Failed"
                                            debugPause
                                            handleError "Fatal Error: Could not resize partitions ($0)\n   Args Passed: $*"
                                        fi
                                        echo "Done"
                                        ;;
                                esac
                                ;;
                        esac
                        debugPause
                        runPartprobe "$hd"
                        dots "Setting up partition variables"
                        getPartitions "$hd"
                        restoreparts=""
                        part_number=0
                        for part in $parts; do
                            getPartitionNumber "$part"
                            [[ $part_number -le $win7partcnt ]] && restoreparts="$restoreparts $part"
                        done
                        echo "Done"
                        debugPause
                    fi
                    ;;
            esac
            ;;
        mps)
            [[ ! -d $imagePath ]] && handleError "Unable to locate image store ($0)\n   Args Passed: $*"
            restorePartitionTablesAndBootLoaders "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
            runPartprobe "$hd"
            gptcheck="$global_gptcheck"
            ;;
        mpa)
            [[ ! -d $imagePath ]] && handleError "Unable to locate image store ($0)\n   Args Passed: $*"
            getHardDisk "true"
            disk_number=1
            for disk in $disks; do
                restorePartitionTablesAndBootLoaders "$disk" "$disk_number" "$imagePath" "$osid" "$imgPartitionType"
                runPartprobe "$disk"
                gptcheck="$global_gptcheck"
                let disk_number+=1
            done
            ;;
    esac
}
putDataBack() {
    runPartprobe "$hd"
    getPartitions "$hd"
    [[ -z $parts ]] && echo -e " * Seems like you are trying to restore to an empty disk. Be aware this will most probably cause trouble.\n"
    echo "   +--------------------------------+"
    echo "   |   Attempting to deploy image   |"
    echo "   +--------------------------------+"
    [[ $imgFormat -eq 1 || $imgLegacy -eq 1 ]] && echo "   |         Using Partimage        |" || echo "   |         Using Partclone        |"
    echo "   +--------------------------------+"
    [[ $mc == yes ]] && usleep 10000000 || usleep 3000000
    case $imgType in
        dd)
            restorePartition "$hd" 1 "$imagePath/$img.*" "$mc"
            ;;
        n|mps|mpa)
            [[ $imgType == +(n|mps) ]] && disks="$hd"
            case $osid in
                [1-2])
                    [[ ! -f $imagePath && ! -d $imagePath ]] && handleError "Fatal Error: Could not locate file ($0)\n   Args Passed: $*"
                    ;;
                [5-7]|9)
                    [[ ! -d $imagePath && ! -f $imagePath/sys.img.000 ]] && handleError "Fatal Error: Could not locate file ($0)\n   Args Passed: $*"
                    ;;
                4|50|51)
                    [[ ! -d $imagePath ]] && handleError "Fatal Error: could not locate file ($0)\n   Args Passed: $*"
                    ;;
            esac
            performRestore "$disks" "$imagePath" "$imgPartitionType" "$mc"
            ;;
    esac
}
findHDDInfo
[[ $nombr -eq 1 ]] && echo " * Skipping partition layout (Single Partition restore)" || preparePartitions
[[ $imgPartitionType != mbr ]] && putDataBack || echo " * Skipping partition restore (MBR Only)"
completeTasking