-
Notifications
You must be signed in to change notification settings - Fork 34
/
bashconvert
87 lines (79 loc) · 2.59 KB
/
bashconvert
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
#!/bin/bash
# Check if the 'pct' command is available on the host machine (Proxmox)
if ! command -v pct &> /dev/null
then
echo "pct could not be found. This script must be run on the host machine Proxmox"
exit 1
fi
# Function to display script usage
usage()
{
cat <<EOF
$1 -h|--help
-n|--name [LXC container name]
-t|--target [target machine SSH URI]
-P|--port [target SSH port]
-i|--id [Proxmox container ID]
-s|--root-size [rootfs size in GB]
-a|--ip [target container IP]
-b|--bridge [bridge interface]
-g|--gateway [gateway IP]
-m|--memory [memory in MB]
-d|--disk-storage [target Proxmox storage pool]
-p|--password [root password for container (min. 5 chars)]
EOF
return 0
}
# Parse command-line options
options=$(getopt -o n:t:P:i:s:a:b:g:m:d:p:f -l help,name:,target:,port:,id:,root-size:,ip:,bridge:,gateway:,memory:,disk-storage:,password:,foo: -- "$@")
if [ $? -ne 0 ]; then
usage "$(basename "$0")"
exit 1
fi
eval set -- "$options"
# Process command-line options
while true
do
case "$1" in
-h|--help) usage "$0" && exit 0;;
-n|--name) name=$2; shift 2;;
-t|--target) target=$2; shift 2;;
-P|--port) port=$2; shift 2;;
-i|--id) id=$2; shift 2;;
-s|--root-size) rootsize=$2; shift 2;;
-a|--ip) ip=$2; shift 2;;
-b|--bridge) bridge=$2; shift 2;;
-g|--gateway) gateway=$2; shift 2;;
-m|--memory) memory=$2; shift 2;;
-p|--password) password=$2; shift 2;;
-d|--disk-storage) storage=$2; shift 2;;
--) shift 2; break ;;
*) break ;;
esac
done
# Function to collect file system data, excluding unnecessary directories and files
collectFS() {
tar -czvvf - -C / \
--exclude="sys" \
--exclude="dev" \
--exclude="run" \
--exclude="proc" \
--exclude="*.log" \
--exclude="*.log*" \
--exclude="*.gz" \
--exclude="*.sql" \
--exclude="swap.img" \
.
}
# SSH into the target machine, execute the file system collection function, and save to a temporary file
ssh -p "$port" "root@$target" "$(typeset -f collectFS); collectFS" > "/tmp/$name.tar.gz"
# Create a Proxmox container using the collected file system data and provided parameters
pct create "$id" "/tmp/$name.tar.gz" \
-description LXC \
-hostname "$name" \
--features nesting=1 \
-memory "$memory" -nameserver 8.8.8.8 \
-net0 name=eth0,ip="$ip"/24,gw="$gateway",bridge="$bridge" \
--rootfs "$rootsize" -storage "$storage" -password "$password"
# Remove the temporary file
rm -rf "/tmp/$name.tar.gz"