-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathinstall_to_usb
executable file
·154 lines (126 loc) · 3.76 KB
/
install_to_usb
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
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
echo ""
date
echo -e "\033[36m===================================="
echo "Installing Linux system to USB drive"
echo -e "====================================\033[37m"
setterm -default
echo ""
fatsize=64
sdcard=${1}
_format=${2}
odir="/tmp/_extdir"
if [ ! -b ${sdcard} ]; then
echo "Error: USB partition not found."
exit 1
fi
umount ${sdcard} > /dev/null 2>&1
#----------------------------------------------------------
echo ""
if [ "${_format}" = "noformat" ] ; then
echo -n "WARNING: USB partition ${sdcard} WILL BE UPDATED !, Continue (y/N)? "
else
echo -n "WARNING: USB partition ${sdcard} WILL BE ERASED !, Continue (y/N)? "
fi
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo "."
echo "Canceled.."
exit 0
fi
echo ""
#----------------------------------------------------------
if [ "${_format}" != "noformat" ] ; then
dd if=/dev/zero of=${sdcard} bs=1M count=1 oflag=direct > /dev/null 2>&1
sync
sleep 1
if [ "${_format}" = "btrfs" ] ; then
echo "Formating linux partition (btrfs), please wait ..."
# format as btrfs
mkfs.btrfs -O ^extref,^skinny-metadata -f -L usblinux ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR formating btrfs partition."
exit 1
fi
else
echo "Formating linux partition (ext4), please wait ..."
mkfs.ext4 -L usblinux ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR formating ext4 partition."
exit 1
fi
fi
sync
echo " linux partition formated."
#************************************************************************
else
_ptype=`blkid | grep ${sdcard} | grep -o "\"btrfs\""`
if [ "${_ptype}" != "" ]; then
_format="btrfs"
fi
fi
# -------------------------------------------------------------------
if [ ! -d $odir ]; then
mkdir -p $odir
fi
rm -rf $odir/* > /dev/null 2>&1
sync
umount $odir > /dev/null 2>&1
sleep 1
# ================
# MOUNT PARTITIONS
# ================
if [ "${_format}" = "btrfs" ] ; then
_mntopt="-o compress-force=lzo"
else
_mntopt=""
fi
echo ""
echo "Mounting USB partitions..."
if ! mount ${_mntopt} ${sdcard} $odir; then
echo "ERROR mounting linux partitions..."
exit 1
fi
echo "linux partition mounted to $odir"
#-----------------------------------------------------------------------------------------------
echo ""
echo "Copying file system to USB drive ..."
echo ""
#-----------------------------------------------------------------------------------------
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats / $odir/ > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo " ERROR."
fi
#-----------------------------------------------------------------------------------------
sync
rm $odir/usr/local/bin/fs_resize_warning > /dev/null 2>&1
echo " Creating \"fstab\""
echo "# OrangePI fstab" > $odir/etc/fstab
if [ "${_format}" = "btrfs" ] ; then
echo "${sdcard} / btrfs subvolid=0,noatime,nodiratime,compress=lzo 0 1" >> $odir/etc/fstab
else
echo "/dev/mmcblk0p2 / ext4 errors=remount-ro,noatime,nodiratime 0 1" >> $odir/etc/fstab
fi
echo "/dev/mmcblk0p1 /media/boot vfat defaults 0 0" >> $odir/etc/fstab
echo "tmpfs /tmp tmpfs nodev,nosuid,mode=1777 0 0" >> $odir/etc/fstab
sync
echo "root=${sdcard}" > /media/boot/cmdline.txt
# UMOUNT
if ! umount $odir; then
echo "ERROR unmounting linux partitions."
exit 0
fi
rm -rf $odir/* > /dev/null 2>&1
rmdir $odir > /dev/null 2>&1
sync
echo ""
echo -e "\033[36m************************************"
echo "Linux system installed to USB drive."
echo -e "************************************\033[37m"
setterm -default
echo ""
exit 0