summaryrefslogtreecommitdiff
path: root/draft/other-tools/fog/fog.upload
blob: 8a80ae8d7e2e021aeb5b1680c6c1d2e3f5604042 (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
#!/bin/bash
. /usr/share/fog/lib/funcs.sh
. /bin/fog.checkin
. /bin/fog.mount
. /bin/fog.checkmount
. /bin/fog.checkimgvar
imagePath="/images/$macWinSafe"
parts=""
part=""
disks=""
disk=""
hd=""
echo " * Preparing to send image file to server"
percent="$pct"
[[ $pct -lt 5 || $pct -ge 100 ]] && percent=5
[[ $pct -lt 10 ]] && percent="0$pct"
fog.statusreporter "$mac" "$web" & disown
statusReporter="$!"
prepareUploadLocation "$imagePath"
echo " * Using Image: $img"
# resizable image type
#  discover windows partitions
#  remove pagefile and hibernate file
#  validate XP partition location
#  save original partition table
#  save MBR
#  shrink filesystems and partitions
#  save GRUB
#  save shrunken partition table
#  clone filesystems
#  restore original MBR and partition table
#  expand filesystems
beginUpload() {
    case $imgType in
        [Nn])
            validResizeOS
            runPartprobe "$hd"
            getPartitions "$hd"
            [[ $osid == @([1-2]|[4-7]|9) ]] && win7partcnt=$(echo $parts | wc -w)
            dots "Checking for fixed partitions"
            part_number=0
            for part in $parts; do
                fsTypeSetting "$part"
                getPartitionNumber "$part"
                case $fstype in
                    ntfs|extfs)
                        continue
                        ;;
                    *)
                        fixed_size_partitions="$fixed_size_partitions:$part_number"
                        ;;
                esac
            done
            echo "Done"
            debugPause
            dots "Getting Windows/Linux Partition Count"
            countPartTypes "$hd" "ntfs" "ntfscnt"
            countPartTypes "$hd" "extfs" "extfscnt"
            if [[ $ntfscnt -eq 0 && $extfscnt -eq 0 ]]; then
                echo "Failed"
                debugPause
                handleError "No resizable partitions found ($0)\n   Args Passed: $*"
            fi
            echo "Done"
            debugPause
            echo " * NTFS Partition count of: $ntfscnt"
            debugPause
            echo " * EXTFS Partition count of: $extfscnt"
            debugPause
            case $osid in
                [4-7]|9|50|51)
                    echo " * Setting up any additional fixed parts"
                    [[ $((ntfscnt + extfscnt)) -gt 0  && $part_number -gt 1 ]] && fixed_size_partitions="$fixed_size_partitions:1"
                    part_number=0
                    for part in $parts; do
                        getPartitionNumber "$part"
                        fsTypeSetting "$part"
                        [[ $fstype != ntfs ]] && continue
                        dots "Mounting partition ($part)"
                        if [[ ! -d /bcdstore ]]; then
                            mkdir -p /bcdstore >/dev/null 2>&1
                            case $? in
                                0)
                                    ;;
                                *)
                                    echo "Failed"
                                    debugPause
                                    handleError " * Could not create mount location ($0->${FUNCNAME[0]})\n    Args Passed: $*"
                                    ;;
                            esac
                        fi
                        ntfs-3g -o remove_hiberfile,rw $part /bcdstore >/tmp/ntfs-mount-output 2>&1
                        case $? in
                            0)
                                echo "Done"
                                debugPause
                                ;;
                            *)
                                echo "Failed"
                                debugPause
                                handleError " * Could not mount $part ($0->${FUNCNAME[0]})\n    Args Passed: $*\n    Reason: $(cat /tmp/ntfs-mount-output)"
                                ;;
                        esac
                        dots "BCD exists on $part in /bcdstore"
                        if [[ ! -f /bcdstore/Boot/BCD ]]; then
                            umount /bcdstore >/dev/null 2>&1
                            echo "No"
                            debugPause
                        else
                            umount /bcdstore >/dev/null 2>&1
                            echo "Yes"
                            debugPause
                            if [[ $part_number -gt 1 ]]; then
                                for ((increment=1; increment < $part_number; increment++)); do
                                    fixed_size_partitions="$fixed_size_partitions:$increment"
                                done
                            fi
                        fi
                    done
                    ;;
            esac
            # All:
            #  save the list of fixed size partitions
            fixed_size_partitions=$(echo $fixed_size_partitions | tr ':' '\n' | uniq | tr '\n' ':' | sed 's/:$//g')
            echo "$fixed_size_partitions" > "$imagePath/d1.fixed_size_partitions"
            # Windows 2000/XP, Vista, 7, 8, 8.1, Linux:
            #  Save unmodified partition table for download use
            #  Start filesytem type record
            dots "Saving original partition table"
            saveOriginalPartitions "$hd" "$imagePath" 1
            swapuuidfilename=""
            swapUUIDFileName "$imagePath" 1
            mainuuidfilename=""
            mainUUIDFileName "$imagePath" 1
            echo -n "" > "$imagePath/d1.original.fstypes"
            echo -n "" > $swapuuidfilename
            echo "Done"
            debugPause
            echo " * Saving original disk/parts UUIDs"
            debugPause
            saveUUIDInformation "$hd" "$mainuuidfilename"
            echo " * Shrinking Partitions on disk"
            debugPause
            for part in $parts; do
                clearMountedDevices "$part"
                removePageFile "$part"
                shrinkPartition "$part" "$imagePath/d1.original.fstypes" "$fixed_size_partitions"
            done
            echo " * Saving shrunken partition table"
            debugPause
            sfdiskminimumpartitionfilename=""
            sfdiskMinimumPartitionFileName "$imagePath" 1
            savePartitionTablesAndBootLoaders "$hd" 1 "$imagePath" "$osid" "$imgPartitionType" "$sfdiskminimumpartitionfilename"
            echo " * Processing Hard Disk: $hd"
            for part in $parts; do
                savePartition "$part" 1 "$imagePath" "$imgPartitionType"
            done
            dots "Restoring Original Partition Layout"
            restoreOriginalPartitions "$hd" "$imagePath" 1
            runPartprobe "$hd"
            echo "Done"
            debugPause
            [[ $osid -eq 2 ]] && correctVistaMBR "$hd"
            getPartitions "$hd"
            for part in $parts; do
                expandPartition "$part" "$fixed_size_partitions"
            done
            ;;
        mps)
            echo " * Processing Hard Disk: $hd"
            echo " * Saving original disk/parts UUIDs"
            debugPause
            mainuuidfilename=""
            mainUUIDFileName "$imagePath" 1
            saveUUIDInformation "$hd" "$mainuuidfilename"
            savePartitionTablesAndBootLoaders "$hd" 1 "$imagePath" "$osid" "$imgPartitionType"
            getPartitions "$hd"
            for part in $parts; do
                savePartition "$part" 1 "$imagePath" "$imgPartitionType"
            done
            ;;
        mpa)
            disk_number=1
            for disk in $disks; do
                debugPause
                echo " * Processing Hard Disk: $disk"
                echo " * Saving original disk/parts UUIDs"
                debugPause
                mainuuidfilename=""
                mainUUIDFileName "$imagePath" $disk_number
                saveUUIDInformation "$disk" "$mainuuidfilename"
                savePartitionTablesAndBootLoaders "$disk" "$disk_number" "$imagePath" "$osid" "$imgPartitionType"
                runPartprobe "$disk"
                getPartitions "$disk"
                for part in $parts; do
                    savePartition "$part" "$disk_number" "$imagePath" "$imgPartitionType"
                done
                echo " * Disk $disk complete"
                let disk_number+=1
            done
            ;;
        dd)
            imgpart="$imagePath/$img"
            mkfifo /tmp/pigz1 >/dev/null 2>&1
            uploadFormat "/tmp/pigz1" "$imgpart"
            partclone.imager -c -s "$hd" -O /tmp/pigz1 -N -f 1
            rm /tmp/pigz1 >/dev/null 2>&1
            clearScreen
            ;;
    esac
}
findHDDInfo
echo " * Now FOG will attempt to capture the image using Partclone"
debugPause
beginUpload
completeTasking