forked from intelligent-agent/Refactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-image-from-sd.sh
executable file
·142 lines (121 loc) · 5.18 KB
/
generate-image-from-sd.sh
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
#!/bin/bash
# This script currently does not have error checking in it and has not been heavily tested.
# use this script at your own risk.
#
# If you are debugging or just concerned uncomment the "set -e" line below to force the script to
# terminate on any error. This includes erroring on already existing directories and similar minor issues.
set -e
set -x
#echo
#echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#echo "! This script currently does not have error checking in it and has not been heavily tested. !"
#echo "! As such use this script at your own risk till it can be refined further. !"
#echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#echo
#echo "This script will resize the file system and partition on the SD card to as small as possible."
#echo "After this it will clone the results to an image file on USB drive"
#echo
#echo "Please make sure the SD card and FAT formatted USB drive are inserted."
#read -rsp $'Press any key to continue...\n' -n1 key
# This is commended out because it currently cannot work, the script shuts the BB down after running
#echo "Running script to clone eMMC to SD"
#echo "/opt/scripts/tools/eMMC/beaglebone-black-make-microSD-flasher-from-eMMC.sh"
echo
DEVICE=$1
PARTITION=${DEVICE}p1
echo "Device: $DEVICE"
echo "Partition: $PARTITION"
# This makes it so the image will boot on other BB not just the one it was built on
echo "Removing UUID references and uncommenting flahser option from /boot/uEnv.txt"
mkdir /mnt/zero
mount $PARTITION /mnt/zero
sed -ie '/^uuid=/d' /mnt/zero/boot/uEnv.txt
sed -ie 's/#cmdline=init=\/opt\/scripts\/tools\/eMMC\/init-eMMC-flasher-v3.sh$/cmdline=init=\/opt\/scripts\/tools\/eMMC\/init-eMMC-flasher-v3.sh/' /mnt/zero/boot/uEnv.txt
echo "Removing WPA wifi access file just in case"
rm -rf /mnt/zero/root/wpa.conf
echo "Clearing bash history"
rm -rf /mnt/zero/root/.bash_history
rm -rf /mnt/zero/home/ubuntu/.bash_history
echo
# Likely not needed but for the sake of making the image smaller we defrag first
echo "Defragmenting partition."
e4defrag /mnt/zero > /dev/null
umount /mnt/zero
echo
# Run file system checks and then shrink the file system as much as possible
echo "Resizing filesystem."
e2fsck -f $PARTITION
resize2fs -pM $PARTITION
resize2fs -pM $PARTITION
e2fsck -f $PARTITION
echo
# Zero out the free space remaining on the file system
echo "Defrag and zero partition free space."
mount $PARTITION /mnt/zero
e4defrag /mnt/zero > /dev/null
dd if=/dev/zero of=/mnt/zero/zeros || true
rm -rf /mnt/zero/zeros
umount /mnt/zero
echo
# Run the file system checks and another series of file system shrinks just in case
# we can shrink the file system any further after zeroing the free space.
echo "Resizing filesystem again."
e2fsck -f $PARTITION
resize2fs -pM $PARTITION
resize2fs -pM $PARTITION
e2fsck -f $PARTITION
echo
# This is where the real danger starts with modifying the partitions
echo "Shrinking partition now."
# Gather useful partition data
fsblockcount=$(tune2fs -l $PARTITION | grep "Block count:" | awk '{printf $3}')
fsblocksize=$(tune2fs -l $PARTITION | grep "Block size:" | awk '{printf $3}')
partblocksize=$(fdisk -l $PARTITION | grep Units: | awk '{printf $8}')
partblockstart=$(fdisk -l -o Device,Start $DEVICE | grep $PARTITION | awk '{printf $2}')
partsize=$((fsblockcount*fsblocksize/partblocksize))
# Write out the partition layout that will replace the currently existing one.
cat <<EOF > /shrink.layout
# partition table of $DEVICE
unit: sectors
$PARTITION : start=${partblockstart}, size=${partsize}, Id=83, bootable
EOF
# Perform actual modificaitons to the partition layout
sfdisk $DEVICE < /shrink.layout
#rm -rf /shrink.layout
# Make sure the system sees the new partition layout, check the file system for issues
# and then resize the file system to the full size of the new partition if it is needed
echo "Re-read partition table"
echo "Wait for the kernel to re-read the table"
sleep 5
hdparm -z $DEVICE
echo "Wait for the kernel to re-read the table"
sleep 5
e2fsck -f $PARTITION
resize2fs $PARTITION
e2fsck -f $PARTITION
echo
# Run one last defrag and zero of the free space before backing it up
echo "Final defrag and zeroing partition free space."
mount $PARTITION /mnt/zero
kamiversion=$(cat /mnt/zero/etc/dogtag | awk '{printf $2}')
e4defrag /mnt/zero > /dev/null
# ignore the failure on this line - it runs until it's out of space
dd if=/dev/zero of=/mnt/zero/zeros || true
rm -rf /mnt/zero/zeros
umount /mnt/zero
rm -rf /mnt/zero
echo
# Final file system check
echo "File system and partition shrink are complete, running last file system check."
e2fsck -f $PARTITION
echo
# Mounting the USB thumb drive and generating the compressed image file on it from the sd card
echo "Generating image file now."
blocksize=$(fdisk -l $DEVICE | grep Units: | awk '{printf $8}')
count=$(fdisk -l -o Device,End $DEVICE | grep $PARTITION | awk '{printf $2}')
ddcount=$((count*blocksize/1000000+1))
dd if=$DEVICE bs=1MB count=${ddcount} | xz -T 0 > Kamikaze-${kamiversion}.img.xz
echo
# Talkie talkie
echo "Image file generated on USB drive as Kamikaze-${kamiversion}.img.xz"
echo "USB drive and MicroSD card can be removed safely now."