-
Notifications
You must be signed in to change notification settings - Fork 77
/
container-storage-setup.sh
executable file
·2765 lines (2286 loc) · 74.4 KB
/
container-storage-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
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
#--
# Copyright 2014-2017 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#++
# Purpose: This script sets up the storage for container runtimes.
# Author: Andy Grimm <[email protected]>
set -e
# container-storage-setup version information
_CSS_MAJOR_VERSION="0"
_CSS_MINOR_VERSION="11"
_CSS_SUBLEVEL="0"
_CSS_EXTRA_VERSION=""
_CSS_VERSION="${_CSS_MAJOR_VERSION}.${_CSS_MINOR_VERSION}.${_CSS_SUBLEVEL}"
[ -n "$_CSS_EXTRA_VERSION" ] && _CSS_VERSION="${_CSS_VERSION}-${_CSS_EXTRA_VERSION}"
# Locking related
_LOCKFD=300
_LOCKDIR="/var/lock/container-storage-setup"
_LOCKFILE="lock"
_CONFIG_NAME=""
_CONFIG_DIR="/var/lib/container-storage-setup/"
# Partition type related
_MAX_MBR_SIZE_BYTES="2199023255040"
# Metadata related stuff
_METADATA_VERSION=1
_INFILE_NAME="infile"
_OUTFILE_NAME="outfile"
_METAFILE_NAME="metadata"
_STATUSFILE_NAME="status"
# This section reads the config file $INPUTFILE
# Read man page for a description of currently supported options:
# 'man container-storage-setup'
_DOCKER_ROOT_LV_NAME="docker-root-lv"
_DOCKER_ROOT_DIR="/var/lib/docker"
_DOCKER_METADATA_DIR="/var/lib/docker"
DOCKER_ROOT_VOLUME_SIZE=40%FREE
_DOCKER_COMPAT_MODE=""
_STORAGE_IN_FILE=""
_STORAGE_OUT_FILE=""
_STORAGE_DRIVERS="devicemapper overlay overlay2"
# Command related variables
_COMMAND_LIST="create activate deactivate remove list export add-dev"
_COMMAND=""
_PIPE1=/run/css-$$-fifo1
_PIPE2=/run/css-$$-fifo2
_TEMPDIR=$(mktemp --tmpdir -d)
# Keeps track of resolved device paths
_DEVS_RESOLVED=""
# Will have currently configured storage options in ${_STORAGE_OUT_FILE}
_CURRENT_STORAGE_OPTIONS=""
_STORAGE_OPTIONS="STORAGE_OPTIONS"
# Keeps track of if we created a volume group or not.
_VG_CREATED=
get_docker_version() {
local version
# docker version command exits with error as daemon is not running at this
# point of time. So continue despite the error.
version=`docker version --format='{{.Client.Version}}' 2>/dev/null` || true
echo $version
}
get_deferred_removal_string() {
local version major minor
if ! version=$(get_docker_version);then
return 0
fi
[ -z "$version" ] && return 0
major=$(echo $version | cut -d "." -f1)
minor=$(echo $version | cut -d "." -f2)
[ -z "$major" ] && return 0
[ -z "$minor" ] && return 0
# docker 1.7 onwards supports deferred device removal. Enable it.
if [ $major -gt 1 ] || ([ $major -eq 1 ] && [ $minor -ge 7 ]);then
echo "--storage-opt dm.use_deferred_removal=true"
fi
}
get_deferred_deletion_string() {
local version major minor
if ! version=$(get_docker_version);then
return 0
fi
[ -z "$version" ] && return 0
major=$(echo $version | cut -d "." -f1)
minor=$(echo $version | cut -d "." -f2)
[ -z "$major" ] && return 0
[ -z "$minor" ] && return 0
if should_enable_deferred_deletion $major $minor; then
echo "--storage-opt dm.use_deferred_deletion=true"
fi
}
should_enable_deferred_deletion() {
# docker 1.9 onwards supports deferred device deletion. Enable it.
local major=$1
local minor=$2
if [ $major -lt 1 ] || ([ $major -eq 1 ] && [ $minor -lt 9 ]);then
return 1
fi
if platform_supports_deferred_deletion; then
return 0
fi
return 1
}
platform_supports_deferred_deletion() {
local deferred_deletion_supported=1
trap cleanup_pipes EXIT
local child_exec="$_SRCDIR/css-child-read-write.sh"
[ ! -x "$child_exec" ] && child_exec="/usr/share/container-storage-setup/css-child-read-write"
if [ ! -x "$child_exec" ];then
return 1
fi
mkfifo $_PIPE1
mkfifo $_PIPE2
unshare -m ${child_exec} $_PIPE1 $_PIPE2 "$_TEMPDIR" &
read -t 10 n <>$_PIPE1
if [ "$n" != "start" ];then
return 1
fi
rmdir $_TEMPDIR > /dev/null 2>&1
deferred_deletion_supported=$?
echo "finish" > $_PIPE2
return $deferred_deletion_supported
}
cleanup_pipes(){
rm -f $_PIPE1
rm -f $_PIPE2
rmdir $_TEMPDIR 2>/dev/null
}
extra_options_has_dm_fs() {
local option
for option in ${EXTRA_STORAGE_OPTIONS}; do
if grep -q "dm.fs=" <<< $option; then
return 0
fi
done
return 1
}
# Given a dm device name in /dev/mapper/ dir
# (ex. /dev/mapper/docker-vg--docker-pool), get associated volume group
get_dmdev_vg() {
local dmdev=${1##"/dev/mapper/"}
local vg
vg=`dmsetup splitname $dmdev --noheadings | cut -d ":" -f1`
echo $vg
}
# Wait for a device for certain time interval. If device is found 0 is
# returned otherwise 1.
wait_for_dev() {
local devpath=$1
local timeout=$DEVICE_WAIT_TIMEOUT
if [ -b "$devpath" ];then
Info "Device node $devpath exists."
return 0
fi
if [ -z "$DEVICE_WAIT_TIMEOUT" ] || [ "$DEVICE_WAIT_TIMEOUT" == "0" ];then
Info "Not waiting for device $devpath as DEVICE_WAIT_TIMEOUT=${DEVICE_WAIT_TIMEOUT}."
return 0
fi
while [ $timeout -gt 0 ]; do
Info "Waiting for device $devpath to be available. Wait time remaining is $timeout seconds"
if [ $timeout -le 5 ];then
sleep $timeout
else
sleep 5
fi
timeout=$((timeout-5))
if [ -b "$devpath" ]; then
Info "Device node $devpath exists."
return 0
fi
done
Info "Timed out waiting for device $devpath"
return 1
}
get_devicemapper_config_options() {
local storage_options
local dm_fs="--storage-opt dm.fs=xfs"
# docker expects device mapper device and not lvm device. Do the conversion.
eval $( lvs --nameprefixes --noheadings -o lv_name,kernel_major,kernel_minor $VG | while read line; do
eval $line
if [ "$LVM2_LV_NAME" = "$CONTAINER_THINPOOL" ]; then
echo _POOL_DEVICE_PATH=/dev/mapper/$( cat /sys/dev/block/${LVM2_LV_KERNEL_MAJOR}:${LVM2_LV_KERNEL_MINOR}/dm/name )
fi
done )
if extra_options_has_dm_fs; then
# dm.fs option defined in ${EXTRA_STORAGE_OPTIONS}
dm_fs=""
fi
storage_options="${_STORAGE_OPTIONS}=\"--storage-driver devicemapper ${dm_fs} --storage-opt dm.thinpooldev=$_POOL_DEVICE_PATH $(get_deferred_removal_string) $(get_deferred_deletion_string) ${EXTRA_STORAGE_OPTIONS}\""
echo $storage_options
}
get_config_options() {
if [ "$1" == "devicemapper" ]; then
get_devicemapper_config_options
return $?
fi
echo "${_STORAGE_OPTIONS}=\"--storage-driver $1 ${EXTRA_STORAGE_OPTIONS}\""
return 0
}
# Check if multiple overlay directories are supported and if overlay module
# itself is supported on the system.
can_mount_overlay() {
local dir="/run/container-storage-setup/"
local lower1="$dir/lower1"
local lower2="$dir/lower2"
local upper="$dir/upper"
local work="$dir/work"
local merged="$dir/merged"
local cmd
# Create multiple directories in /run
if ! mkdir -p $dir; then
Error "Failed to create directory $dir"
return 1
fi
cmd="mkdir -p $lower1 $lower2 $upper $work $merged"
if ! $cmd; then
Error "Failed to run $cmd"
return 1
fi
cmd="unshare -m mount -t overlay -o lowerdir=$lower1:$lower2,upperdir=$upper,workdir=$work none $merged"
if ! $cmd; then
Error "Failed to run $cmd"
return 1
fi
return 0
}
is_xfs_ftype_enabled() {
local mountroot=$1
local fstype
fstype=$(stat -f -c '%T' $mountroot)
[ "$fstype" != "xfs" ] && return 0
# For xfs, see https://bugzilla.redhat.com/show_bug.cgi?id=1288162#c8
if test "$(xfs_info $mountroot | grep -o 'ftype=[01]')" = "ftype=0"; then
return 1
fi
return 0
}
write_storage_config_file () {
local storage_driver=$1
local storage_out_file=$2
local storage_options
if [ -z "$storage_driver" ];then
touch "$storage_out_file"
return 0
fi
if ! storage_options=$(get_config_options $storage_driver); then
return 1
fi
cat <<EOF > ${storage_out_file}.tmp
$storage_options
EOF
mv -Z ${storage_out_file}.tmp ${storage_out_file}
}
convert_size_in_bytes() {
local size=$1 prefix suffix
# if it is all numeric, it is valid as by default it will be MiB.
if [[ $size =~ ^[[:digit:]]+$ ]]; then
echo $(($size*1024*1024))
return 0
fi
# supprt G, G[bB] or Gi[bB] inputs.
prefix=${size%[bBsSkKmMgGtTpPeE]i[bB]}
prefix=${prefix%[bBsSkKmMgGtTpPeE][bB]}
prefix=${prefix%[bBsSkKmMgGtTpPeE]}
# if prefix is not all numeric now, it is an error.
if ! [[ $prefix =~ ^[[:digit:]]+$ ]]; then
return 1
fi
suffix=${data_size#$prefix}
case $suffix in
b*|B*) echo $prefix;;
s*|S*) echo $(($prefix*512));;
k*|K*) echo $(($prefix*2**10));;
m*|M*) echo $(($prefix*2**20));;
g*|G*) echo $(($prefix*2**30));;
t*|T*) echo $(($prefix*2**40));;
p*|P*) echo $(($prefix*2**50));;
e*|E*) echo $(($prefix*2**60));;
*) return 1;;
esac
}
data_size_in_bytes() {
local data_size=$1
local bytes vg_size free_space percent
# -L compatible syntax
if [[ $DATA_SIZE != *%* ]]; then
bytes=`convert_size_in_bytes $data_size`
[ $? -ne 0 ] && return 1
# If integer overflow took place, value is too large to handle.
if [ $bytes -lt 0 ];then
Error "DATA_SIZE=$data_size is too large to handle."
return 1
fi
echo $bytes
return 0
fi
if [[ $DATA_SIZE == *%FREE ]];then
free_space=$(vgs --noheadings --nosuffix --units b -o vg_free $VG)
percent=${DATA_SIZE%\%FREE}
echo $((percent*free_space/100))
return 0
fi
if [[ $DATA_SIZE == *%VG ]];then
vg_size=$(vgs --noheadings --nosuffix --units b -o vg_size $VG)
percent=${DATA_SIZE%\%VG}
echo $((percent*vg_size/100))
fi
return 0
}
check_min_data_size_condition() {
local min_data_size_bytes data_size_bytes free_space
[ -z $MIN_DATA_SIZE ] && return 0
if ! check_numeric_size_syntax $MIN_DATA_SIZE; then
Fatal "MIN_DATA_SIZE value $MIN_DATA_SIZE is invalid."
fi
if ! min_data_size_bytes=$(convert_size_in_bytes $MIN_DATA_SIZE);then
Fatal "Failed to convert MIN_DATA_SIZE to bytes"
fi
# If integer overflow took place, value is too large to handle.
if [ $min_data_size_bytes -lt 0 ];then
Fatal "MIN_DATA_SIZE=$MIN_DATA_SIZE is too large to handle."
fi
free_space=$(vgs --noheadings --nosuffix --units b -o vg_free $VG)
if [ $free_space -lt $min_data_size_bytes ];then
Fatal "There is not enough free space in volume group $VG to create data volume of size MIN_DATA_SIZE=${MIN_DATA_SIZE}."
fi
if ! data_size_bytes=$(data_size_in_bytes $DATA_SIZE);then
Fatal "Failed to convert desired data size to bytes"
fi
if [ $data_size_bytes -lt $min_data_size_bytes ]; then
# Increasing DATA_SIZE to meet minimum data size requirements.
Info "DATA_SIZE=${DATA_SIZE} is smaller than MIN_DATA_SIZE=${MIN_DATA_SIZE}. Will create data volume of size specified by MIN_DATA_SIZE."
DATA_SIZE=$MIN_DATA_SIZE
fi
}
create_lvm_thin_pool () {
if [ -z "$_DEVS_RESOLVED" ] && [ -z "$_VG_EXISTS" ]; then
Fatal "Specified volume group $VG does not exist, and no devices were specified"
fi
if [ ! -n "$DATA_SIZE" ]; then
Fatal "DATA_SIZE not specified."
fi
if ! check_data_size_syntax $DATA_SIZE; then
Fatal "DATA_SIZE value $DATA_SIZE is invalid."
fi
check_min_data_size_condition
if [ -n "$POOL_META_SIZE" ]; then
_META_SIZE_ARG="$POOL_META_SIZE"
else
# Calculate size of metadata lv. Reserve 0.1% of the free space in the VG
# for docker metadata.
_VG_SIZE=$(vgs --noheadings --nosuffix --units s -o vg_size $VG)
_META_SIZE=$(( $_VG_SIZE / 1000 + 1 ))
if [ -z "$_META_SIZE" ];then
Fatal "Failed to calculate metadata volume size."
fi
_META_SIZE_ARG=${_META_SIZE}s
fi
if [ -n "$CHUNK_SIZE" ]; then
_CHUNK_SIZE_ARG="-c $CHUNK_SIZE"
fi
if [[ $DATA_SIZE == *%* ]]; then
_DATA_SIZE_ARG="-l $DATA_SIZE"
else
_DATA_SIZE_ARG="-L $DATA_SIZE"
fi
lvcreate -y --type thin-pool --zero n $_CHUNK_SIZE_ARG --poolmetadatasize $_META_SIZE_ARG $_DATA_SIZE_ARG -n $CONTAINER_THINPOOL $VG
}
get_configured_thin_pool() {
local options tpool opt
options=$_CURRENT_STORAGE_OPTIONS
[ -z "$options" ] && return 0
# This assumes that thin pool is specified as dm.thinpooldev=foo. There
# are no spaces in between.
for opt in $options; do
if [[ $opt =~ dm.thinpooldev* ]];then
tpool=${opt#*=}
echo "$tpool"
return 0
fi
done
}
check_docker_storage_metadata() {
local docker_devmapper_meta_dir="$_DOCKER_METADATA_DIR/devicemapper/metadata/"
[ ! -d "$docker_devmapper_meta_dir" ] && return 0
# Docker seems to be already using devicemapper storage driver. Error out.
Error "Docker has been previously configured for use with devicemapper graph driver. Not creating a new thin pool as existing docker metadata will fail to work with it. Manual cleanup is required before this will succeed."
Info "Docker state can be reset by stopping docker and by removing ${_DOCKER_METADATA_DIR} directory. This will destroy existing docker images and containers and all the docker metadata."
exit 1
}
systemd_escaped_filename () {
local escaped_path filename path=$1
escaped_path=$(echo ${path}|sed 's|-|\\x2d|g')
filename=$(echo ${escaped_path}.mount|sed 's|/|-|g' | cut -b 2-)
echo $filename
}
# Compatibility mode code
run_docker_compatibility_code() {
# Verify storage options set correctly in input files
check_storage_options
# Query and save current storage options
if ! _CURRENT_STORAGE_OPTIONS=$(get_current_storage_options); then
return 1
fi
determine_rootfs_pvs_vg
if [ $_RESET -eq 1 ]; then
reset_storage_compat
exit 0
fi
partition_disks_create_vg
grow_root_pvs
# NB: We are growing root here first, because when root and docker share a
# disk, we'll default to giving some portion of remaining space to docker.
# Do this operation only if root is on a logical volume.
[ -n "$_ROOT_VG" ] && grow_root_lv_fs
if is_old_data_meta_mode; then
Fatal "Old mode of passing data and metadata logical volumes to docker is not supported. Exiting."
fi
setup_storage_compat
}
#
# In the past we created a systemd mount target file, we no longer
# use it, but if one pre-existed we still need to handle it.
#
remove_systemd_mount_target () {
local mp=$1
local filename=$(systemd_escaped_filename $mp)
if [ -f /etc/systemd/system/$filename ]; then
if [ -x /usr/bin/systemctl ];then
systemctl disable $filename >/dev/null 2>&1
systemctl stop $filename >/dev/null 2>&1
systemctl daemon-reload
fi
rm -f /etc/systemd/system/$filename >/dev/null 2>&1
fi
}
# This is used in compatibility mode.
reset_extra_volume_compat () {
local mp filename
local lv_name=$1
local mount_dir=$2
local vg=$3
if extra_volume_exists $lv_name $vg; then
mp=$(extra_lv_mountpoint $vg $lv_name $mount_dir)
if [ -n "$mp" ];then
if ! umount $mp >/dev/null 2>&1; then
Fatal "Failed to unmount $mp"
fi
fi
lvchange -an $vg/${lv_name}
lvremove $vg/${lv_name}
else
return 0
fi
# If the user has manually unmounted mount directory, mountpoint (mp)
# will be empty. Extract ${mp} from $(mount_dir) in that case.
if [ -z "$mp" ];then
mp=${mount_dir}
fi
remove_systemd_mount_target $mp
}
reset_lvm_thin_pool () {
local thinpool_name=$1
local vg=$2
if lvm_pool_exists $thinpool_name $vg; then
lvchange -an $vg/${thinpool_name}
lvremove $vg/${thinpool_name}
fi
}
# Used in compatibility mode. Determine if already configured thin pool
# is managed by container-storage-setup or not. Returns 0 if tpool is
# managed otherwise 1.
is_managed_tpool_compat() {
local tpool=$1
local thinpool_name=${CONTAINER_THINPOOL}
local escaped_pool_lv_name=`echo $thinpool_name | sed 's/-/--/g'`
# css generated thin pool device name starts with /dev/mapper/ and
# ends with $thinpool_name
[[ "$tpool" == /dev/mapper/*${escaped_pool_lv_name} ]] && return 0
return 1
}
# This is used in comatibility mode.
bringup_existing_thin_pool_compat() {
local tpool=$1
# css generated thin pool device name starts with /dev/mapper/ and
# ends with $thinpool_name
if ! is_managed_tpool_compat "$tpool";then
Fatal "Thin pool ${tpool} does not seem to be managed by container-storage-setup. Exiting."
fi
if ! wait_for_dev "$tpool"; then
Fatal "Already configured thin pool $tpool is not available. If thin pool exists and is taking longer to activate, set DEVICE_WAIT_TIMEOUT to a higher value and retry. If thin pool does not exist any more, remove ${_STORAGE_OUT_FILE} and retry"
fi
}
# This is used in comatibility mode. Returns 0 if thin pool is already
# configured and wait could find the device. Returns 1 if thin pool is
# not configured and probably needs to be created. Terminates script
# on fatal errors.
check_existing_thinpool_compat() {
local tpool
# Check if a thin pool is already configured in /etc/sysconfig/docker-storage
# If yes, wait for that thin pool to come up.
tpool=`get_configured_thin_pool`
[ -z "$tpool" ] && return 1
Info "Found an already configured thin pool $tpool in ${_STORAGE_OUT_FILE}"
bringup_existing_thin_pool_compat "$tpool"
return
}
# This is used in comatibility mode.
setup_lvm_thin_pool_compat () {
local thinpool_name=${CONTAINER_THINPOOL}
if check_existing_thinpool_compat; then
process_auto_pool_extenion ${VG} ${thinpool_name}
# We found existing thin pool and waited for it and processed auto
# pool extension changes. There should not be any need to process
# further
return
fi
# At this point of time, a volume group should exist for lvm thin pool
# operations to succeed. Make that check and fail if that's not the case.
if ! vg_exists "$VG";then
Fatal "No valid volume group found. Exiting."
else
_VG_EXISTS=1
fi
if ! lvm_pool_exists $thinpool_name $VG; then
[ -n "$_DOCKER_COMPAT_MODE" ] && check_docker_storage_metadata
create_lvm_thin_pool
[ -n "$_STORAGE_OUT_FILE" ] && write_storage_config_file $STORAGE_DRIVER "$_STORAGE_OUT_FILE"
else
# At this point /etc/sysconfig/docker-storage file should exist. If user
# deleted this file accidently without deleting thin pool, recreate it.
if [ -n "$_STORAGE_OUT_FILE" -a ! -f "${_STORAGE_OUT_FILE}" ];then
Info "${_STORAGE_OUT_FILE} file is missing. Recreating it."
write_storage_config_file $STORAGE_DRIVER "$_STORAGE_OUT_FILE"
fi
fi
process_auto_pool_extenion ${VG} ${thinpool_name}
}
lvm_pool_exists() {
local lv_data
local lvname lv lvsize
local thinpool_name=$1
local vg=$2
if [ -z "$thinpool_name" ]; then
Fatal "Thin pool name must be specified."
fi
lv_data=$( lvs --noheadings -o lv_name,lv_attr --separator , $vg | sed -e 's/^ *//')
SAVEDIFS=$IFS
for lv in $lv_data; do
IFS=,
read lvname lvattr <<< "$lv"
# pool logical volume has "t" as first character in its attributes
if [ "$lvname" == "$thinpool_name" ] && [[ $lvattr == t* ]]; then
IFS=$SAVEDIFS
return 0
fi
done
IFS=$SAVEDIFS
return 1
}
# If a ${_STORAGE_OUT_FILE} file is present and if it contains
# dm.datadev or dm.metadatadev entries, that means we have used old mode
# in the past.
is_old_data_meta_mode() {
if [ ! -f "${_STORAGE_OUT_FILE}" ];then
return 1
fi
if ! grep -e "^${_STORAGE_OPTIONS}=.*dm\.datadev" -e "^${_STORAGE_OPTIONS}=.*dm\.metadatadev" ${_STORAGE_OUT_FILE} > /dev/null 2>&1;then
return 1
fi
return 0
}
grow_root_pvs() {
# If root is not in a volume group, then there are no root pvs and nothing
# to do.
[ -z "$_ROOT_PVS" ] && return 0
# Grow root pvs only if user asked for it through config file.
[ "$GROWPART" != "true" ] && return
if [ ! -x "/usr/bin/growpart" ];then
Error "GROWPART=true is specified and /usr/bin/growpart executable is not available. Install /usr/bin/growpart and try again."
return 1
fi
# Note that growpart is only variable here because we may someday support
# using separate partitions on the same disk. Today we fail early in that
# case. Also note that the way we are doing this, it should support LVM
# RAID for the root device. In the mirrored or striped case, we are growing
# partitions on all disks, so as long as they match, growing the LV should
# also work.
for pv in $_ROOT_PVS; do
if ! test -b $pv; then
Error "Not a block device: $pv"
fi
local major_hex minor_hex major_minor
local devpath partition parent_path parent_device
major_hex=$(stat -c '%t' $pv)
minor_hex=$(stat -c '%T' $pv)
major_minor=$((0x${major_hex})):$((0x${minor_hex}))
devpath=$(realpath /sys/dev/block/$major_minor)
partition=$(cat $devpath/partition)
parent_path=$(dirname $devpath)
parent_device=/dev/$(basename ${parent_path})
# TODO: Remove the || true here
growpart ${parent_device} ${partition} || true
pvresize $pv
done
}
grow_root_lv_fs() {
if [ -n "$ROOT_SIZE" ]; then
# Allow user to pass in an argument that could be provided
# to -L (like 10G or +5G) or an argument that could be passed
# to -l (like 80%FREE). Switch based on if '%' is in string.
local root_size_arg
if [[ $ROOT_SIZE =~ % ]]; then
root_size_arg="-l $ROOT_SIZE"
else
root_size_arg="-L $ROOT_SIZE"
fi
# TODO: Error checking if specified size is <= current size
lvextend -r $root_size_arg $_ROOT_DEV || true
fi
}
# Determines if a device is already added in a volume group as pv. Returns
# 0 on success.
is_dev_part_of_vg() {
local dev=$1
local vg=$2
if ! pv_name=$(pvs --noheadings -o pv_name -S pv_name=$dev,vg_name=$vg); then
Fatal "Error running command pvs. Exiting."
fi
[ -z "$pv_name" ] && return 1
pv_name=`echo $pv_name | tr -d '[ ]'`
[ "$pv_name" == "$dev" ] && return 0
return 1
}
is_block_dev_partition() {
local bdev=$1 devparent
if ! disktype=$(lsblk -n --nodeps --output type ${bdev}); then
Fatal "Failed to run lsblk on device $bdev"
fi
if [ "$disktype" == "part" ];then
return 0
fi
if [ "$disktype" == "mpath" ];then
return 1
fi
# For loop device partitions, lsblk reports type as "loop" and not "part".
# So check if device has a parent in the tree and if it does, there are high
# chances it is partition (except the case of lvm volumes)
if ! devparent=$(lsblk -npls -o NAME ${bdev}|tail -n +2); then
Fatal "Failed to run lsblk on device $bdev"
fi
if [ -n "$devparent" ];then
return 0
fi
return 1
}
check_wipe_block_dev_sig() {
local bdev=$1
local sig
if ! sig=$(wipefs -p $bdev); then
Fatal "Failed to check signatures on device $bdev"
fi
[ "$sig" == "" ] && return 0
if [ "$WIPE_SIGNATURES" == "true" ];then
Info "Wipe Signatures is set to true. Any signatures on $bdev will be wiped."
if ! wipefs -a $bdev; then
Fatal "Failed to wipe signatures on device $bdev"
fi
return 0
fi
while IFS=, read offset uuid label type; do
[ "$offset" == "# offset" ] && continue
Fatal "Found $type signature on device ${bdev} at offset ${offset}. Wipe signatures using wipefs or use WIPE_SIGNATURES=true and retry."
done <<< "$sig"
}
# This is used in compatibility mode
canonicalize_block_devs_compat() {
local devs=$1 dev
local devs_abs dev_abs
local dest_dev
for dev in ${devs}; do
# If the device name is a symlink, follow it and use the target
if [ -h "$dev" ];then
if ! dest_dev=$(readlink -e $dev);then
Fatal "Failed to resolve symbolic link $dev"
fi
dev=$dest_dev
fi
# Looks like we allowed just device name (sda) as valid input. In
# such cases /dev/$dev should be a valid block device.
dev_abs=$dev
[ ! -b "$dev" ] && dev_abs="/dev/$dev"
[ ! -b "$dev_abs" ] && Fatal "$dev_abs is not a valid block device."
if is_block_dev_partition ${dev_abs}; then
Fatal "Partition specification unsupported at this time."
fi
devs_abs="$devs_abs $dev_abs"
done
# Return list of devices to caller.
echo "$devs_abs"
}
# This is used in config creation mode
canonicalize_block_devs_generic() {
local devs=$1 dev
local devs_resolved resolved_device
for dev in ${devs}; do
if ! resolved_device=$(realpath -e $dev);then
Fatal "Failed to resolve path for device ${dev}"
fi
[ ! -b "$resolved_device" ] && Fatal "$resolved_device is not a valid block device."
if is_block_dev_partition ${resolved_device}; then
Fatal "Partition specification unsupported at this time."
fi
if [ -n "$devs_resolved" ]; then
devs_resolved="$devs_resolved $resolved_device"
else
devs_resolved="$resolved_device"
fi
done
# Return list of devices to caller.
echo "$devs_resolved"
}
# Make sure passed in devices are valid block devies. Also make sure they
# are not partitions. Names which are of the form "sdb", convert them to
# their absolute path for processing in rest of the script.
canonicalize_block_devs() {
local input_dev_list="$1"
local devs_list
if [ "$_DOCKER_COMPAT_MODE" == "1" ];then
devs_list=$(canonicalize_block_devs_compat "$input_dev_list") || return 1
else
devs_list=$(canonicalize_block_devs_generic "$input_dev_list") || return 1
fi
echo $devs_list
}
# Scans all the disks listed in DEVS= and returns the disks which are not
# already part of volume group and are new and require further processing.
scan_disks() {
local disk_list="$1"
local vg=$2
local wipe_signatures=$3
local new_disks=""
for dev in $disk_list; do
local part=$(dev_query_first_child $dev)
if [ -n "$part" ] && is_dev_part_of_vg ${part} $vg; then
Info "Device ${dev} is already partitioned and is part of volume group $VG"
continue
fi
# If signatures are being overridden, then simply return the disk as new
# disk. Even if it is partitioned, partition signatures will be wiped.
if [ "$wipe_signatures" == "true" ];then
new_disks="$new_disks $dev"
continue
fi
# If device does not have partitions, it is a new disk requiring processing.
if [[ -z "$part" ]]; then
new_disks="$dev $new_disks"
continue
fi
Fatal "Device $dev is already partitioned and cannot be added to volume group $vg"
done
echo $new_disks
}
determine_partition_type() {
local dev="$1" size_bytes part_type
if ! size_bytes=$(blockdev --getsize64 "$dev"); then
Fatal "Failed to determine size of disk $dev"
fi
if [ $size_bytes -gt $_MAX_MBR_SIZE_BYTES ];then
part_type="gpt"
else
part_type="dos"
fi
echo $part_type
}
create_partition_sfdisk(){
local dev="$1" part_type="$2" size part_label
# Use a single partition of a whole device
# TODO:
# * Consider gpt, or unpartitioned volumes
# * Error handling when partition(s) already exist
# * Deal with loop/nbd device names. See growpart code
if [ "$part_type" == "gpt" ];then
# Linux LVM GUID for GPT. Taken from Wiki.
part_label="E6D6D379-F507-44C2-A23C-238F2A3DF928"
# Create as big a partition as possible.
size=""
else
part_label="8e"
size=$(( $( awk "\$4 ~ /"$( basename $dev )"/ { print \$3 }" /proc/partitions ) * 2 - 2048 ))
fi
cat <<EOF | sfdisk $dev
unit: sectors
label: $part_type
2048,${size},$part_label
EOF
}
create_partition_parted(){
local dev="$1"
local part_type="$2"
if [ "$part_type" == "gpt" ];then
parted $dev --script mklabel gpt mkpart "container-partition" 0% 100% set 1 lvm on
else
parted $dev --script mklabel msdos mkpart primary 0% 100% set 1 lvm on
fi
}
create_partition() {
local dev="$1" part part_type
part_type=`determine_partition_type "$dev"`
if [ -x "/usr/sbin/parted" ]; then
create_partition_parted $dev "$part_type"
else
create_partition_sfdisk $dev "$part_type"
fi