-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlxw.sh
executable file
·225 lines (206 loc) · 5.95 KB
/
lxw.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh
set -e
launch(){
create $1
start
shell
}
# Create a new container using a specified image.
# Use the current folder name as container name.
# Map the host users uid/gid to the container user.
create(){
local image=$1
local container=${PWD##*/}
echo Creating container ${container} from image ${image}
lxc init ${image} ${container}
lxc config set ${container} boot.autostart false
lxc config set ${container} raw.idmap "both $(id -u) 0"
lxc config set ${container} user.lxw.type lxw
lxc config set ${container} user.lxw.base ${image}
lxc config set ${container} user.lxw.created "$(date -Iseconds)"
echo Container ${container} has been created.
}
# Start a container, using the current folder name as container name.
start(){
local container=${PWD##*/}
echo Starting container ${container}
lxc start ${container}
until lxw ip | grep -q -m 3 "."; do sleep 0.5 ; done
echo Container ${container} has been started.
echo The IP address is $(getip)
}
# Restart a container, using the current folder name as container name.
restart(){
local container=${PWD##*/}
echo Restarting container ${container}
lxc restart ${container}
until lxw ip | grep -q -m 3 "."; do sleep 0.5 ; done
echo Container ${container} has been restarted.
echo The IP address is $(getip)
}
# Shutdown a container, using the current folder name as container name.
stop(){
local container=${PWD##*/}
echo Stopping container ${container}
lxc stop ${container}
echo Container ${container} has been stopped.
}
# Destroy a container, using the current folder name as container name.
# Command needs to be confirmed, running containers are stopped before deletion.
destroy(){
local container=${PWD##*/}
echo Killing container ${container}
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) delete;;
* ) exit;;
esac
}
delete(){
local container=${PWD##*/}
lxc delete ${container} -f
echo Container ${container} has been removed.
}
# Open a shell inside a container, using the current folder name as container name.
# We don't know the current shell, so we use a little su tweak here.
shell(){
local container=${PWD##*/}
echo Entering container ${container}
lxc exec ${container} su -
}
# Run a single shell command inside a container, using the current folder name as container name.
execute(){
local container=${PWD##*/}
lxc exec ${container} -t -- ${command}
}
# Mount a host folder inside the container, using the current folder name as container name.
# We need to specify an alias for the mount, the (absolute or relative) path on the host,
# and the absolute path inside the container.
mnt(){
local device=$1
local source=$(readlink -e $2)
local target=$3
local container=${PWD##*/}
echo Mounting device ${device} to container ${container}: ${source} "=>" ${target}
lxc config device add ${container} ${device} disk source=${source} path=${target}
echo Folder has been mounted.
}
# Unmount a shared folder, using the current folder name as container name, and the alias
unmnt(){
local device=$1
local container=${PWD##*/}
echo Unmounting device ${device} from container ${container}
lxc config device remove ${container} ${device}
echo Folder has been unmounted.
}
# Show the current status of the container (e.g. the IP)
status(){
local container=${PWD##*/}
lxc list ${container} user.lxw.type=lxw
}
# List containers created by LXW
list(){
lxc list user.lxw.type=lxw
}
# Show the current ip address of the container
getip(){
local container=${PWD##*/}
lxc list ${container} -c4 --format=csv | awk '{print $1;}'
}
# Show usage instructions
usage(){
echo "LXW is a simple wrapper for LXD cli commands"
echo ""
echo "Usage: lxw <command>"
echo ""
echo "Commands:"
echo " launch <image> - Quickly create a container using an image, start it and enter a shell"
echo " create <image> - Create a container using an image"
echo " start - Start the container"
echo " restart - Restart the container"
echo " stop - Stop the container"
echo " kill - Remove the container (needs confirmation)"
echo " mount <alias> <host_dir> <container_dir> - Share a host folder with container"
echo " unmount <alias> - Stop sharing a host folder with container"
echo " shell - Open a shell inside the container"
echo " exec <command> - Run a single shell command inside the container"
echo " status - Show current container status"
echo " list - List containers created by LXW"
echo " ip - Show current container ip address"
echo " help - Show usage instructions"
}
# We need at least one argument
if [ $# -eq 0 ]; then
usage
exit 1
fi
# Run the command given as argument
case "$1" in
"launch")
if [ $# -ne 2 ]; then
usage
exit 1;
fi
launch $2
;;
"create")
if [ $# -ne 2 ]; then
usage
exit 1;
fi
create $2
;;
"start")
start
;;
"restart")
restart
;;
"stop")
stop
;;
"kill")
destroy
;;
"mount")
if [ $# -ne 4 ]; then
usage
exit 1;
fi
mnt $2 $3 $4
;;
"unmount")
if [ $# -ne 2 ]; then
usage
exit 1;
fi
unmnt $2
;;
"shell")
shell
;;
"exec")
if [ $# -lt 2 ]; then
usage
exit 1;
fi
shift
command="$*"
execute
;;
"status")
status
;;
"list")
list
;;
"ip")
getip
;;
"help")
usage
;;
*)
usage
;;
esac