summaryrefslogtreecommitdiff
path: root/draft/other-tools/fog/fog-notes.txt
blob: 61a7d1267f287465f2152d5526d49b851f1aa3bb (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
./src/buildroot/package/fog/scripts/usr/share/fog/lib/funcs.sh


            pigz $PIGZ_COMP < $fifo | split -a 3 -d -b 200m - ${file}. &


    mainuuidfilename="$imagePath/d${disk_number}.original.uuids"
    swapuuidfilename="$imagePath/d${disk_number}.original.swapuuids"
    sfdiskoriginalpartitionfilename="$imagePath/d${disk_number}.partitions"
    sfdisklegacyoriginalpartitionfilename="$imagePath/d${disk_number}.original.partitions"
    sfdiskminimumpartitionfilename="$imagePath/d${disk_number}.minimum.partitions"
    sgdiskoriginalpartitionfilename="$imagePath/d${disk_number}.sgdisk.original.partitions"
    fixed_size_file="$imagePath/d${disk_number}.fixed_size_partitions"
    hasgrubfilename="$imagePath/d${disk_number}.has_grub"
    [[ -n $sgdisk ]] && hasgrubfilename="$imagePath/d${disk_number}.grub.mbr"
    [[ -n $sgdisk && $hasGRUB -eq 1 ]] && \
	mbr="$imagePath/d${disk_number}.grub.mbr" || \
	mbr="$imagePath/d${disk_number}.mbr"
    ebrfilename="$path/d${disk_number}p${part_number}.ebr"

# Save enough MBR and embedding area to capture all of GRUB
# Strategy is to capture EVERYTHING before the first partition.
# Then, leave a marker that this is a GRUB MBR for restoration.
# We could get away with less storage, but more details are required
# to parse the information correctly.  It would make the process
# more complicated.

# $1 is the disk
# $2 is the disk number
# $3 is the image path to save the file to.
# $4 is the determinator of sgdisk use or not
saveGRUB() {


    # Determine the number of sectors to copy
    # Hack Note: print $4+0 causes the column to be interpretted as a number
    #            so the comma is tossed
    local count=$(sfdisk -d $disk 2>/dev/null | awk /start=\ *[1-9]/'{print $4+0}' | sort -n | head -n1)


    local has_grub=$(dd if=$disk bs=512 count=1 2>&1 | grep -i 'grub')

    # Ensure that no more than 1MiB of data is copied (already have this size used elsewhere)
    [[ $count -gt 2048 ]] && count=2048
    local mbrfilename=""
    MBRFileName "$imagePath" "$disk_number" "mbrfilename" "$sgdisk"
    dd if=$disk of=$mbrfilename count=$count bs=512 >/dev/null 2>&1

}



hasGrubFileName() {
    local imagePath="$1"  # e.g. /net/dev/foo
    local disk_number="$2"    # e.g. 1
    local sgdisk="$3"
    [[ -z $imagePath ]] && handleError "No image path passed (${FUNCNAME[0]})\n   Args Passed: $*"
    [[ -z $disk_number ]] && handleError "No disk number passed (${FUNCNAME[0]})\n   Args Passed: $*"
    hasgrubfilename="$imagePath/d${disk_number}.has_grub"
    [[ -n $sgdisk ]] && hasgrubfilename="$imagePath/d${disk_number}.grub.mbr"
}

savePartitionTablesAndBootLoaders() {

    case $hasgpt in
        0)
            strdots="Saving Partition Tables (MBR)"
            case $osid in
                4|50|51)
                    [[ $disk_number -eq 1 ]] && strdots="Saving Partition Tables and GRUB (MBR)"
                    ;;
            esac
            dots "$strdots"
            saveGRUB "$disk" "$disk_number" "$imagePath"
            sfdisk -d $disk 2>/dev/null > $sfdiskfilename
            [[ $have_extended_partition -ge 1 ]] && saveAllEBRs "$disk" "$disk_number" "$imagePath"
            echo "Done"
            ;;
        1)
            dots "Saving Partition Tables (GPT)"
            saveGRUB "$disk" "$disk_number" "$imagePath" "true"
            sgdisk -b "$imagePath/d${disk_number}.mbr" $disk >/dev/null 2>&1
            if [[ ! $? -eq 0 ]]; then
                echo "Failed"
                debugPause
                handleError "Error trying to save GPT partition tables (${FUNCNAME[0]})\n   Args Passed: $*"
            fi
            sfdisk -d $disk 2>/dev/null > $sfdiskfilename
            echo "Done"
            ;;
    esac
}o

clearPartitionTables() {
    sgdisk -Z $disk >/dev/null 2>&1
}

restorePartitionTablesAndBootLoaders() {
    if [[ $table_type == GPT ]]; then
        dots "Restoring Partition Tables (GPT)"
        restoreGRUB "$disk" "$disk_number" "$imagePath" "true"
        sgdisk -gel $tmpMBR $disk >/dev/null 2>&1
        [[ ! $? -eq 0 ]] && handleError "Error trying to restore GPT partition tables (${FUNCNAME[0]})\n   Args Passed: $*"
        global_gptcheck="yes"
        echo "Done"
    else
	[big cheat for MBR, dd, sfdisk for EBRs]
    fi
}


savePartition() {
    case $fstype in
        swap)
            echo " * Saving swap partition UUID"
            swapUUIDFileName "$imagePath" "$disk_number"
            saveSwapUUID "$swapuuidfilename" "$part"
            ;;
        *)
            case $parttype in
                0x5|0xf)
                    echo " * Not capturing content of extended partition"
                    debugPause
                    EBRFileName "$imagePath" "$disk_number" "$part_number"
                    touch "$ebrfilename"
                    ;;
                *)
                    echo " * Using partclone.$fstype"
                    debugPause
                    imgpart="$imagePath/d${disk_number}p${part_number}.img"
                    uploadFormat "$fifoname" "$imgpart"
                    partclone.$fstype -fsck-src-part -c -s $part -O $fifoname -N -f 1
                    case $? in
                        0)
                            mv ${imgpart}.000 $imgpart >/dev/null 2>&1
                            echo " * Image Captured"
                            ;;
                        *)
                            handleError "Failed to complete capture (${FUNCNAME[0]})\n   Args Passed: $*"
                            ;;
                    esac
                    ;;
            esac
            ;;
    esac