-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagecreate.sh
179 lines (143 loc) · 4.22 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
#!/bin/bash
# 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 )
[[ $( ls -A BOOT ) ]] && notempty+='BOOT '
[[ $( ls -A ROOT ) ]] && notempty+='ROOT'
if [[ $notempty ]]; then
dialog "${optbox[@]}" --infobox "
\Z1$notempty\Z0 directory not empty.
" 0 0
exit
fi
dialog "${optbox[@]}" --infobox "
\Z1r\Z0Audio
\Z1Create\Z0 Image File
" 9 58
sleep 2
col=$( tput cols )
banner() {
echo
def='\e[0m'
bg='\e[44m'
printf "$bg%*s$def\n" $col
printf "$bg%-${col}s$def\n" " $1"
printf "$bg%*s$def\n" $col
}
dialog "${optbox[@]}" --msgbox "
\Z1Insert micro SD card\Z0
If already inserted:
For proper detection, remove and reinsert again.
" 0 0
sd=$( dmesg -T | tail | grep ' sd .*GB' )
[[ -z $sd ]] && sleep 2 && sd=$( dmesg -T | tail | grep ' sd .* logical blocks' )
if [[ -z $sd ]]; then
dialog "${optbox[@]}" --infobox "
\Z1No SD card found.\Z0
" 0 0
exit
fi
mount=$( mount | grep '/dev.*BOOT\|/dev.*ROOT' | cut -d' ' -f1-3 | sort )
if [[ -z $mount ]]; then
dialog "${optbox[@]}" --infobox "
\Z1SD card not mounted.\Z0
" 0 0
exit
fi
dev=/dev/$( echo "$sd" | awk -F'[][]' '{print $4}' )
#devname=$( dmesg | grep Direct-Access | tail -1 | tr -s ' ' | awk '{NF-=5;print substr($0,index($0,$6))}' )
dialog "${optbox[@]}" --yesno "
Confirm micro SD card: \Z1$dev\Z0
Detail:
$( echo $sd | sed 's/ sd /\nsd /; s/\(\[sd.\]\) /\1\n/; s/\(blocks\): (\(.*\))/\1\n\\Z1\2\\Z0/' )
$mount
" 0 0
[[ $? != 0 ]] && exit
BOOT=$( mount | grep /dev.*BOOT | cut -d' ' -f3 )
ROOT=$( mount | grep /dev.*ROOT | cut -d' ' -f3 )
if [[ ! -e $BOOT/config.txt ]]; then
dialog "${optbox[@]}" --infobox "
\Z1$dev\Z0 is not \Z1r\Z0Audio.
" 0 0
exit
fi
version=$( cat $ROOT/srv/http/data/system/version )
revision=$( cat $ROOT/srv/http/data/addons/r$version )
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 "${opt[@]}" --output-fd 1 --inputbox "
Image filename:
" 0 0 rAudio-$version-$model-$revision.img.xz )
imagedir=$( dialog "${optbox[@]}" --title 'Save to: ([space]=select)' --stdout --dselect $PWD/ 20 40 )
imagepath="${imagedir%/}/$imagefile" # %/ - remove trailing /
# auto expand root partition
touch $BOOT/expand
clear -x
banner "Image: $imagefile"
banner 'Shrink ROOT partition ...'
echo
bar='\e[44m \e[0m'
part=${dev}2
partsize=$( fdisk -l $part | awk '/^Disk/ {print $2" "$3}' )
used=$( df -k 2> /dev/null | grep $part | awk '{print $3}' )
umount -l -v ${dev}1 ${dev}2
e2fsck -fy $part
shrink() {
echo -e "$bar Shrink #$1 ...\n"
partinfo=$( tune2fs -l $part )
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 $part | 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 $part $(( 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 $imagepath
echo
dd if=$dev bs=512 iflag=fullblock count=$endsector | nice -n 10 xz -9 --verbose --threads=0 > "$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