This repository was archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwrite_image
More file actions
executable file
·103 lines (83 loc) · 3.68 KB
/
Copy pathwrite_image
File metadata and controls
executable file
·103 lines (83 loc) · 3.68 KB
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
#!/bin/sh
# Write the turtlebot3 image to desk and
# perform post-writing steps that do not require a boot
# TODO: Make a good interface for this script
# The raspberry pi image location
raspi_image=$1
# The sdcard device (e.g. /dev/mmcblk0).
sdcard_dev=$2
# Hostname for the turtlebot
turtlebot_name=$3
# Public key, used to access the turtlebot
public_key=$4
# The network connection profile. If it is called <something>.robot.nmconnection it will use
# robotdns (https://github.com/m-elwin/robotdns)
if [ -f "$5" ] # if the specified file does not exist, search in /etc/NetworkManager/system-connections
then
nmconnection=$5
else
nmconnection="/etc/NetworkManager/system-connections/${5}.nmconnection"
fi
# The address of the robotdns server (or blank if not using robotdns).
# If using robotdns, the network connection profile must already be a valid robotdns profile and be named <something>.robot.nmconnection
robotdns=$6
# user id of the msr user. This is always the same when we create the image, and we need it to change ownership
# (or we'd have to proot in)
msr_uid=1000
# compute the partitions of the device.
# naming convention depends on if they end
# in a letter or a number.
if [ $(expr ${sdcard_dev} : '.*[0-9]') = 0 ]
then
# block device name not end in number
sdcard_bootpart=${sdcard_dev}1
sdcard_ospart=${sdcard_dev}2
else
# block device name ends in number
sdcard_bootpart=${sdcard_dev}p1
sdcard_ospart=${sdcard_dev}p2
fi
sudo umount $sdcard_bootpart
sudo umount $sdcard_ospart
echo "Writing the image (takes some time)"
sudo dd bs=4M if=$raspi_image of=$sdcard_dev conv=fsync oflag=direct status=progress
echo "Expanding partition"
sudo parted $sdcard_dev resizepart 2 100%
sudo e2fsck -fy $sdcard_ospart
sudo resize2fs $sdcard_ospart
echo "Mounting the sd card"
mkdir -p /tmp/raspi
sudo mount $sdcard_ospart /tmp/raspi
echo "Setting hostname to $turtlebot_name"
echo $turtlebot_name | sudo tee /tmp/raspi/etc/hostname
echo "127.0.1.1 $turtlebot_name" | sudo tee -a /tmp/raspi/etc/hosts
echo "Copying the public key"
sudo mkdir -p /tmp/raspi/home/msr/.ssh
sudo cp $public_key /tmp/raspi/home/msr/.ssh/authorized_keys
sudo chown $msr_uid:$msr_uid /tmp/raspi/home/msr/.ssh/authorized_keys
# Install the network connection, while automatically removing uuid or wifi interface names
echo "Installing the network connection profile"
robot_nmfile=/tmp/raspi/etc/NetworkManager/system-connections/$(basename $nmconnection)
sudo grep -v "uuid=\|interface-name=" $nmconnection | sudo tee $robot_nmfile
sudo chmod 600 $robot_nmfile
if [ ! -z "$robotdns" ]
then
# create the settings file
echo "export nmconnect=$(basename $nmconnection | cut -d'.' -f 1)" | sudo tee ${robot_nmfile}.settings
echo "export dnshost=$robotdns" | sudo tee -a ${robot_nmfile}.settings
echo "export user_home=/home/msr" | sudo tee -a ${robot_nmfile}.settings
sudo chmod 600 ${robot_nmfile}.settings
# Generate the ssh key and install it
rm -f /tmp/id_robotdns.pub /tmp/id_robotdns
ssh-keygen -t ed25519 -C $turtlebot_name -f /tmp/id_robotdns -q -N ""
sudo mv /tmp/id_robotdns /tmp/raspi/home/msr/.ssh/
sudo cp /tmp/id_robotdns.pub /tmp/raspi/home/msr/.ssh/
sudo chown $msr_uid:$msr_uid /tmp/raspi/home/msr/.ssh/id_robotdns
sudo chown $msr_uid:$msr_uid /tmp/raspi/home/msr/.ssh/id_robotdns.pub
# install the fingerprint
ssh-keyscan -H $robotdns | sudo tee /tmp/raspi/home/msr/.ssh/known_hosts
sudo chown $msr_uid:$msr_uid /tmp/raspi/home/msr/.ssh/known_hosts
cp /tmp/raspi/home/msr/.ssh/id_robotdns.pub /tmp/id_robotdns.pub
echo "Send /tmp/id_robotdns.pub to your system administrator so the robot can access the robotdns server"
fi
sudo umount /tmp/raspi