-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-rootfs.sh
executable file
·142 lines (117 loc) · 3.73 KB
/
setup-rootfs.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
#!/bin/bash
_sdir=$(dirname "$(readlink -f "$0")")
_rel="${_sdir##$PWD}"
set -u
LXC_PATH="/var/lib/lxc"
container_name="fc"
show_help(){
cat <<HELP
$(basename $0) [options]
Options:
--name NAME : Container name (default: $container_name)
--lxc-path PATH : LXC Path (default: $LXC_PATH)
--freecad-src SRC : Use SRC as path to copy existing FreeCAD git
source into the container
HELP
}
is_on_btrfs(){
stat -f --format=%T "$1" | grep -q btrfs
}
end_message(){
cat <<EOL
Run FreeCAD anytime with:
.$_rel/freecad.sh
To update your FreeCAD binary:
.$_rel/update-fc.sh
EOL
}
die(){
>&2 echo
>&2 echo "$@"
exit 1
}
help_die(){
>&2 echo
>&2 echo "$@"
show_help
exit 1
}
# Parse command line arguments
# ---------------------------
support_ssh_x=false
user="fc"
freecad_src=
# Initialize parameters
# ---------------------------
args_backup=("$@")
args=()
_count=1
while [ $# -gt 0 ]; do
key="${1:-}"
case $key in
-h|-\?|--help)
show_help # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
--name) shift
container_name="$1"
;;
--user) shift
user="$1"
;;
--lxc-path) shift
LXC_PATH="$1"
;;
--freecad-src) shift
freecad_src="$1"
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
help_die "Unknown option: $1"
;;
*) # Generate the new positional arguments: $arg1, $arg2, ... and ${args[@]}
if [[ ! -z ${1:-} ]]; then
declare arg$((_count++))="$1"
args+=("$1")
fi
;;
esac
[[ -z ${1:-} ]] && break || shift
done; set -- "${args_backup[@]-}"
# Use $arg1 in place of $1, $arg2 in place of $2 and so on,
# "$@" is in the original state,
# use ${args[@]} for new positional arguments
[[ -d "$LXC_PATH/$container_name" ]] || die "Please create the container ($container_name) first."
[[ "$(whoami)" == "root" ]] || { sudo "$0" "$@"; exit 0; }
CHROOT="$_sdir/run-in-chroot.sh -n $container_name --unattended"
# install basic packages
packages="nano sudo git" # tmux
$support_ssh_x && packages="$packages xbase-clients"
password=$user
$CHROOT -- "export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH'; \
apt-get update --allow-releaseinfo-change && apt-get install -y $packages; \
grep $user /etc/passwd -q || { \
echo 'adding user $user'; \
useradd -m $user; \
usermod -a -G sudo $user; \
echo "$user:$password" | chpasswd; \
} && echo "User $user already exits.""
builder_scripts_on_rootfs="/home/$user/$(basename $(dirname $_sdir))"
builder_scripts="$LXC_PATH/$container_name/rootfs/$builder_scripts_on_rootfs"
if [[ ! -d "$builder_scripts" ]]; then
cp -a "$(dirname "$_sdir")" "$builder_scripts"
$CHROOT -- "chown $user $builder_scripts_on_rootfs -R"
fi
freecad_src_target_on_rootfs="/home/$user/FreeCAD"
freecad_src_target="$LXC_PATH/$container_name/rootfs/$freecad_src_target_on_rootfs"
if [[ -n "$freecad_src" && ! -d "$freecad_src_target" ]]; then
echo "Provided FreeCAD git source, copying."
is_on_btrfs $LXC_PATH && opt="--reflink=always" || opt=""
cp -a $opt "$freecad_src" "$freecad_src_target"
$CHROOT -- "chown $user $freecad_src_target_on_rootfs -R"
fi
$CHROOT -- "cd /home/$user; $(basename $builder_scripts)/install-fc-deps.sh || dpkg --configure -a \
&& sudo -u $user $(basename $builder_scripts)/build-fc.sh"
echo "FreeCAD is successfully compiled."
end_message