forked from billw2/rpi-clone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpi-clone-setup
executable file
·144 lines (122 loc) · 2.9 KB
/
rpi-clone-setup
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
#!/bin/bash
# Usage: rpi-clone-setup {-t|--test} hostname
# eg: sudo rpi-clone-setup bozo
#
# This script is automatically run by rpi-clone (when it is given -s options)
# to setup an alternate hostname. A cloned file system mounted on /mnt/clone
# is expected unless testing with the -t option.
#
# Or, this script can be run by hand at the end of a clone when rpi-clone
# pauses with the cloned file systems still mounted on /mnt/clone.
#
# Or, run this script by hand with -t to process files in test directories
# under /tmp/clone-test. Run -t and look at the files to see if the files
# have been edited OK.
# eg: sudo rpi-clone-setup -t bozo
#
# This is a starter script that handles only /etc/hosts and /etc/hostname.
# Make sure the script works correctly for your /etc/hosts file.
#
# If adding a customization for another file:
# Add the file to file_list.
# If needed, add a mkdir -p line to the "if ((testing))" part.
# Add the scripting necessary to customize the file.
# Test new scripting by running: rpi-clone-setup -t newhostname
#
file_list="etc/hostname etc/hosts"
clone=/mnt/clone
clone_test=/tmp/clone-test
PGM=`basename $0`
if [ `id -u` != 0 ]
then
echo "You must be root to run $PGM"
exit 0
fi
function usage
{
echo "Usage: $PGM hostname {-t|--test}"
echo " Eg: $PGM rpi1"
echo " Modify files appropriate to set up for a new host."
echo " Files handled are:"
for file in $file_list
do
echo " $file"
done
echo ""
echo "If testing (-t flag) files are copied and processed to $clone_test"
echo ""
exit 0
}
testing=0
while [ "$1" ]
do
case "$1" in
-t|--test)
testing=1
;;
*)
if [ "$newhost" != "" ]
then
echo "Bad args"
usage
fi
newhost=$1
;;
esac
shift
done
if [ "$newhost" = "" ]
then
echo -e "You must specify a target hostname\n"
usage
fi
echo -e "\t$newhost\t- target hostname"
if ((!testing)) && [ ! -d /mnt/clone/etc ]
then
echo "A destination clone file system is not mounted on /mnt/clone"
echo "Aborting!"
exit 0
fi
if ((testing))
then
cd /tmp
rm -rf $clone_test
clone=$clone_test
mkdir -p $clone/etc
echo "**********************************************"
echo "Testing setup: copying files to $clone"
for file in $file_list
do
echo " cp /$file $clone/$file"
cp /$file $clone/$file
done
echo "This test run will modify those files."
echo "**********************************************"
echo ""
fi
##
# Set /etc/hostname
#
cd $clone/etc
echo $newhost > hostname
#
# Read it back to verify.
#
echo "$clone/etc/hostname - set new hostname: "
LINE=`cat hostname`
echo -e "$LINE\n"
##
# Edit /etc/hosts - edit the sed command if editing fails for your /etc/hosts.
#
cd $clone/etc
sed -i s/"$HOSTNAME"/"$newhost"/ hosts
#
# Read it back to verify.
#
echo "$clone/etc/hosts - set new hostname \"$newhost\" in lines: "
LINE=`grep $newhost hosts`
echo -e "$LINE\n"
##
# Add more customizations if needed.
#
exit 0