-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimagecreate.sh
194 lines (159 loc) · 4.46 KB
/
imagecreate.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
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
#!/bin/bash
cleanup() {
umount -l $partboot $partroot 2> /dev/null
rmdir /home/$USER/{BOOT,ROOT} 2> /dev/null
exit
}
trap cleanup INT
# required packages
if [[ -e /usr/bin/pacman ]]; then
[[ ! -e /usr/bin/bsdtar ]] && packages+='bsdtar '
[[ ! -e /usr/bin/dialog ]] && packages+='dialog '
[[ $packages ]] && pacman -Sy --noconfirm $packages
else
[[ ! -e /usr/bin/bsdtar ]] && packages+='bsdtar libarchive-tools '
[[ ! -e /usr/bin/dialog ]] && packages+='dialog '
[[ $packages ]] && apt install -y $packages
fi
optbox=( --colors --no-shadow --no-collapse --nocancel )
dialog "${optbox[@]}" --infobox "
\Z1r\Z0Audio
\Z1Create\Z0 Image File
" 9 58
sleep 2
banner() {
echo
def='\e[0m'
bg='\e[44m'
printf "$bg%*s$def\n" $COLUMNS
printf "$bg%-${COLUMNS}s$def\n" " $1"
printf "$bg%*s$def\n" $COLUMNS
}
dialog "${optbox[@]}" --msgbox "
\Z1Insert micro SD card\Z0
If already inserted:
For proper detection, remove and reinsert again.
" 0 0
deviceLine() {
devline=$( dmesg \
| tail \
| grep ' sd.* GiB\|mmcblk.* GiB' \
| tail -1 )
}
deviceLine
[[ ! $devline ]] && sleep 2 && deviceLine
if [[ ! $devline ]]; then
dialog "${optbox[@]}" --infobox "
\Z1No SD card found.\Z0
" 0 0
exit
fi
if [[ $devline == *\[sd?\]* ]]; then
name=$( echo $devline | sed -E 's|.*\[(.*)\].*|\1|' )
dev=/dev/$name
partboot=${dev}1
partroot=${dev}2
else
name=$( echo $devline | sed -E 's/.*] (.*): .*/\1/' )
dev=/dev/$name
partboot=${dev}p1
partroot=${dev}p2
fi
list=$( lsblk -o name,size,mountpoint | grep -v ^loop | sed "/^$name/ {s/^/\\\Z1/; s/$/\\\Z0/}" )
dialog "${optbox[@]}" --yesno "
Device list:
$list
Confirm SD card:
$( echo "$list" | grep '\\Z1' )
" 0 0
[[ $? != 0 ]] && exit
umount -l $partboot $partroot 2> /dev/null
BOOT=/mnt/BOOT
ROOT=/mnt/ROOT
[[ $( ls -A $BOOT 2> /dev/null ) ]] && warnings="
$BOOT not empty."
[[ $( ls -A $ROOT 2> /dev/null ) ]] && warnings+="
$ROOT not empty."
[[ $warnings ]] && dialog "${optbox[@]}" --infobox "$warnings" 0 0 && exit
mkdir -p /mnt/{BOOT,ROOT}
mount $partboot $BOOT
mount $partroot $ROOT
if [[ ! -e $BOOT/config.txt ]]; then
dialog "${optbox[@]}" --infobox "
\Z1$dev\Z0 is not \Z1r\Z0Audio.
" 0 0
exit
fi
release=$( cat $ROOT/srv/http/data/addons/r1 )
if [[ -e $BOOT/kernel8.img ]]; then
model=64bit
elif [[ -e $BOOT/kernel7.img ]]; then
model=RPi2
else # $BOOT/kernel.img
model=RPi0-1
fi
imagefile=$( dialog "${optbox[@]}" --output-fd 1 --inputbox "
Image filename:
" 0 0 rAudio-$model-$release.img.xz )
selectdir=$PWD/
[[ -e $PWD/BIG ]] && selectdir+=BIG
imagedir=$( dialog "${optbox[@]}" --title 'Save to: ([space]=select)' --stdout --dselect $selectdir 20 40 )
imagepath="${imagedir%/}/$imagefile" # %/ - remove trailing /
clear -x
touch $BOOT/expand # auto expand root partition
umount -l -v $partboot $partroot
rmdir /home/$USER/{BOOT,ROOT} 2> /dev/null
e2fsck -fy $partroot
banner "Image: $imagefile"
banner 'Shrink ROOT partition ...'
echo
bar='\e[44m \e[0m'
partsize=$( fdisk -l $partroot | awk '/^Disk/ {print $2" "$3}' )
used=$( df -k 2> /dev/null | grep $partroot | awk '{print $3}' )
shrink() {
echo -e "$bar Shrink Pass #$1 ...\n"
partinfo=$( tune2fs -l $partroot )
blockcount=$( awk '/Block count/ {print $NF}' <<< "$partinfo" )
freeblocks=$( awk '/Free blocks/ {print $NF}' <<< "$partinfo" )
blocksize=$( awk '/Block size/ {print $NF}' <<< "$partinfo" )
sectorsize=$( sfdisk -l $dev | awk '/Units/ {print $8}' )
startsector=$( fdisk -l $dev | grep $partroot | awk '{print $2}' )
usedblocks=$(( blockcount - freeblocks ))
targetblocks=$(( usedblocks * 105 / 100 ))
Kblock=$(( blocksize / 1024 ))
newsize=$(( ( targetblocks + Kblock - 1 ) / Kblock * Kblock ))
sectorsperblock=$(( blocksize / sectorsize ))
endsector=$(( startsector + newsize * sectorsperblock ))
if (( $(( newsize - target )) < 10 )); then
echo Already reached minimum size.
else
# shrink filesystem to minimum
resize2fs -fp $partroot $(( newsize * Kblock ))K
parted $dev ---pretend-input-tty <<EOF
unit
s
resizepart
2
$endsector
Yes
quit
EOF
fi
}
echo
shrink 1
shrink 2
banner 'Compressed to image file ...'
echo
echo -e "$bar $imagepath"
echo
threads=$(( $( nproc ) - 2 ))
dd if=$dev bs=512 iflag=fullblock count=$endsector | nice -n 10 xz -v -T $threads > "$imagepath"
byte=$( stat --printf="%s" "$imagepath" )
mb=$( awk "BEGIN { printf \"%.1f\n\", $byte / 1024 / 1024 }" )
dialog "${optbox[@]}" --infobox "
Image file created:
\Z1$imagepath\Z0
$mb MiB
\Z1BOOT\Z0 and \Z1ROOT\Z0 have been unmounted.
" 10 58