-
Notifications
You must be signed in to change notification settings - Fork 27
/
create-factory-reset
executable file
·117 lines (88 loc) · 4.09 KB
/
create-factory-reset
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
#!/bin/bash
# this whole thing has become ridiculously complicated
# I think I should redo it in python
# fail on errors, undefined variables, and errors in pipes
set -eu -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# output formatting functions like pr_header, pr_debug etc
source "$DIR/lib/display_funcs"
# handles options processing for script
source "$DIR/lib/usage.sh"
# variables derived from setup options and various constants
source "$DIR/lib/setup.sh"
# bunch of utility functions for inspecting/setting values etc
source "$DIR/lib/utils.sh"
# this is where the main logic lives
source "$DIR/lib/stages.sh"
# initial attempt at having some unit tests
source "$DIR/lib/tests.sh"
# dump variables as seen after options and defauls processing
show_setup_var_summary
# this is to cleanup any previous runs that didn't run cleanup at the end
# this won't cleanup mounts from different image to the one give with -i
if [ ! -z "${OPTION_CLEANUP_PRE}" ]; then
cleanup
fi
if [ "$OPT_DO_CHECKS" ] ; then
check_prerequisites
check_sources
fi
if [ ! -z "${OPT_RUN_TESTS}" ]; then
run_tests
exit 0
fi
# extract the partition start/sizes for the source image
# these are needed to create the copy of the original
[ "$OPT_GET_PART_FOR_ORIG" ] && get_partitions_for_original
# extract part start/sizes for slim image (if used)
# we need the size of the root partition from the slim image
# to calculate the total space required slim+recovery.zip+free space
[ "$OPT_GET_PART_FOR_SLIM" ] && get_partitions_for_slim
# to avoid conflicts, generate uuids for boot, recovery, root partions
# @TODO not sure if this is actually needed.. to check that...
# currently the only thing that seems absolutely necessary, is that the
# root/recovery parts of the recovery image have unique UUIDs
[ "$OPT_MAKE_UUIDS" ] && make_uuids
# mount the boot and root partiions of source img to loopback devices ro
[ "$OPT_MOUNT_ORIG" ] && make_loop_and_mount_original
# make a writable copy of the source image and mount it
[ "$OPT_MOUNT_COPY" ] && make_loop_and_mount_copy
# copy the partitions from the source into the writable copy
[ "$OPT_COPY_TO_COPY" ] && copy_original_to_copy
# insert the generated UUID into the rootfs fstab from those generated earlier
# @TODO if this is going to be done, it needs to search and replace rather than
# template out the fstab... disabled as will probably break things as it is
# [ "$OPT_FIX_ROOTFS_FSTAB" ] && fix_copy_rootfs_fstab
# the resize script fails if the root partiion is not in position 2
# so this needs to be copied into the copy image
# @TODO not sure if this is still needed, as the current script checks the
# partition number of the root partition.
# the only thing this script does now (??) is add some sleeps to make the output
# more useful in failure situations
[ "$OPT_FIX_RESIZE_SCRIPT" ] && fix_resize_script
# zip the copy of the rootfs for use in restoring
[ "$OPT_MAKE_RECOVERY_ZIP" ] && output_zipped_copy_rootfs
# going to use the slim image for the recovery partition
[ "$OPT_MOUNT_SLIM" ] && make_loop_and_mount_slim
# calcule the required bytes and sectors for the restore p2 partition
# from the recovery.zip plus the used space on the slim p2 partition
[ "$OPT_GET_RECOVERY_SIZES" ] && get_recovery_root_part_size
# use the values to create a restore img file and mount it
[ "$OPT_MOUNT_RESTORE" ] && make_loop_and_mount_restore
# copy the various source partiions into the restore image
# mount the partitions for further editing
[ "$OPT_COPY_TO_RESTORE" ] && copy_to_restore
[ "$OPT_FIXUP_RECOVERY_ROOTFS" ] && fixup_fstab_in_recovery_rootfs
# edit the cmdline.txt file for correct partition
[ "$OPT_FIX_CMDLINE_TXT" ] && overwrite_cmdline_for_boot
# restore triggers the recovery from the running rpi
[ "$OPT_MAKE_RESTORE_SCRIPT" ] && make_restore_script
# recovery is triggered after reboot to restore partition
[ "$OPT_MAKE_RECOVERY_SCRIPT" ] && make_recovery_script
[ "$OPT_POST_SUMMARY" ] && get_postbuild_summary
# enable_rpi_serial_console
if [ "${OPTION_CLEANUP_POST}" ]; then
cleanup
fi
pr_header "finished"
pr_info "RESTORE image is ${IMG_RESTORE}"