Repository navigation
Expand file tree
/
Copy pathdev
More file actions
executable file
·278 lines (248 loc) · 6.57 KB
/
Copy pathdev
File metadata and controls
executable file
·278 lines (248 loc) · 6.57 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
set -e
source ~/.secrets/gcp-defaults.sh
HALTD_PORT=5000
GCP_BUCKET=9cp
GCP_PROJECT=$GCP_HENRI_PROJECT
GCP_ZONE=$GCP_HENRI_ZONE
GCP_REGION=$GCP_HENRI_REGION
GCP_NETWORK=$GCP_HENRI_NET_9CP
vm() {
local mode=$1
#local ram=$(awk '/MemTotal:/ { printf "%d", $2/1024-250 }' /proc/meminfo)
local ram=1300 # g1-small instance has 1.7G ram
local opts=
local cd=
local disk0=
local disk1=
case $mode in
debian-install)
cd=debian.iso
;;
debian-boot)
disk0=debian.raw
disk1=disk.raw
;;
9front-iso)
cd=9front.iso
disk0=disk.raw
;;
test)
disk0=disk.raw
# opts+="-curses "
# opts+="-nographic "
;;
*)
echo "unknown mode: $mode" 1>&2
exit 1
esac
[ -z $cd ] || opts+="-drive if=none,id=vd3,format=raw,file=$cd \
-device scsi-cd,drive=vd3,bootindex=0 "
[ -z $disk0 ] || opts+="-drive if=none,id=vd0,format=raw,file=$disk0 \
-device scsi-hd,drive=vd0 "
[ -z $disk1 ] || opts+="-drive if=none,id=vd1,format=raw,file=$disk1 \
-device scsi-hd,drive=vd1 "
qemu-system-x86_64 \
-m $ram -smp $(nproc) \
-net user,$forwards \
-net nic,model=e1000 \
-device virtio-scsi-pci,id=scsi \
$opts
}
upload() {
local name=$1
[ -z $name ] && {
echo "usage: upload NAME"
exit 2
}
local tar=$name
tar --format=oldgnu -Sczf $tar.tar.gz disk.raw
gsutil list gs://$GCP_BUCKET || gsutil mb -l $GCP_REGION gs://$GCP_BUCKET
gsutil rm gs://$GCP_BUCKET/$tar.tar.gz || true
gsutil cp $tar.tar.gz gs://$GCP_BUCKET
gcloud compute images delete $name --quiet || true
gcloud compute images create $name \
--storage-location=$GCP_REGION \
--source-uri gs://$GCP_BUCKET/$tar.tar.gz
}
disk-to-image() {
local name=$1
[[ $# != 1 && $# != 2 ]] && {
echo -e "usage: disk-to-image NAME [IMAGE]\n eg disk-to-image nino\n or disk-to-image nino nino-123"
exit 2
}
local image="${2:-$name-$(date +%F-%H-%M)}"
echo "creating image $image"
gcloud compute images create $image \
--project=$GCP_PROJECT \
--source-disk=$name \
--source-disk-zone=$GCP_ZONE
}
export-image() {
local name="$1"
[ -z $name ] && {
echo "usage: export-image IMAGE_NAME"
exit 2
}
local uri=gs://$GCP_BUCKET/"$name".tar.gz
echo "exporting image $name to $uri"
gcloud compute images export \
--destination-uri "$uri" \
--image "$name"
}
cp-image() {
local name="$1"
[ -z $name ] && {
echo "usage: cp-image IMAGE_NAME_WO_tar.gz"
exit 2
}
local uri=gs://$GCP_BUCKET/"$name".tar.gz
echo "downloading $uri to $name.tar.gz"
gsutil cp "$uri" $name.tar.gz
du -sh $name.tar.gz
}
download-disk() {
local name="$1"
[ -z $name ] && {
echo "usage: download-disk IMAGE_NAME_WO_tar.gz"
return 2
}
status $name && {
echo "refusing to create image for running vm"
return 1
}
local image="$name-$(date +%F-%H-%M)"
disk-to-image $name $image
export-image $image
cp-image $image
}
status() {
local name=$1
[ -z $name ] && {
echo "usage: status NAME"
exit 2
}
gcloud compute instances list | grep "^$name " || (
echo "not running: $name"
return 1
)
}
start() {
local name=$1
[ -z $name ] && {
echo "usage: start NAME"
exit 2
}
local machine_type=g1-small
local ip_opt=
case $name in
nino)
# machine_type=custom-2-4096
# machine_type=custom-4-4096
# machine_type=e2-custom-4-2048
machine_type=e2-custom-4-3072 # $92
# machine_type=e2-custom-6-3072 # $132
# machine_type=custom-8-8192
ip_opt="
--network-interface=network=ninecp,address=$GCP_HENRI_NINO_IP,private-network-ip=$GCP_HENRI_NINO_IP_NINECP \
--disk=name=$name,device-name=$name,mode=rw,boot=yes"
;;
*)
echo "unknown: $name" 1>&2
exit 2
;;
esac
if gcloud compute instances list | grep -Eq "^$name "; then
echo "starting existing machine"
gcloud compute instances start $name \
--zone $GCP_ZONE
else
echo "creating machine"
gcloud compute instances create $name \
--zone $GCP_ZONE \
--machine-type=$machine_type \
--metadata serial-port-enable=TRUE \
--can-ip-forward \
$ip_opt
fi
#serial $name
}
stop() {
local name=$1
local ip
case $name in
nino) # this is nino on gcp
ip=192.168.0.1
;;
*)
echo "unknown: $name" 1>&2
exit 2
;;
esac
echo halt > /dev/tcp/$ip/$HALTD_PORT
}
serial() {
local name=$1
[ -z $name ] && {
echo "usage: serial NAME"
exit 2
}
gcloud compute connect-to-serial-port --zone $GCP_ZONE $name
}
## creates firewall rules for my current ip to access my vms
fw() {
echo "use gcloud2"; exit 1
local fw_name=ninecp
local range="$(curl -sf ifconfig.me)"
[ -z $range ] && return 1
gcloud compute \
--project=$GCP_PROJECT \
--quiet \
firewall-rules delete $fw_name \
|| true
local haltd_port=5000
local ssh_9="tcp:$haltd_port,tcp:22,tcp:564,tcp:567,tcp:655,tcp:5356,tcp:17007,tcp:17010,tcp:17019,tcp:17567,icmp"
local ssh_tinc="tcp:22,tcp:655,icmp"
local rules="$ssh_tinc"
gcloud compute \
--project=$GCP_PROJECT \
firewall-rules create $fw_name \
--description="For laptop to connect to plan9 boxes" \
--direction=INGRESS \
--priority=1000 \
--network=$GCP_NETWORK \
--action=ALLOW \
--rules=$rules \
--source-ranges="$range/32"
}
term() {
echo "if you can't connect, consider running: sudo systemctl start tinc; gcloud2 fw"
local name=$1
local user=henri
case $name in
nino-public-ip)
dwm2 workspace 7
ip=$GCP_HENRI_NINO_IP
exec drawterm -h $ip -s $ip -a $ip -u $user
;;
nino)
dwm2 workspace 7
ip=nino
exec drawterm -h $ip -s $ip -a $ip -u $user
;;
*)
echo "unknown: $name"
;;
esac
}
http-hack() {
# hget http://10.0.2.2:9090 | rc
while :; do
local sz=$(wc -c ins | awk '{print $1}')
sz=$(( sz + 2 ))
(echo -e "HTTP/1.1 200 OK\r\nContent-Length: $sz\n\n "; cat ins; echo) \
| nc -l -p 9090 localhost || true
done
}
cd "$(dirname $0)"
"$@"