-
Notifications
You must be signed in to change notification settings - Fork 23
/
bpi-migrate
executable file
·1236 lines (1160 loc) · 26.7 KB
/
bpi-migrate
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#GPL
#set -x
BPILICENSE="GPLv2 or any later version"
BPICMD="bpi-migrate"
BPICMDVERSION="v1.3.11(github)"
CONFFILE=""
BPIMODE=
NEWBPIMODE=""
#BPIBOARD="bpi-m3"
#support bpi-migrate -c boardlist.conf
BPIBOARD=""
IMGVER=""
#IMGVER="-demo"
#IMGVER="-preview"
#IMGVER="-beta"
IMGDISK="-sd-emmc"
SRC=""
DST=""
SIZE=""
BOOTSIZE=""
BPI_BOOT_UUID=""
BPI_ROOT_UUID=""
IMGFILE=""
DEVICE=""
if [[ $DEVICE == "" ]]; then DEVICE="/dev/mmcblk1"; fi
TMPFILE=/tmp/.${BPICMD}.tmp.$$
LOOP_DEV="/dev/loop0"
LOOP_PART_BOOT="${LOOP_DEV}p1"
LOOP_PART_SYS="${LOOP_DEV}p2"
UBOOTIMGGZ=BPI_M3_720P.img.gz
UBOOTFILES="BPI-R2-HEAD440-0k.img.gz
BPI-R2-HEAD1-512b.img.gz
"
#BOOTFILES=BPI-BOOT.tgz
BOOTFILES=
ROOTFILE=
BPIFILES="bpi-tools.tgz
3.3.0-BPI-M2-Kernel.tgz
3.4.39-BPI-M2P-Kernel.tgz
3.4.39-BPI-M3-Kernel.tgz
3.4.112-sun8i.tgz
3.4.112-sun7i.tgz
3.10.101-pine64-bpi-m64.tgz
rpi-kernel.tgz
4.6.5-300.fc24.armv7hl.tgz
3.10.65-BPI-M2U-Kernel.tgz
libvdpau_sunxi.tgz
bt.tgz
camera.tgz
brcm.tgz"
#
# BPIFILE / BPIURL
#
BPIFILE="$HOME/.${BPICMD}.lst"
#BPI-ROOT.lst
#
## Hello from Google,
## On Aug 31, 2016, we will discontinue serving content via
## googledrive.com/host/[id] and the webpages will not be accessible anymore.
#
#BPIURL="https://googledrive.com/host/0B_YnvHgh2rwjVE10U3U0Q0NCdWM"
BPIURL="https://github.com/BPI-SINOVOIP/BPI-files/raw/master/others/for-bpi-tools/BPI-ROOT.lst"
BPIURLNEW=""
#
## URLBASE
#
URLBASE="https://github.com/BPI-SINOVOIP/BPI-files/raw/master"
#
## URLCONF
#
URLCONF="${URLBASE}/others/for-bpi-tools/conf"
#
# update / download
#
F_UPDATE=NO
F_DOWNLOAD=NO
F_GOOGLE=YES
######################################################
for IN in "$@"
do
if [[ "$IN" == *"="* ]] ; then
P=${IN%%=*}
V=${IN##*=}
echo "INFO: $P=${V:-(empty)}"
eval $P=$V
fi
done
######################################################
usage() {
cat - >&2 <<EOF
${BPICMD}(${BPICMDVERSION}), migrate a system to bananapi image environment.
Usage: ${BPICMD} [OPTIONS]...
${BPICMD} [ --help | -v | --version ]
Options:
-c, --config config file
-u, --update update index files
-G, --download download files for bpi-migrate
-r, --root rootfile user root file for BPI-ROOT (ex. root.tgz)
-b, --boot bootfile user boot file for BPI-BOOT (ex. boot.tgz)
-s, --size image size(defaut: 7456 MB for eMMC)
-B, --BOOTSIZE BPI-BOOT partition size(default: 256 MB)
-I, --IMGDISK IMG TYPE ('-sd' '-emmc' '-sd-emmc')
-V, --IMGVER IMG VERSION ('-demo' '-preview' '-beta')
-h, --help Print usage
-v, --version Print version information and quit
ex: (prepare env.)
1. mkdir BPI-ROOT ; cd BPI-ROOT
2. bpi-migrate -u -G ; #download files for bpi-migrate
ex: (create BPI-M2U BPI-M3 busybox image, auto download files from github)
0. sudo su
1. bpi-migrate -c bpi-m2u.conf -c busybox.conf
2. bpi-migrate -c bpi-m3.conf -c busybox.conf
ex: (default create BPI-M3 image)
0. sudo su
1. bpi-migrate -r /pathtouser/root.tgz
ex: (create BPI-M2P image)
0. sudo su
1. bpi-migrate -r /pathtouser/root.tgz --uboot BPI_M2P_720P.img.gz
ex: (create BPI-R1 image)
0. sudo su
1. bpi-migrate -r root.tgz --uboot u-boot-2016.05-bpi-r1-legacy.img.gz
for test only:
-d, --device device device
-i, --imgfile imgfile imgfile
-S, --src src src from
-D, --dst dst dst to
EOF
exit 1
}
F_INDEX=
download_index() {
if [ "x${F_INDEX}" == "xOK" ] ; then
return
fi
echo BPIFILE=$BPIFILE
echo "Wait for download index file ..."
curl -s -L $BPIURL > $BPIFILE
echo "OK!!\n"
F_INDEX=OK
}
get_conffile() {
CONFDIRS=". board os tools pkg"
FILE=$1
if [ ! -f "${FILE}" ] ; then
echo "Wait for download $FILE ..."
for IN in ${CONFDIRS} ; do
U="${URLCONF}/${IN}/${FILE}"
curl -s -L $U > $FILE
eval `grep "^BPICONF" $FILE`
if [ "x$FILE" == "x$BPICONF" ] ; then
echo $U
echo "OK!!\n"
break
fi
done
fi
}
get_bpi_github() {
FILE=$1
# check DIR
if [ -d "${FILE}" ] ; then
echo "$FILE ... OK!!"
return
fi
DIRS=". SD/BPI-ROOT SD/BPI-BOOT SD/100MB rootfs"
if [ ! -f "${FILE}" ] ; then
echo "Wait for download $FILE ..."
for D in ${DIRS} ; do
echo -n "."
U="${URLBASE}/${D}/${FILE}"
#echo $U
curl -s -L $U > $FILE
SHELLCMD=$?
if [ ! $SHELLCMD -eq 0 ] ; then
rm -f $FILE
continue
fi
#check file
ERRORMSG=("error" "Not Found")
for CHECK in "${ERRORMSG[@]}" ; do
if [ ! -f "${FILE}" ] ; then
break
fi
#echo "grep ${CHECK} $FILE "
grep "${CHECK}" $FILE 2>&1 >/dev/null
SHELLCMD=$?
if [ $SHELLCMD -eq 0 ] ; then
rm -f $FILE
fi
done
if [ -f "${FILE}" ] ; then
echo $U
echo "OK!!\n"
break
fi
done
fi
}
list_files() {
echo
echo "bpi files:"
awk '{ printf "%-12s\t%s\n", $2, $4 }' $BPIFILE
}
get_all_versions() {
echo
echo "bpi files:"
for IN in `cat $BPIFILE | awk '{ print $2 }'` ; do
IMGFILE=/usr/bin/$IN
BPICMDVERSION="old version"
if [ -f "${IMGFILE}" ] ; then
eval `grep "^BPICMDVERSION" $IMGFILE`
else
BPICMDVERSION="NOT INSTALLED"
fi
echo "$IMGFILE $BPICMDVERSION"
done
}
get_all_files() {
echo
echo "download bpi files:"
for IN in `cat $BPIFILE | awk '{ print $2 }'` ; do
echo $IN
IMGFILE=$IN
get_bpifile
chmod 644 $IMGFILE
done
}
upgrade_all_files() {
echo
echo "upgrade bpi files:"
for IN in `cat $BPIFILE | awk '{ print $2 }'` ; do
IMGFILE=/usr/bin/$IN
echo $IMGFILE
if [ -f "${IN}" ] ; then
cp -a ${IN} ${IMGFILE}
chown root:root ${IMGFILE}
chmod 755 ${IMGFILE}
fi
done
}
get_bpifile() {
MD5TMP="/tmp/.md5.tmp.$$"
#echo "BPIFILE=$BPIFILE"
#echo "IMGFILE=$IMGFILE"
#echo "MD5TMP=$MD5TMP"
LINE=`cat $BPIFILE | grep $IMGFILE`
#echo LINE=$LINE
set -- $LINE
BPIURL=$3
if [ ! -z ${BPIURLNEW} ] ; then
BPIURL=${BPIURLNEW}/$2
fi
echo "$1 $2" > $MD5TMP
if [ ! -f "${IMGFILE}" ] ; then
curl -s -L $BPIURL > $IMGFILE
fi
md5sum -c $MD5TMP
rm -f $MD5TMP
file $IMGFILE | grep "shell script" >/dev/null
SHELLCMD=$?
if [ $SHELLCMD -eq 0 ] ; then
eval `grep "^BPICMDVERSION" $IMGFILE`
echo "$IMGFILE: $BPICMDVERSION"
fi
}
requires_root() {
if [ "$(id -u)" -ne 0 ]; then
echo >&1 "ERROR: This command can only be used by root."
exit 1
fi
}
upgrade_bpi_files() {
requires_root
get_all_files
upgrade_all_files
get_all_versions
}
bpi_tools_main() {
if [ ! -f "${BPIFILE}" ] ; then
download_index
fi
if [ -z ${IMGFILE} ] ; then
usage
list_files
exit 0
fi
case ${IMGFILE} in
all)
get_all_files
exit 0
;;
--all)
upgrade_bpi_files
exit 0
;;
--download)
get_all_files
exit 0
;;
--genfiles)
#gen_bpi_files
exit 0
;;
--upgrade)
upgrade_bpi_files
exit 0
;;
--ver*)
get_all_versions
exit 0
;;
--update)
usage
download_index
list_files
exit 0
;;
*)
echo "INFO: Try to get $IMGFILE ..."
get_bpifile
exit 0
;;
esac
}
list_images() {
DIR=`pwd`
if [[ -d ${IMGFILE} ]] ; then DIR=${IMGFILE} ; fi
find $DIR /usr/lib/u-boot/bananapi | sort > $TMPFILE
echo "bpi images: (*.img.zip)"
for IN in `cat $TMPFILE | grep '\.img.zip$'` ; do
echo $IN
done
echo "bpi images: (*.img)"
for IN in `cat $TMPFILE | grep '\.img$'` ; do
echo $IN
done
echo "bpi images: (*.img.gz)"
for IN in `cat $TMPFILE | grep '\.img.gz$'` ; do
echo $IN
done
echo "xz images: (*.xz)"
for IN in `cat $TMPFILE | grep '\.xz$'` ; do
echo $IN
done
echo "zip images: (*.zip)"
for IN in `cat $TMPFILE | grep '\.zip$'` ; do
echo $IN
done
echo "raw images: (*.raw)"
for IN in `cat $TMPFILE | grep '\.raw$'` ; do
echo $IN
done
rm -f $TMPFILE
}
list_disks() {
echo
echo "Disks: (lsblk | grep disk)"
lsblk | grep disk
echo
echo "Disks: (fdisk -l | grep Disk | grep bytes)"
fdisk -l | grep Disk | grep bytes
}
umount_device() {
F_RM=`lsblk -o RM $DEVICE | grep 1`
if [ -z "$F_RM" ] ; then
case $DEVICE in
*mmcblk*)
echo "INFO: $DEVICE : SD/eMMC !!"
;;
*)
echo "INFO: $DEVICE NOT THE removable device!! EXIT!!"
exit 1
;;
esac
fi
echo "umount device: ${DEVICE}"
for IN in `df -k | awk '{ print $1 }' | grep "${DEVICE}"` ; do
PART=$IN
echo umount $PART
umount $PART
done
}
create_new_disk() {
echo
echo "#####################"
echo "# Creating new disk"
echo "# DISKSIZE = ${SIZE}"
echo "#####################"
echo
#dd if=/dev/zero bs=1M count=${SIZE} | pv | dd of=${DEVICE}
LOOP_DEV=${DEVICE}
LOOP_PART_BOOT="${LOOP_DEV}1"
LOOP_PART_SYS="${LOOP_DEV}2"
echo
echo "#########################"
echo " +>${LOOP_DEV} "
echo " +->${LOOP_PART_BOOT} "
echo " +->${LOOP_PART_SYS} "
echo "#########################"
echo
#
echo
echo "#############################"
echo "# Creating partitions table #"
echo "#############################"
echo
sudo parted -s "${LOOP_DEV}" mklabel msdos
sudo parted -s "${LOOP_DEV}" unit MiB mkpart primary fat32 -- 100MiB 356MiB
sudo parted -s "${LOOP_DEV}" unit MiB mkpart primary ext2 -- 356MiB ${SIZE}MiB
# support BPI-R2: we disable the boot flag for bypass the BPI-R2 MBR check
# sudo parted -s "${LOOP_DEV}" set 1 boot on
sudo parted -s "${LOOP_DEV}" print
sudo partprobe "${LOOP_DEV}"
#
echo
echo "########################"
echo "# Creating filesystems #"
echo "########################"
echo
sudo mkfs.vfat -F 32 "${LOOP_PART_BOOT}" -I -n BPI-BOOT
# sudo mkfs.ext4 -O ^has_journal -E stride=2,stripe-width=1024 -b 4096 "${LOOP_PART_SYS}" -L BPI-ROOT
sudo mkfs.ext4 -O ^metadata_csum,^64bit,^has_journal -E stride=2,stripe-width=1024 -b 4096 "${LOOP_PART_SYS}" -L BPI-ROOT
sudo sync
}
create_new_image() {
if [ ! -z ${USER_NEW_IMAGE} ] ; then
${USER_NEW_IMAGE}
return $?
fi
if [ -z ${SIZE} ] ; then
SIZE=7456
fi
if [ -z ${BOOTSIZE} ] ; then
BOOTSIZE=256
fi
RAWSIZE=100
OFFSET=$((RAWSIZE + BOOTSIZE))
echo "SIZE=$SIZE, RAWSIZE=$RAWSIZE, BOOTSIZE=$BOOTSIZE, OFFSET=$OFFSET"
echo
echo "#####################"
echo "# Creating img file #"
echo "# ${SIZE} = 8G EMMC #"
echo "#####################"
echo
dd if=/dev/zero bs=1M count=${SIZE} | pv | dd of=${IMGFILE}
LOOP_DEV=`sudo losetup -f --show ${IMGFILE}`
LOOP_PART_BOOT="${LOOP_DEV}p1"
LOOP_PART_SYS="${LOOP_DEV}p2"
echo
echo "#########################"
echo " +>${LOOP_DEV} "
echo " +->${LOOP_PART_BOOT} "
echo " +->${LOOP_PART_SYS} "
echo "#########################"
echo
#
SIZE1=$((SIZE - 1))
echo
echo "#############################"
echo "# Creating partitions table #"
echo "#############################"
echo
sudo parted -s "${LOOP_DEV}" mklabel msdos
sudo parted -s "${LOOP_DEV}" unit MiB mkpart primary fat32 -- ${RAWSIZE}MiB ${OFFSET}MiB
sudo parted -s "${LOOP_DEV}" unit MiB mkpart primary ext2 -- ${OFFSET}MiB ${SIZE1}MiB
# support BPI-R2: we disable the boot flag for bypass the BPI-R2 MBR check
# sudo parted -s "${LOOP_DEV}" set 1 boot on
sudo parted -s "${LOOP_DEV}" print
sudo partprobe "${LOOP_DEV}"
#
echo
echo "########################"
echo "# Creating filesystems #"
echo "########################"
echo
if [ -z ${BPI_BOOT_UUID} ] ; then
sudo mkfs.vfat -F 32 "${LOOP_PART_BOOT}" -I -n BPI-BOOT
else
sudo mkfs.vfat -F 32 "${LOOP_PART_BOOT}" -I -n BPI-BOOT -i ${BPI_BOOT_UUID}
fi
if [ -z ${BPI_ROOT_UUID} ] ; then
sudo mkfs.ext4 -O ^metadata_csum,^64bit,^has_journal -E stride=2,stripe-width=1024 -b 4096 "${LOOP_PART_SYS}" -L BPI-ROOT
else
sudo mkfs.ext4 -O ^metadata_csum,^64bit,^has_journal -E stride=2,stripe-width=1024 -b 4096 "${LOOP_PART_SYS}" -L BPI-ROOT -U ${BPI_ROOT_UUID}
fi
sudo sync
}
mount_image() {
LOOP_DEV=`sudo losetup -f --show ${IMGFILE}`
LOOP_PART_BOOT="${LOOP_DEV}p1"
LOOP_PART_SYS="${LOOP_DEV}p2"
echo
echo "#########################"
echo " +>${LOOP_DEV} "
echo " +->${LOOP_PART_BOOT} "
echo " +->${LOOP_PART_SYS} "
echo "#########################"
echo
#
sudo sync
}
write_ubootimggz() {
#
U_INFILE=$1
U_OUTFILE=$2
U_BS=1024
U_SEEK=8
#
if [ ! -f "${U_INFILE}" ] ; then
echo "Warning: CAN NOT OPEN bootloader file ${U_INFILE}"
return 1
fi
#
case ${U_INFILE} in
*0k*)
U_BS=1024
U_SEEK=0
;;
*512b*)
U_BS=512
U_SEEK=1
;;
*1k*)
U_BS=1024
U_SEEK=1
;;
*2k*)
U_BS=1024
U_SEEK=2
;;
*8k*)
U_BS=1024
U_SEEK=8
;;
*) # default sunxi seek=8
U_BS=1024
U_SEEK=8
;;
esac
echo "sudo gunzip -c ${U_INFILE} | dd of=${U_OUTFILE} bs=${U_BS} seek=${U_SEEK}"
sudo gunzip -c ${U_INFILE} | dd of=${U_OUTFILE} bs=${U_BS} seek=${U_SEEK}
}
write_uboot() {
echo
echo "##########################"
echo "# Burning the bootloader #"
echo "##########################"
echo
for IN in ${UBOOTFILES} ${UBOOTIMGGZ} ; do
get_bpi_github ${IN}
write_ubootimggz ${IN} ${LOOP_DEV}
done
sudo sync
}
write_bpi_boot() {
#BOOTFILES=""
BOOTDIR=/mnt
#df -k | grep "/dev/sdc1" | awk '{ print $6 }'
echo
echo "######################"
echo "# Copying boot files #"
echo "######################"
echo
sudo mount -t vfat "${LOOP_PART_BOOT}" $BOOTDIR
for IN in ${BOOTFILES} ; do
get_bpi_github ${IN}
if [ -f "${IN}" ] ; then
echo tar xf $IN --keep-directory-symlink -C $BOOTDIR
tar xf $IN --keep-directory-symlink -C $BOOTDIR 2>/dev/null
else
if [ -d "${IN}" ] ; then
echo tar from dir ${IN}
(cd ${IN} ; tar cf - .) | (cd $BOOTDIR ; tar xf -)
else
echo "Warning: CAN NOT OPEN BOOTFILE file ${IN}"
fi
fi
done
sudo sync
ls -al $BOOTDIR
sudo umount $BOOTDIR
}
write_bpi_root() {
#BPIFILES=""
ROOTDIR=/mnt
echo
echo "##################"
echo "# Copying rootfs #"
echo "##################"
echo
echo "sudo mount -t ext4 "${LOOP_PART_SYS}" $ROOTDIR"
sudo mount -t ext4 "${LOOP_PART_SYS}" $ROOTDIR
#sudo rm -rf /mnt/*
for IN in ${ROOTFILE} ${BPIFILES} ; do
get_bpi_github ${IN}
if [ -f "${IN}" ] ; then
echo tar xvf $IN --keep-directory-symlink -C $ROOTDIR
tar xf $IN --keep-directory-symlink -C $ROOTDIR
else
if [ -d "${IN}" ] ; then
echo tar from dir ${ROOTFILE}
(cd ${IN} ; tar cf - .) | (cd $ROOTDIR ; tar xf -)
else
echo "Warning: CAN NOT OPEN rootfs file ${IN}"
fi
fi
done
sudo sync
ls -al $ROOTDIR
sudo umount $ROOTDIR
}
release_loopdev() {
echo
echo release_loopdev
echo
echo "sudo losetup -d ${LOOP_DEV}"
sudo losetup -d ${LOOP_DEV}
echo
LOOPDEV=$(losetup -a | awk '{ print $1 }' | rev | cut -c 2- | rev | tac)
for IN in $LOOPDEV; do
echo "losetup -d $IN"
losetup -d $IN
done
echo "###########"
echo "# Done !! #"
echo "###########"
}
disk_to_bpi_image() {
DEVICE="$SRC"
IMGFILE="$DST"
#
#step 1: 7456MB(emmc size)
create_new_image
#
#step 2: 100MB
write_uboot
#
#step 3: 256MB
write_bpi_boot
#
#step 4: ~7456MB
write_bpi_root
#
#step 5: release loop dev.
release_loopdev
#
}
requires_root() {
if [ "$(id -u)" -ne 0 ]; then
echo >&1 "ERROR: This command can only be used by root."
exit 1
fi
}
blue_led_on() {
echo "*** start COPY (blue led on ) ....."
if [ -f /sys/class/leds/blue_led/trigger ]; then
echo default-on > /sys/class/leds/blue_led/trigger
fi
}
blue_led_off() {
echo "*** end COPY (blue led off) ....."
if [ -f /sys/class/leds/blue_led/trigger ]; then
echo none > /sys/class/leds/blue_led/trigger
fi
}
bpi_copy_image() {
echo "=============================================================="
echo IMGFILE=${IMGFILE}
echo "=============================================================="
case ${IMGFILE} in
*.zip)
echo zip
unzip -p ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*.img)
echo img
dd if=${IMGFILE} bs=10M | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*.raw)
echo raw
dd if=${IMGFILE} bs=10M | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*.img.gz)
echo img.gz
# ( gunzip -c ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer ) > /dev/null 2>&1
gunzip -dc ${IMGFILE} | pv | dd of=${DEVICE} bs=1024 seek=8 status=noxfer
;;
*.xz)
echo xz
unxz -c ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*.7z)
echo 7z
7z x -so ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*)
echo default
;;
esac
sync;sync;sync
}
bpi_copy_disk() {
echo "=============================================================="
echo IMGFILE=${IMGFILE}
echo "=============================================================="
case ${IMGFILE} in
*.zip)
echo zip
if [ -z ${SIZE} ] ; then
echo "dd if=${DEVICE} bs=1M | pv | zip ${IMGFILE} -"
dd if=${DEVICE} bs=1M | pv | zip ${IMGFILE} -
else
echo "dd if=${DEVICE} bs=1M count=${SIZE} | pv | zip ${IMGFILE} -"
dd if=${DEVICE} bs=1M count=${SIZE} | pv | zip ${IMGFILE} -
fi
;;
*.img)
echo img
if [ -z ${SIZE} ] ; then
dd if=${DEVICE} bs=10M | pv | dd of=${IMGFILE} bs=10M status=noxfer
else
echo "dd if=${DEVICE} bs=1M count=${SIZE} | pv | dd of=${IMGFILE} bs=10M status=noxfer"
dd if=${DEVICE} bs=1M count=${SIZE} | pv | dd of=${IMGFILE} bs=10M status=noxfer
fi
;;
*.raw)
echo raw
dd if=${DEVICE} bs=10M | pv | dd of=${IMGFILE} bs=10M status=noxfer
;;
*.img.gz)
echo img.gz
#( gunzip -c ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer ) > /dev/null 2>&1
#gunzip -dc ${IMGFILE} | pv | dd of=${DEVICE} bs=1024 seek=8 status=noxfer
;;
*.xz)
echo xz
#unxz -c ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*.7z)
echo 7z
#7z x -so ${IMGFILE} | pv | dd of=${DEVICE} bs=10M status=noxfer
;;
*)
echo default
;;
esac
sync;sync;sync
}
bpi_imagetodisk() {
usage
if [ ! -f "${IMGFILE}" ] ; then
echo "Warning: CAN NOT USE ${IMGFILE}"
exit 0
fi
echo "Warning: Try to write ${IMGFILE} to BOOTDISK ${DEVICE}"
requires_root
echo "=============================================================="
STARTTIME=`date +%s`
date
blue_led_on
umount_device
bpi_copy_image
(eject ${DEVICE}) > /dev/null 2>&1
blue_led_off
date
ENDTIME=`date +%s`
RUNTIMESEC=$((ENDTIME-STARTTIME))
MIN=$(((RUNTIMESEC)/60))
SEC=$(((RUNTIMESEC)%60))
echo "=============================================================="
echo "RUNTIME" "${MIN}:${SEC}"
echo "OK!! You can remove the BOOTDISK ${DEVICE} now!!"
exit 0
}
bpi_disktoimage() {
usage
if [ ! -b ${DEVICE} ] ; then
echo "Warning: CAN NOT USE ${DEVICE}"
exit 0
fi
echo "Info: Try to write ${DEVICE} to BOOTDISK ${IMGFILE}"
requires_root
echo "=============================================================="
STARTTIME=`date +%s`
date
#blue_led_on
umount_device
bpi_copy_disk
(eject ${DEVICE}) > /dev/null 2>&1
#blue_led_off
date
ENDTIME=`date +%s`
RUNTIMESEC=$((ENDTIME-STARTTIME))
MIN=$(((RUNTIMESEC)/60))
SEC=$(((RUNTIMESEC)%60))
echo "=============================================================="
echo "RUNTIME" "$MIN:$SEC"
echo "OK!! You can remove the BOOTDISK ${DEVICE} now!!"
exit 0
}
bpi_createnewdisk() {
usage
if [ ! -b ${DEVICE} ] ; then
echo "Warning: CAN NOT USE ${DEVICE}"
exit 0
fi
echo "Info: Try to create NEW BOOTDISK ${DEVICE} with ${IMGFILE}"
requires_root
echo "=============================================================="
STARTTIME=`date +%s`
date
#blue_led_on
umount_device
#
#step 1: 7456MB(emmc size)
create_new_disk
#step 2: 100MB
write_uboot
#
#step 3: 256MB
write_bpi_boot
#
#step 4: ~7456MB
write_bpi_root
#
(eject ${DEVICE}) > /dev/null 2>&1
#blue_led_off
date
ENDTIME=`date +%s`
RUNTIMESEC=$((ENDTIME-STARTTIME))
MIN=$(((RUNTIMESEC)/60))
SEC=$(((RUNTIMESEC)%60))
echo "=============================================================="
echo "RUNTIME" "$MIN:$SEC"
echo "OK!! You can remove the BOOTDISK ${DEVICE} now!!"
exit 0
}
bpi_migratefromroot() {
if [ -z ${IMGFILE} ] ; then
IMGFILE="${ROOTFILE}-${BPIBOARD}.img"
if [ -d ${ROOTFILE} ] ; then
A=$(date +%F)
B=$(basename ${ROOTFILE})
IMGFILE="${A}_${B}-${BPIBOARD}.img"
fi
echo IMGFILE=${IMGFILE}
fi
echo "Info: Try to create NEW IMAGE ${IMGFILE} with ${ROOTFILE}"
requires_root
echo "=============================================================="
STARTTIME=`date +%s`
date
#blue_led_on
#
#step 1: 7456MB(emmc size)
create_new_image
#step 2: 100MB
write_uboot
#
#step 3: 256MB
write_bpi_boot
#
#step 4: ~7456MB
write_bpi_root
#
#step 5: release loop dev.
release_loopdev
#blue_led_off
date
ENDTIME=`date +%s`
RUNTIMESEC=$((ENDTIME-STARTTIME))
MIN=$(((RUNTIMESEC)/60))
SEC=$(((RUNTIMESEC)%60))
echo "=============================================================="
echo "RUNTIME" "$MIN:$SEC"
echo "OK!!"
exit 0
}
bpi_migratefromimg() {
if [ -z ${UBOOTIMGGZ} ] ; then
UBOOTIMGGZ=BPI_M3_720P.img.gz
echo UBOOTIMGGZ=${UBOOTIMGGZ}
fi
echo "Info: Try to create NEW IMAGE ${IMGFILE} with ${UBOOTIMGGZ}"
requires_root
echo "=============================================================="
STARTTIME=`date +%s`
date
#blue_led_on
#
#step 1: 7456MB(emmc size)
mount_image
#step 2: 100MB
write_uboot
#
#step 3: 256MB
#write_bpi_boot
#
#step 4: ~7456MB
#write_bpi_root
#
#step 5: release loop dev.
release_loopdev
#step 6: img to zip
#cat ${IMGFILE} | pv | zip ${IMGFILE}.zip -
echo zip ${IMGFILE}.zip
zip ${IMGFILE}.zip ${IMGFILE}
#blue_led_off
date
ENDTIME=`date +%s`
RUNTIMESEC=$((ENDTIME-STARTTIME))
MIN=$(((RUNTIMESEC)/60))
SEC=$(((RUNTIMESEC)%60))
echo "=============================================================="
echo "RUNTIME" "$MIN:$SEC"
echo "OK!!"
exit 0
}
#
#BPI: download BPI-ROOT files from internet or intranet
#
bpi_root_intranet() {
#U="ftp://pi:bananapi@127.0.0.1/BPI-ROOT"
U="ftp://pi:bananapi@172.20.10.7/BPI-ROOT"
bpi-tools --update BPIURL="${U}/BPI-ROOT.lst" BPIFILE=BPI-ROOT.lst
bpi-tools --download BPIFILE=BPI-ROOT.lst BPIURLNEW="${U}"
}
bpi_root_google() {
U="https://googledrive.com/host/0B_YnvHgh2rwjVE10U3U0Q0NCdWM"
bpi-tools --download BPIURL="${U}" BPIFILE=BPI-ROOT.lst
}
bpi_root_main() {
if [[ "x${F_GOOGLE}" == "xYES" ]] ; then
echo bpi_root_google
bpi_root_google