-
Notifications
You must be signed in to change notification settings - Fork 90
/
cli.sh
executable file
·1265 lines (1156 loc) · 30 KB
/
cli.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
################################################################################
#
# Linux Deploy CLI
# (C) 2012-2019 Anton Skshidlevsky <[email protected]>, GPLv3
#
################################################################################
VERSION="2.5.1"
################################################################################
# Common
################################################################################
msg()
{
echo "$@"
}
is_ok()
{
if [ $? -eq 0 ]; then
if [ -n "$2" ]; then
msg "$2"
fi
return 0
else
if [ -n "$1" ]; then
msg "$1"
fi
return 1
fi
}
get_platform()
{
local arch="$1"
if [ -z "${arch}" ]; then
arch=$(uname -m)
fi
case "${arch}" in
arm64|aarch64)
echo "arm_64"
;;
arm*)
echo "arm"
;;
x86_64|amd64)
echo "x86_64"
;;
i[3-6]86|x86)
echo "x86"
;;
*)
echo "unknown"
;;
esac
}
get_uuid()
{
cat /proc/sys/kernel/random/uuid
}
multiarch_support()
{
if [ -d "/proc/sys/fs/binfmt_misc" ]; then
return 0
else
return 1
fi
}
selinux_inactive()
{
if [ -e "/sys/fs/selinux/enforce" ]; then
return $(cat /sys/fs/selinux/enforce)
else
return 0
fi
}
loop_support()
{
if [ -n "$(losetup -f)" ]; then
return 0
else
return 1
fi
}
user_home()
{
[ -e "${CHROOT_DIR}/etc/passwd" ] || return 1
local user_name="$1"
[ -n "${user_name}" ] || return 1
echo $(grep -m1 "^${user_name}:" "${CHROOT_DIR}/etc/passwd" | awk -F: '{print $6}')
}
user_shell()
{
[ -e "${CHROOT_DIR}/etc/passwd" ] || return 1
local user_name="$1"
[ -n "${user_name}" ] || return 1
echo $(grep -m1 "^${user_name}:" "${CHROOT_DIR}/etc/passwd" | awk -F: '{print $7}')
}
is_mounted()
{
local mount_point="$1"
[ -n "${mount_point}" ] || return 1
if $(grep -q " ${mount_point%/} " /proc/mounts); then
return 0
else
return 1
fi
}
is_archive()
{
local src="$1"
[ -n "${src}" ] || return 1
if [ -z "${src##*gz}" -o -z "${src##*bz2}" -o -z "${src##*xz}" ]; then
return 0
fi
return 1
}
get_pids()
{
local pid pidfile pids
for pid in $*
do
pidfile="${CHROOT_DIR}${pid}"
if [ -e "${pidfile}" ]; then
pid=$(cat "${pidfile}")
fi
if [ -e "/proc/${pid}" ]; then
pids="${pids} ${pid}"
fi
done
if [ -n "${pids}" ]; then
echo ${pids}
return 0
else
return 1
fi
}
is_started()
{
get_pids $* >/dev/null
}
is_stopped()
{
is_started $*
test $? -ne 0
}
kill_pids()
{
local pids=$(get_pids $*)
if [ -n "${pids}" ]; then
kill -9 ${pids}
return $?
fi
return 0
}
remove_files()
{
local item target
for item in $*
do
target="${CHROOT_DIR}${item}"
if [ -e "${target}" ]; then
rm -f "${target}"
fi
done
return 0
}
make_dirs()
{
local item target
for item in $*
do
target="${CHROOT_DIR}${item}"
if [ -d "${target%/*}" -a ! -d "${target}" ]; then
mkdir "${target}"
fi
done
return 0
}
chroot_exec()
{
unset TMP TEMP TMPDIR LD_PRELOAD LD_DEBUG
local path="${PATH}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
if [ "$1" = "-u" ]; then
local username="$2"
shift 2
fi
case "${METHOD}" in
chroot)
if [ -n "${username}" ]; then
if [ $# -gt 0 ]; then
chroot "${CHROOT_DIR}" /bin/su - ${username} -c "$*"
else
chroot "${CHROOT_DIR}" /bin/su - ${username}
fi
else
PATH="${path}" chroot "${CHROOT_DIR}" $*
fi
;;
proot)
if [ -z "${PROOT_TMP_DIR}" ]; then
export PROOT_TMP_DIR="${TEMP_DIR}"
fi
local mounts="-b /proc -b /dev -b /sys"
if [ -n "${MOUNTS}" ]; then
mounts="${mounts} -b ${MOUNTS// / -b }"
fi
local emulator
if [ -n "${EMULATOR}" ]; then
emulator="-q ${EMULATOR}"
fi
if [ -n "${username}" ]; then
if [ $# -gt 0 ]; then
proot -r "${CHROOT_DIR}" -w / ${mounts} ${emulator} -0 -l -e /bin/su - ${username} -c "$*"
else
proot -r "${CHROOT_DIR}" -w / ${mounts} ${emulator} -0 -l -e /bin/su - ${username}
fi
else
PATH="${path}" proot -r "${CHROOT_DIR}" -w / ${mounts} ${emulator} -0 -l -e $*
fi
;;
esac
}
################################################################################
# Params
################################################################################
params_read()
{
local conf_file="$1"
[ -e "${conf_file}" ] || return 1
local item key val
while read item
do
key=$(echo ${item} | grep -o '^[0-9A-Z_]\{1,32\}')
val=${item#${key}=}
if [ -n "${key}" ]; then
eval ${key}="${val}"
if [ -n "${OPTLST##* ${key} *}" ]; then
OPTLST="${OPTLST}${key} "
fi
fi
done < ${conf_file}
}
params_write()
{
local conf_file="$1"
[ -n "${conf_file}" ] || return 1
echo "# ${conf_file##*/} $(date '+%F %R')" > ${conf_file}
local key val
for key in ${OPTLST}
do
eval "val=\$${key}"
if [ -n "${key}" -a -n "${val}" ]; then
echo "${key}=\"${val}\"" >> ${conf_file}
fi
done
}
params_parse()
{
OPTIND=1
if [ $# -gt 0 ] ; then
local item key val
for item in "$@"
do
key=$(expr "${item}" : '--\([0-9a-z-]\{1,32\}=\{0,1\}\)' | sed 'y/-abcdefghijklmnopqrstuvwxyz/_ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
if [ -n "${key##*=*}" ]; then
val="true"
else
key=${key%*=}
val=$(expr "${item}" : '--[0-9a-z-]\{1,32\}=\(.*\)')
fi
if [ -n "${key}" ]; then
eval ${key}=\"${val}\"
let OPTIND=OPTIND+1
if [ -n "${OPTLST##* ${key} *}" ]; then
OPTLST="${OPTLST}${key} "
fi
fi
done
fi
#echo ${OPTLST} | tr ' ' '\n' | awk '!x[$0]++'
}
params_check()
{
local params_list="$@"
local key val params_lost
for key in ${params_list}
do
eval "val=\$${key}"
if [ -z "${val}" ]; then
params_lost="${params_lost} ${key}"
fi
done
if [ -n "${params_lost}" ]; then
msg "Missing parameters:${params_lost}"
return 1
fi
return 0
}
################################################################################
# Configs
################################################################################
config_which()
{
local conf_file="$1"
if [ -n "${conf_file}" ]; then
if [ -n "${conf_file##*/*}" ]; then
conf_file="${CONFIG_DIR}/${conf_file}.conf"
fi
echo "${conf_file}"
fi
}
config_update()
{
local source_conf="$1"; shift
local target_conf="$1"; shift
params_read "${source_conf}"
params_parse "$@"
params_write "${target_conf}"
}
config_list()
{
local conf_file="$1"
local conf
for conf in $(ls "${CONFIG_DIR}/")
do
(
unset DISTRIB ARCH SUITE INCLUDE
. "${CONFIG_DIR}/${conf}"
formated_desc=$(printf "%-15s %-10s %-10s %-10s %.30s\n" "${conf%.conf}" "${DISTRIB}" "${ARCH}" "${SUITE}" "${INCLUDE}")
msg "${formated_desc}"
)
done
}
config_remove()
{
local conf_file="$1"
if [ -e "${conf_file}" ]; then
rm -f "${conf_file}"
else
return 1
fi
}
################################################################################
# Components
################################################################################
component_is_compatible()
{
local target="$@"
[ -n "${target}" ] || return 0
local item
for item in ${target}
do
case "${DISTRIB}:${ARCH}:${SUITE}" in
${item})
return 0
;;
esac
done
return 1
}
component_is_exclude()
{
local component="$1"
[ -n "${component}" ] || return 1
for item in ${EXCLUDE_COMPONENTS}
do
case "${component}" in
${item}*)
return 0
;;
esac
done
return 1
}
component_depends()
{
local components="$@"
[ -n "${components}" ] || return 0
local component conf_file TARGET DEPENDS
for component in ${components}
do
component="${component%/}"
# check deadlocks
[ -z "${IGNORE_DEPENDS##* ${component} *}" ] && continue
IGNORE_DEPENDS="${IGNORE_DEPENDS}${component} "
# check component exist
conf_file="${INCLUDE_DIR}/${component}/deploy.conf"
[ -e "${conf_file}" ] || continue
# read component variables
eval $(grep -e '^TARGET=' -e '^DEPENDS=' "${conf_file}")
# check compatibility
if [ "${WITHOUT_CHECK}" != "true" ]; then
component_is_compatible ${TARGET} || continue
fi
if [ "${REVERSE_DEPENDS}" = "true" ]; then
# output
echo ${component}
# process depends
component_depends ${DEPENDS}
else
# process depends
component_depends ${DEPENDS}
# output
echo ${component}
fi
done
}
component_exec()
{
local components="$@"
if [ "${WITHOUT_DEPENDS}" != "true" ]; then
components=$(IGNORE_DEPENDS=" " component_depends ${components})
fi
[ -n "${components}" ] || return 1
(set -e
for COMPONENT in ${components}
do
COMPONENT_DIR="${INCLUDE_DIR}/${COMPONENT}"
[ -d "${COMPONENT_DIR}" ] || continue
unset NAME DESC TARGET PARAMS DEPENDS EXTENDS
TARGET='*:*:*'
# read config
. "${COMPONENT_DIR}/deploy.conf"
# default functions
do_install() { return 0; }
do_configure() { return 0; }
do_start() { return 0; }
do_stop() { return 0; }
do_status() { return 0; }
do_help() { return 0; }
# load extends
for component in ${EXTENDS} ${COMPONENT}
do
if [ -e "${INCLUDE_DIR}/${component}/deploy.sh" ]; then
. "${INCLUDE_DIR}/${component}/deploy.sh"
fi
done
# exclude components
component_is_exclude ${COMPONENT} && continue
# check parameters
[ "${WITHOUT_CHECK}" != "true" ] && params_check ${PARAMS}
# exec action
[ "${DEBUG_MODE}" = "true" ] && msg "## ${COMPONENT} : ${DO_ACTION}"
set +e
eval ${DO_ACTION} || exit 1
set -e
done
exit 0)
is_ok || return 1
}
component_list()
{
local components="$@"
local component output DESC
if [ -z "${components}" ]; then
components=$(cd "${INCLUDE_DIR}" && find . -type f -name "deploy.conf" | while read f
do
component="${f%/*}"
component="${component#*/}"
echo "${component}"
done)
fi
components=$(IGNORE_DEPENDS=" " component_depends ${components} | sort)
for component in $components
do
# output
DESC=''
eval $(grep '^DESC=' "${INCLUDE_DIR}/${component}/deploy.conf")
output=$(printf "%-30s %.49s\n" "${component}" "${DESC}")
msg "${output}"
done
}
component_dir() {
echo "${INCLUDE_DIR}/$1"
}
################################################################################
# Containers
################################################################################
container_mounted()
{
if [ "${METHOD}" = "chroot" ]; then
is_mounted "${CHROOT_DIR}"
else
return 0
fi
}
fs_check()
{
if is_mounted "${CHROOT_DIR}"; then
return 1
fi
local checkfs=$(which e2fsck)
if [ -z "${checkfs}" ]; then
return 1
fi
case "${TARGET_TYPE}" in
file|partition)
${checkfs} -p "${TARGET_PATH}" >/dev/null
return 0
;;
esac
return 1
}
mount_part()
{
case "$1" in
root)
msg -n "/ ... "
if ! is_mounted "${CHROOT_DIR}" ; then
[ -d "${CHROOT_DIR}" ] || mkdir -p "${CHROOT_DIR}"
local mnt_opts
[ -d "${TARGET_PATH}" ] && mnt_opts="bind" || mnt_opts="rw,relatime"
mount -o ${mnt_opts} "${TARGET_PATH}" "${CHROOT_DIR}" &&
mount -o remount,exec,suid,dev "${CHROOT_DIR}"
is_ok "fail" "done" || return 1
else
msg "skip"
fi
;;
proc)
msg -n "/proc ... "
local target="${CHROOT_DIR}/proc"
if ! is_mounted "${target}" ; then
[ -d "${target}" ] || mkdir -p "${target}"
mount -t proc proc "${target}"
is_ok "fail" "done"
else
msg "skip"
fi
;;
sys)
msg -n "/sys ... "
local target="${CHROOT_DIR}/sys"
if ! is_mounted "${target}" ; then
[ -d "${target}" ] || mkdir -p "${target}"
mount -t sysfs sys "${target}"
is_ok "fail" "done"
else
msg "skip"
fi
;;
dev)
msg -n "/dev ... "
local target="${CHROOT_DIR}/dev"
if ! is_mounted "${target}" ; then
[ -d "${target}" ] || mkdir -p "${target}"
mount -o bind /dev "${target}"
is_ok "fail" "done"
else
msg "skip"
fi
;;
shm)
msg -n "/dev/shm ... "
if ! is_mounted "/dev/shm" ; then
[ -d "/dev/shm" ] || mkdir -p /dev/shm
mount -o rw,nosuid,nodev,mode=1777 -t tmpfs tmpfs /dev/shm
fi
local target="${CHROOT_DIR}/dev/shm"
if ! is_mounted "${target}" ; then
mount -o bind /dev/shm "${target}"
is_ok "fail" "done"
else
msg "skip"
fi
;;
pts)
msg -n "/dev/pts ... "
if ! is_mounted "/dev/pts" ; then
[ -d "/dev/pts" ] || mkdir -p /dev/pts
mount -o rw,nosuid,noexec,gid=5,mode=620,ptmxmode=000 -t devpts devpts /dev/pts
fi
local target="${CHROOT_DIR}/dev/pts"
if ! is_mounted "${target}" ; then
mount -o bind /dev/pts "${target}"
is_ok "fail" "done"
else
msg "skip"
fi
;;
fd)
if [ ! -e "/dev/fd" -o ! -e "/dev/stdin" -o ! -e "/dev/stdout" -o ! -e "/dev/stderr" ]; then
msg -n "/dev/fd ... "
[ -e "/dev/fd" ] || ln -s /proc/self/fd /dev/
[ -e "/dev/stdin" ] || ln -s /proc/self/fd/0 /dev/stdin
[ -e "/dev/stdout" ] || ln -s /proc/self/fd/1 /dev/stdout
[ -e "/dev/stderr" ] || ln -s /proc/self/fd/2 /dev/stderr
is_ok "fail" "done"
fi
;;
tty)
if [ ! -e "/dev/tty0" ]; then
msg -n "/dev/tty ... "
ln -s /dev/null /dev/tty0
is_ok "fail" "done"
fi
;;
tun)
if [ ! -e "/dev/net/tun" ]; then
msg -n "/dev/net/tun ... "
[ -d "/dev/net" ] || mkdir -p /dev/net
mknod /dev/net/tun c 10 200
is_ok "fail" "done"
fi
;;
binfmt_misc)
multiarch_support || return 0
local binfmt_dir="/proc/sys/fs/binfmt_misc"
if ! is_mounted "${binfmt_dir}" ; then
msg -n "${binfmt_dir} ... "
mount -t binfmt_misc binfmt_misc "${binfmt_dir}"
is_ok "fail" "done"
fi
;;
esac
return 0
}
container_mount()
{
[ "${METHOD}" = "chroot" ] || return 0
if [ $# -eq 0 ]; then
container_mount root proc sys dev shm pts fd tty tun binfmt_misc
return $?
fi
params_check TARGET_PATH || return 1
msg -n "Checking file system ... "
fs_check
is_ok "skip" "done"
msg "Mounting the container: "
local item
for item in $*
do
mount_part ${item} || return 1
done
return 0
}
container_umount()
{
params_check TARGET_PATH || return 1
container_mounted || { msg "The container is not mounted." ; return 0; }
msg -n "Release resources ... "
local is_release=0
local lsof_full=$(lsof | awk '{print $1}' | grep -c '^lsof')
if [ "${lsof_full}" -eq 0 ]; then
local pids=$(lsof | grep "${CHROOT_DIR%/}" | awk '{print $1}' | uniq)
else
local pids=$(lsof | grep "${CHROOT_DIR%/}" | awk '{print $2}' | uniq)
fi
kill_pids ${pids}; is_ok "fail" "done"
msg "Unmounting partitions: "
local is_mnt=0
local mask
for mask in '.*' '*'
do
local parts=$(cat /proc/mounts | awk '{print $2}' | grep "^${CHROOT_DIR%/}/${mask}$" | sort -r)
local part
for part in ${parts}
do
local part_name=$(echo ${part} | sed "s|^${CHROOT_DIR%/}/*|/|g")
msg -n "${part_name} ... "
for i in 1 2 3
do
umount ${part} && break
sleep 1
done
is_ok "fail" "done"
is_mnt=1
done
done
[ "${is_mnt}" -eq 1 ]; is_ok " ...nothing mounted"
local loop=$(losetup -a | grep "${TARGET_PATH%/}" | awk -F: '{print $1}')
if [ -n "${loop}" ]; then
msg -n "Disassociating loop device ... "
losetup -d "${loop}"
is_ok "fail" "done"
fi
return 0
}
container_start()
{
container_mounted || { msg "The container is not mounted." ; return 1; }
DO_ACTION='do_start'
if [ $# -gt 0 ]; then
component_exec "$@"
else
component_exec "${INCLUDE}"
fi
}
container_stop()
{
container_mounted || { msg "The container is not mounted." ; return 1; }
DO_ACTION='do_stop'
if [ $# -gt 0 ]; then
component_exec "$@"
else
component_exec "${INCLUDE}"
fi
}
container_shell()
{
container_mounted || container_mount || return 1
DO_ACTION='do_start'
component_exec core
USER="root"
SHELL="$(user_shell $USER)"
HOME="$(user_home $USER)"
[ -n "${TERM}" ] || TERM="linux"
[ -n "${PS1}" ] || PS1="\u@\h:\w\\$ "
export USER SHELL HOME TERM PS1
if [ -e "${CHROOT_DIR}/etc/motd" ]; then
msg $(cat "${CHROOT_DIR}/etc/motd")
fi
chroot_exec "$@" 2>&1
return $?
}
rootfs_import()
{
local rootfs_file="$1"
[ -n "${rootfs_file}" ] || return 1
container_mounted || container_mount root || return 1
case "${rootfs_file}" in
*tar)
msg -n "Importing rootfs from tar archive ... "
if [ -e "${rootfs_file}" ]; then
tar xf "${rootfs_file}" -C "${CHROOT_DIR}"
elif [ -z "${rootfs_file##http*}" ]; then
wget -q -O - "${rootfs_file}" | tar x -C "${CHROOT_DIR}"
else
msg "fail"; return 1
fi
is_ok "fail" "done" || return 1
;;
*gz)
msg -n "Importing rootfs from tar.gz archive ... "
if [ -e "${rootfs_file}" ]; then
tar xzf "${rootfs_file}" -C "${CHROOT_DIR}"
elif [ -z "${rootfs_file##http*}" ]; then
wget -q -O - "${rootfs_file}" | tar xz -C "${CHROOT_DIR}"
else
msg "fail"; return 1
fi
is_ok "fail" "done" || return 1
;;
*bz2)
msg -n "Importing rootfs from tar.bz2 archive ... "
if [ -e "${rootfs_file}" ]; then
tar xjf "${rootfs_file}" -C "${CHROOT_DIR}"
elif [ -z "${rootfs_file##http*}" ]; then
wget -q -O - "${rootfs_file}" | tar xj -C "${CHROOT_DIR}"
else
msg "fail"; return 1
fi
is_ok "fail" "done" || return 1
;;
*xz)
msg -n "Importing rootfs from tar.xz archive ... "
if [ -e "${rootfs_file}" ]; then
tar xJf "${rootfs_file}" -C "${CHROOT_DIR}"
elif [ -z "${rootfs_file##http*}" ]; then
wget -q -O - "${rootfs_file}" | tar xJ -C "${CHROOT_DIR}"
else
msg "fail"; return 1
fi
is_ok "fail" "done" || return 1
;;
*)
msg "Incorrect filename, supported only tar, tar.gz, tar.bz2 or tar.xz archives."
return 1
;;
esac
return 0
}
rootfs_export()
{
local rootfs_file="$1"
[ -n "${rootfs_file}" ] || return 1
container_mounted || container_mount root || return 1
case "${rootfs_file}" in
*gz)
msg -n "Exporting rootfs as tar.gz archive ... "
tar czvf "${rootfs_file}" --exclude='./dev' --exclude='./sys' --exclude='./proc' -C "${CHROOT_DIR}" . >/dev/null
is_ok "fail" "done" || return 1
;;
*bz2)
msg -n "Exporting rootfs as tar.bz2 archive ... "
tar cjvf "${rootfs_file}" --exclude='./dev' --exclude='./sys' --exclude='./proc' -C "${CHROOT_DIR}" . >/dev/null
is_ok "fail" "done" || return 1
;;
*xz)
msg -n "Exporting rootfs as tar.xz archive ... "
tar cJvf "${rootfs_file}" --exclude='./dev' --exclude='./sys' --exclude='./proc' -C "${CHROOT_DIR}" . >/dev/null
is_ok "fail" "done" || return 1
;;
*)
msg "Incorrect filename, supported only gz, bz2 or xz archives."
return 1
;;
esac
}
container_status()
{
local model=$(which getprop >/dev/null && getprop ro.product.model)
if [ -n "${model}" ]; then
msg -n "Device: "
msg "${model}"
fi
local android=$(which getprop >/dev/null && getprop ro.build.version.release)
if [ -n "${android}" ]; then
msg -n "Android: "
msg "${android}"
fi
msg -n "Architecture: "
msg "$(uname -m)"
msg -n "Kernel: "
msg "$(uname -r)"
msg -n "Memory: "
local mem_total=$(grep ^MemTotal /proc/meminfo | awk '{print $2}')
let mem_total=${mem_total}/1024
local mem_free=$(grep ^MemFree /proc/meminfo | awk '{print $2}')
let mem_free=${mem_free}/1024
msg "${mem_free}/${mem_total} MB"
msg -n "Swap: "
local swap_total=$(grep ^SwapTotal /proc/meminfo | awk '{print $2}')
let swap_total=${swap_total}/1024
local swap_free=$(grep ^SwapFree /proc/meminfo | awk '{print $2}')
let swap_free=${swap_free}/1024
msg "${swap_free}/${swap_total} MB"
msg -n "SELinux: "
selinux_inactive && msg "inactive" || msg "active"
msg -n "Loop devices: "
loop_support && msg "yes" || msg "no"
msg -n "Support binfmt_misc: "
multiarch_support && msg "yes" || msg "no"
msg -n "Supported FS: "
local supported_fs=$(printf '%s ' $(grep -v nodev /proc/filesystems | sort))
msg "${supported_fs}"
msg -n "Installed system: "
local linux_version=$([ -r "${CHROOT_DIR}/etc/os-release" ] && . "${CHROOT_DIR}/etc/os-release"; [ -n "${PRETTY_NAME}" ] && echo "${PRETTY_NAME}" || echo "unknown")
msg "${linux_version}"
msg "Status of components: "
local DO_ACTION='do_status'
component_exec "${INCLUDE}"
msg "Mounted parts: "
local item
for item in $(grep "${CHROOT_DIR%/}" /proc/mounts | awk '{print $2}' | sed "s|${CHROOT_DIR%/}/*|/|g")
do
msg "* ${item}"
done
}
helper()
{
cat <<EOF
Linux Deploy ${VERSION}
(c) 2012-2019 Anton Skshidlevsky, GPLv3
USAGE:
${0##*/} [OPTIONS] COMMAND ...
OPTIONS:
-p NAME - configuration profile
-d - enable debug mode
-t - enable trace mode
COMMANDS:
config [...] [PARAMETERS] [NAME ...] - configuration management
- without parameters displays a list of configurations
-r - remove the current configuration
-i FILE - import the configuration
-x - dump of the current configuration
-l - list of dependencies for the specified or are connected components
-a - list of all components without check compatibility
deploy [...] [PARAMETERS] [-n NAME] [NAME ...] - install the distribution and included components
-m - mount the container before deployment
-i - install without configure
-c - configure without install
-n NAME - skip installation of this component
import FILE|URL - import a rootfs into the current container from archive (tgz, tbz2 or txz)
export FILE - export the current container as a rootfs archive (tgz, tbz2 or txz)
shell [-u USER] [COMMAND] - execute the specified command in the container, by default /bin/bash
-u USER - switch to the specified user
mount - mount the container
umount - unmount the container
start [-m] [NAME ...] - start all included or only specified components
-m - mount the container before start
stop [-u] [NAME ...] - stop all included or only specified components
-u - unmount the container after stop
status [NAME ...] - display the status of the container and components
help [NAME ...] - show this help or help of components
EOF
}
do_info()
{
cat <<EOF
Name: ${COMPONENT}
Description: ${DESC}
Target: ${TARGET}
Depends: ${DEPENDS}
Help:
EOF
}
################################################################################
umask 0022
unset LANG
if [ -z "${ENV_DIR}" ]; then
ENV_DIR=$(readlink "$0")
ENV_DIR="${ENV_DIR%/*}"
if [ -z "${ENV_DIR}" ]; then
ENV_DIR=$(realpath "${0%/*}")
fi
fi
if [ -e "${ENV_DIR}/cli.conf" ]; then
. "${ENV_DIR}/cli.conf"
fi