-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-setup.sh
63 lines (50 loc) · 1.36 KB
/
lxc-setup.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
#!/bin/bash
#
# Author: Indrek Haav
# Source: https://github.com/IndrekHaav/proxmox-helpers
set -e
setup () {
# anything in this function will be executed inside the container
echo "Setup starting on $(date)"
}
if [ "$EUID" -ne 0 ]; then
echo "Error: this script must be run as root!"
exit 1
fi
if [ "${container:=}" == "lxc" ]; then
# we're inside the container, so call the setup function and exit
setup
exit 0
fi
# the following runs on the host
if ! command -v pct > /dev/null; then
echo "Error: pct not found. Are you running on Proxmox?"
exit 1
fi
script_name=$(basename "$0")
ctid=$1
if [ -z "$ctid" ]; then
echo "Usage: $script_name <CTID>"
exit 1
fi
if ! status=$(pct status "$ctid" 2>/dev/null); then
echo "Error: container $ctid not found!"
exit 1
fi
if [ "$status" != "status: running" ]; then
echo "Error: container $ctid is not running!"
exit 1
fi
hostname=$(pct exec "$ctid" hostname)
echo "Setting up container $ctid ($hostname)"
echo "Copying script to container... "
if ! pct push "$ctid" "$0" "/root/$script_name" --perms 760; then
echo "Error: unable to push script to container; please copy manually."
exit 1
fi
echo "Executing script..."
pct exec "$ctid" -- bash -c "/root/$script_name | tee /root/setup.log"
echo "Cleaning up... "
pct exec "$ctid" rm "/root/$script_name"
echo "DONE"
exit 0