-
Notifications
You must be signed in to change notification settings - Fork 4
/
scan.go
979 lines (895 loc) · 31.5 KB
/
scan.go
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
package fsextender
import (
"bytes"
"errors"
"fmt"
"github.com/rekby/fsextender/Godeps/_workspace/src/github.com/rekby/gpt"
"github.com/rekby/fsextender/Godeps/_workspace/src/github.com/rekby/mbr"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"unicode"
)
//go:generate stringer -type storageItemType
type storageItemType int
// Max depth of storage. Used for infinite loop detection.
// Максимальная глубина стека устройств. В настроящий момент используется как простой определитель циклов
const max_STORAGE_DEEP = 100
// Minimum size of created partition. For avoid create new partition with few KB of space.
// Минимальный размер свободного места для создания нового раздела
const min_SIZE_NEW_PARTITION = 100 * 1024 * 1024
// Count of extents for PV metadata (for calculations).
// Количество блоков LVM PV, резервируемых под метаданные (при расчетах).
const lvm_PV_METADATA_RESERVED = 2
const (
type_UNKNOWN storageItemType = iota
type_FS
type_DISK
type_LVM_GROUP
type_LVM_PV
// Unused LVM PV
// Неиспользуемый LVM PV
type_LVM_PV_ADD
// new LVM PV
// Новый физический раздел LVM
type_LVM_PV_NEW
type_LVM_LV
type_PARTITION
type_PARTITION_NEW
type_SKIP
type_LAST // Doesn't use in work - for tests only.
)
type storageItem struct {
Type storageItemType // Storage type. Тип устройства
Path string // Path to device or name of device (for LVM group). Путь к устройству или имя (например для LVM)
Child int // Index of storage, what can help extend the item. Индекс устройства, на который будет влиять расширение этого устройства
Size uint64
FreeSpace uint64 // Free space of item, without sum of unde items. For example - size for PV extend to size of partition
// or free space in LVM Volume group.
// Максимальный объем, который может предоставить устройство, без учета роста нижележащих устройст
// Например расширение PV до размера раздела или расширение раздела до размера диска, свободное место в LVM Group и т.п.
FSType string // Type of file system (for type type_FS) тип файловой системы (для типа type_FS)
Partition partition // For types type_PARTITION and type_PARTITION_NEW. Описание раздела диска - для типов (type_PARTITION, type_PARTITION_NEW)
LVMExtentSize uint64 // Extent size for type_LVM_GROUP, type_LVM_PV, type_LVM_PV_ADD, type_LVM_PV_NEW. Размер экстента для типа type_LVM_GROUP, type_LVM_PV, type_LVM_PV_ADD, type_LVM_PV_NEW
SkipReason string
OldType storageItemType // Type of item before skip
}
func (this storageItem) String() string {
base := fmt.Sprintf("[Type: %v, Path: %v, Size: %v (+%v), Child: %v",
this.Type, this.Path, formatSize(this.Size), formatSize(this.FreeSpace), this.Child)
switch this.Type {
case type_FS:
base += ", FS: " + this.FSType
case type_PARTITION, type_PARTITION_NEW:
base += ", PartNum=" + strconv.FormatUint(uint64(this.Partition.Number), 10)
case type_LVM_GROUP, type_LVM_PV, type_LVM_PV_ADD, type_LVM_PV_NEW:
base += ", ExtentSize: " + formatSize(this.LVMExtentSize)
case type_SKIP:
base += ", Reason: " + this.SkipReason
}
return base + "]"
}
type diskInfo struct {
Path string
PartTable string // msdos/gpt
Size uint64 // Bytes
Major int
Minor int
SectorSizeLogical uint64 // Logical size of sector - for operation with partition table (in bytes). Логический размер сектора диска, в байтах
Partitions []partition
MaxPartitionCount uint32
}
type partition struct {
Disk *diskInfo
Path string
Number uint32 // Partition numbers start start from 1. Value 0 mean free space
FirstByte uint64
LastByte uint64
}
type partitionSortByFirstByte []partition
// implement sort.Interface
func (arr partitionSortByFirstByte) Len() int {
return len(arr)
}
func (arr partitionSortByFirstByte) Less(i, j int) bool {
return arr[i].FirstByte < arr[j].FirstByte
}
func (arr partitionSortByFirstByte) Swap(i, j int) {
tmpSwap := arr[i]
arr[i] = arr[j]
arr[j] = tmpSwap
}
func (p partition) Size() uint64 {
return p.LastByte - p.FirstByte + 1
}
func (p partition) IsFreeSpace() bool {
return p.Number == 0
}
func (p partition) makePath() string {
// Drive path ends with number, for example /dev/loop0
if len(p.Disk.Path) > 0 {
last := p.Disk.Path[len(p.Disk.Path)-1]
if last >= '0' && last <= '9' {
return p.Disk.Path + "p" + strconv.FormatUint(uint64(p.Number), 10)
}
}
return p.Disk.Path + strconv.FormatUint(uint64(p.Number), 10)
}
type lvmPV struct {
Path string
VolumeGroup string
Size uint64
}
var majorMinorDeviceTypeCache = make(map[[2]int]storageItem)
func blkid(path string) string {
buf := &bytes.Buffer{}
cmd := exec.Command("blkid", path)
cmd.Stdout = buf
cmd.Run()
out := buf.Bytes()
start := bytes.Index(out, []byte(`TYPE="`))
if start == -1 {
return ""
}
typeBytes := out[start+len(`TYPE="`):]
end := bytes.Index(typeBytes, []byte(`"`))
if end == -1 {
return ""
}
return string(typeBytes[:end])
}
var diskNewPartitionNumLastGeneratedNum = make(map[[2]int]uint32)
func diskNewPartitionNum(disk diskInfo) uint32 {
mm := [2]int{disk.Major, disk.Minor}
start := diskNewPartitionNumLastGeneratedNum[mm]
partNumLoop:
for partNum := start + 1; true; partNum++ {
for _, part := range disk.Partitions {
// Check if exist partition with the number
// Проверяем есть ли разделы с таким номером
if part.Number == partNum {
continue partNumLoop
}
}
diskNewPartitionNumLastGeneratedNum[mm] = partNum
return partNum
}
return 0
}
func extendScanWays(startPoint string) (storage []storageItem, err error) {
startPoint = filepath.Clean(startPoint)
startPoint, err = filepath.Abs(startPoint)
if err != nil {
log.Println("Can't abs of startpoint: ", startPoint)
return
}
startPoint, err = readLink(startPoint)
if err != nil {
log.Println("Can't readlink of startpoint: ", startPoint)
return
}
scanLVM()
// Check if startPoint is mount point of file system. If yes - find mounted device. Take last mount line.
// проверяем является ли startPoint точкой монтирования. Если да - находим смонтированное устройство.
// Если подходящих строк монтирования несколько - берём последнюю строку.
mountsBytes, err := ioutil.ReadFile("/proc/mounts")
if err != nil {
return
}
mountFrom := ""
for _, lineBytes := range bytes.Split(mountsBytes, []byte("\n")) {
parts := bytes.Split(lineBytes, []byte(" "))
if len(parts) < 3 {
// empty line
continue
}
from := parts[0]
to := parts[1]
if string(to) == startPoint {
mountFrom = string(from)
}
}
if mountFrom != "" {
startPoint = mountFrom
}
startPoint, err = readLink(startPoint)
if err != nil {
log.Println("Can't read symlink for mountpoint: ", startPoint, err)
return
}
storage = make([]storageItem, 0)
toScan := []storageItem{storageItem{Path: startPoint, Child: -1}}
toScanLoop:
for len(toScan) > 0 {
if len(storage) > max_STORAGE_DEEP {
return storage, errors.New("Struct is cicle or very large")
}
// pop item
item := toScan[len(toScan)-1]
toScan = toScan[:len(toScan)-1]
switch item.Type {
case type_UNKNOWN:
blk := blkid(item.Path)
major, minor := getMajorMinor(item.Path)
switch {
case blk == "ext2", blk == "ext3", blk == "ext4", blk == "xfs":
item.Type = type_FS
item.FSType = blk
case getTypeByMajorMinor(major, minor) != type_UNKNOWN:
item.Type = getTypeByMajorMinor(major, minor)
default:
// Skip unknown devices
// не получилось понять что за устройство - пропускаем
log.Printf("Can't detect device type. Path: '%v' Blk: '%v', major: %v, minor: %v", item.Path, blk, major, minor)
continue toScanLoop
}
// Scan once more with right type of device
// тип устройства определился - будем сканировать подробнее на следующем проходе
toScan = append(toScan, item)
case type_FS:
switch item.FSType {
case "ext2", "ext3", "ext4":
item.Size, err = fsGetSizeExt(item.Path)
if err != nil {
log.Printf("Can't get size of filesystem: %v (%v). Skip it.\n", item.Path, err)
continue toScanLoop
}
case "xfs":
item.Size, err = fsGetSizeXFS(item.Path)
if err != nil {
log.Printf("Can't get size of filesystem: %v (%v). Skip it.\n", item.Path, err)
continue toScanLoop
}
default:
log.Printf("I don't khow method to detect size of filesystem %v (%v). Skip it.", item.Path, item.FSType)
continue toScanLoop
}
storage = append(storage, item)
major, minor := getMajorMinor(item.Path)
underLevelType := getTypeByMajorMinor(major, minor)
if underLevelType != type_UNKNOWN {
newItem := storageItem{
Path: item.Path,
Type: underLevelType,
Child: len(storage) - 1,
}
toScan = append(toScan, newItem)
}
case type_PARTITION:
diskPath, partNumber, err := extractPartNumber(item.Path)
if err != nil {
log.Println(err.Error())
continue toScanLoop
}
disk, err := readDiskInfo(diskPath)
if err != nil {
log.Println("Error while scan partition. Skip it: ", item.Path, diskPath, err)
continue toScanLoop
}
for i, partition := range disk.Partitions {
if partition.Number != partNumber {
continue
}
item.Size = partition.Size()
item.Partition = partition
// Check if can extend partition.
// Если можем расшириться за счет свободного места между разделами или до конца диска
if i+1 < len(disk.Partitions) && disk.Partitions[i+1].IsFreeSpace() {
freeSpace := disk.Partitions[i+1]
item.FreeSpace = uint64(freeSpace.LastByte - partition.LastByte)
}
}
// If partition has not fund, example: extended partition in mbr
// Если раздел не найден, например расширенный раздел mbr
if item.Partition.Number == 0 {
item.Type = type_SKIP
if disk.PartTable == "msdos" && partNumber > 4 {
item.SkipReason = "Extended partition on mbr"
} else {
item.SkipReason = "Coud not found partition in partition table"
}
}
storage = append(storage, item)
// LVM_PV free space detection
if item.Child != -1 && storage[item.Child].Type == type_LVM_PV {
child := &storage[item.Child]
newSize := lvmPVCalcSize(item.Size, child.LVMExtentSize)
if newSize > child.Size {
child.FreeSpace = newSize - child.Size
}
}
case type_DISK:
storage = append(storage, item)
continue
case type_LVM_LV:
// Normalize path to LVM LV
// Если был передан полный путь к LVM - заменяем его описанием из кеша, заполненного при сканировании LVM
if major, minor := getMajorMinor(item.Path); major != 0 {
item.Path = majorMinorDeviceTypeCache[[2]int{major, minor}].Path
}
item.Size = lvmLVGetSize(item.Path)
storage = append(storage, item)
lvm_group := storageItem{
Type: type_LVM_GROUP,
Path: item.Path[:strings.Index(item.Path, "/")],
Child: len(storage) - 1,
}
toScan = append(toScan, lvm_group)
case type_LVM_PV, type_LVM_PV_ADD:
item.Size = lvmPVGetSize(item.Path)
storage = append(storage, item)
major, minor := getMajorMinor(item.Path)
parent := storageItem{}
parent.Path = item.Path
parent.Child = len(storage) - 1
parent.Type = getTypeByMajorMinor(major, minor)
if parent.Type != type_UNKNOWN {
toScan = append(toScan, parent)
}
case type_LVM_GROUP:
item.Size, item.FreeSpace, item.LVMExtentSize = lvmVGGetSize(item.Path)
storage = append(storage, item)
lvmGroupIndex := len(storage) - 1
// Find my and free pvs
for _, pv := range getLvmPV() {
if pv.VolumeGroup == "" {
// Can use free LVM PV
// Незанятые PV, можно использовать
parent := storageItem{Path: pv.Path, Type: type_LVM_PV_ADD, Child: len(storage) - 1, LVMExtentSize: item.LVMExtentSize}
// Calc usable PV size
// для свободных pv система выдает размер равный размеру раздела, так что испольузем расчетный размер
parent.Size = lvmPVCalcSize(pv.Size, item.LVMExtentSize)
parent.FreeSpace = parent.Size
toScan = append(toScan, parent)
} else if pv.VolumeGroup == item.Path {
// LVM PV in the LV group
// PV, входящие в эту группу
parent := storageItem{Path: pv.Path, Size: pv.Size, Type: type_LVM_PV, Child: len(storage) - 1, LVMExtentSize: item.LVMExtentSize}
toScan = append(toScan, parent)
} else {
// nothing
}
}
// Find free space for create new partition
for _, part := range getNewPartitions() {
pvCreate := storageItem{Child: lvmGroupIndex, Path: part.Path, Type: type_LVM_PV_NEW, LVMExtentSize: item.LVMExtentSize}
storage = append(storage, pvCreate)
partCreate := storageItem{Child: len(storage) - 1, Path: part.Path, Type: type_PARTITION_NEW, FreeSpace: part.Size(),
Partition: part}
storage = append(storage, partCreate)
}
}
}
// Fix free space for extend filesystem. We can't detect it while scan - on the step the program doesn't know partition/LVM size
// Поправить свободной место файловой системы - оно не может быть определено просто во время, т.к. на этом шаге программа еще не знает размера нижележащего раздела/LVM
for _, item := range storage {
if item.Child != -1 && storage[item.Child].Type == type_FS {
fs := &storage[item.Child]
switch {
case item.Size > fs.Size:
fs.FreeSpace += item.Size - fs.Size
case item.Size < fs.Size:
log.Printf("WARNING: Filesystem size (%v) more then underliing layer (%v, %v)\n", fs.Path, item.Type, item.Path)
case item.Size == fs.Size:
// do nothing
}
}
}
return storage, err
}
func extractPartNumber(path string) (diskPath string, partNumber uint32, err error) {
runePath := []rune(path)
if !unicode.IsDigit(runePath[len(runePath)-1]) {
return "", 0, fmt.Errorf("Can't extract part number from: %v", path)
}
startPartNumber := len(runePath) - 1
for startPartNumber > 0 && unicode.IsDigit(runePath[startPartNumber-1]) {
startPartNumber--
}
diskPathRunes := runePath[:startPartNumber]
// If path have format /dev/loop0p1
if len(diskPathRunes) > 1 && diskPathRunes[len(diskPathRunes)-1] == 'p' && unicode.IsDigit(diskPathRunes[len(diskPathRunes)-2]) {
diskPathRunes = diskPathRunes[:len(diskPathRunes)-1]
}
diskPath = string(diskPathRunes)
partNumber64, _ := parseUint(string(runePath[startPartNumber:]))
return diskPath, uint32(partNumber64), nil
}
func fsGetSizeExt(path string) (size uint64, err error) {
var blockCount, blockSize uint64
var blockCountPresent, blockSizePresent bool
res := cmdTrimLines("tune2fs", "-l", path)
for _, line := range res {
if strings.HasPrefix(line, "Block size:") {
blockSizePresent = true
blockSizeString := strings.TrimSpace(line[len("Block size:"):])
blockSize, err = parseUint(blockSizeString)
if err != nil {
return
}
}
if strings.HasPrefix(line, "Block count:") {
blockCountString := strings.TrimSpace(line[len("Block count:"):])
blockCountPresent = true
blockCount, err = parseUint(blockCountString)
if err != nil {
return
}
}
}
if !blockCountPresent || !blockSizePresent {
return 0, fmt.Errorf("Can't get filesistem size: %v", path)
}
size = blockCount * blockSize
return
}
/*
path - пусть к блочному устройству, на котором расположена xfs
*/
func fsGetSizeXFS(path string) (size uint64, err error) {
var tmpMountPath string
if _, err = getMountPoint(path); err != nil {
tmpMountPath, err = ioutil.TempDir("", "")
if _, _, err = cmd("mount", "-t", "xfs", path, tmpMountPath); err != nil {
return 0, fmt.Errorf("(fsGetSizeXFS) Can't xfs mount: %v", err)
}
}
res := cmdTrimLines("xfs_info", path)
if tmpMountPath != "" {
cmd("umount", tmpMountPath)
os.Remove(tmpMountPath)
}
for _, line := range res {
if !strings.HasPrefix(line, "data ") {
continue
}
// data = bsize=4096 blocks=26240000, imaxpct=25
// field 0 field 1 field 2 field 3 field 4
fields := strings.Fields(line)
if !strings.HasPrefix(fields[2], "bsize=") || !strings.HasPrefix(fields[3], "blocks=") {
continue
}
blockSize, err := parseUint(fields[2][len("bsize="):])
if err != nil {
return 0, err
}
blockCount, err := parseUint(fields[3][len("blocks=") : len(fields[3])-1]) // cut "blocks=" from start and "," from end.
if err != nil {
return 0, err
}
size = blockSize * blockCount
return size, nil
}
return 0, fmt.Errorf("I can't find size of xfs filesystem: %v", path)
}
// Return size of block device as it showed by kernel (in bytes)
func getDiskSize(path string) uint64 {
for i := 0; i < TRY_COUNT; i++ {
if i > 0 {
log.Println("Try to read devsize once more: ", path)
time.Sleep(time.Second)
}
res, errString, err := cmd("blockdev", "--getsize64", path)
if err != nil {
log.Println("Error while call blockdev: ", path, err, errString)
continue
}
size, err := parseUint(strings.TrimSpace(res))
if err == nil {
return size
} else {
log.Println("Can't read block device size:", path, err)
}
}
return 0
}
// return slice if all finded LVM PV
// Возвращает список всех известных lvmPV
func getLvmPV() []lvmPV {
buf := &bytes.Buffer{}
cmd := exec.Command("pvs", "-o", "pv_name,vg_name,pv_size", "--units", "B", "--separator", "|", "--noheading")
cmd.Stdout = buf
cmd.Run()
res := make([]lvmPV, 0)
for _, line := range strings.Split(buf.String(), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
lineParts := strings.Split(line, "|")
sizeString := lineParts[2][:len(lineParts[2])-1]
size, err := parseUint(sizeString)
if err != nil {
log.Println("Can't parse size: ", line, err)
continue
}
res = append(res, lvmPV{Path: lineParts[0], VolumeGroup: lineParts[1], Size: size})
}
return res
}
func getMajorMinor(path string) (major, minor int) {
for {
linkDest, err := os.Readlink(path)
if err == nil {
if filepath.IsAbs(linkDest) {
path = linkDest
} else {
path = filepath.Join(filepath.Dir(path), linkDest)
}
} else {
break
}
}
buf := &bytes.Buffer{}
cmd := exec.Command("stat", "-c", "%t:%T", path)
cmd.Stdout = buf
cmd.Run()
bufParts := strings.Split(buf.String(), ":")
if len(bufParts) != 2 {
return 0, 0
}
majorInt64, _ := strconv.ParseInt(strings.TrimSpace(bufParts[0]), 16, 32)
minorInt64, _ := strconv.ParseInt(strings.TrimSpace(bufParts[1]), 16, 32)
return int(majorInt64), int(minorInt64)
}
func getMountPoint(devPath string) (res string, err error) {
originalMajor, originalMinor := getMajorMinor(devPath)
if originalMajor == 0 {
return "", fmt.Errorf("Can't get original major/minor numbers: %v", devPath)
}
mountsBytes, err := ioutil.ReadFile("/proc/mounts")
if err != nil {
return
}
// Find mount point of the partition
// Ищем точку монтирования указанного устройства
mounts := string(mountsBytes)
for _, line := range strings.Split(mounts, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
fields := strings.Fields(line)
major, minor := getMajorMinor(fields[0])
if major == originalMajor && minor == originalMinor {
return fields[1], nil
}
}
return "", fmt.Errorf("Can't find mountpoint of: %v", devPath)
}
// Find and return partitions for create.
// Находит и возвращает описания разделов, которые можно создать на свободном дисковом пространстве.
func getNewPartitions() (res []partition) {
disks := make(map[string]diskInfo)
majorMinorCache := make(map[[2]int]string)
// Scan disks
// Сканируем диски
filepath.Walk("/dev", func(path string, info os.FileInfo, err error) error {
// If it isn't block device - skip
// Если это не файл блочного устройства - пропускаем
if !(info.Mode()&os.ModeDevice == os.ModeDevice && info.Mode()&os.ModeCharDevice == 0) {
return nil
}
major, minor := getMajorMinor(path)
if getTypeByMajorMinor(major, minor) != type_DISK {
return nil
}
disk, err := readDiskInfo(path)
if err != nil {
return err
}
if _, ok := majorMinorCache[[2]int{disk.Major, disk.Minor}]; ok {
//log.Printf("Skip '%v' by major-minor cache (%v,%v). Prev path: %v\n", path, disk.Major, disk.Minor, cachedPath)
return nil
}
disks[disk.Path] = disk
majorMinorCache[[2]int{disk.Major, disk.Minor}] = path
return nil
})
// For every disk find places for create partition
// Для каждого диска смотрим какие новые разделы можно создать.
diskLoop:
for _, disk := range disks {
if disk.PartTable != "msdos" && disk.PartTable != "gpt" {
log.Printf("I can't work with non msdos/gpt table partition. TODO.: %v, %v", disk.Path, disk.PartTable)
continue diskLoop
}
for _, part := range disk.Partitions {
if !part.IsFreeSpace() {
continue
}
if part.Size() >= min_SIZE_NEW_PARTITION {
// Need store point to copy of current item state.
// В for _, disk := range ... меняется сам экземпляр disk, а нам нужно сохранить ссылку на копию
// текущего диска
disk_copy := disk
partNum := diskNewPartitionNum(*part.Disk)
if partNum == 0 {
log.Println("Can't create partition on disk. have no free partition table entries")
continue
}
newPartition := partition{Disk: &disk_copy, FirstByte: part.FirstByte, LastByte: part.LastByte,
Number: partNum,
}
newPartition.Path = newPartition.makePath()
res = append(res, newPartition)
}
}
}
return res
}
// from https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/devices.txt?id=v4.2
// and cat /proc/devices
// and call pvs,lvs for detection lvm device
func getTypeByMajorMinor(major, minor int) storageItemType {
if res, ok := majorMinorDeviceTypeCache[[2]int{major, minor}]; ok {
return res.Type
}
switch major {
case 7:
return type_DISK
case 3, 22, 33, 34, 56, 57, 88, 89, 90, 91:
if minor%64 == 0 {
return type_DISK
} else {
return type_PARTITION
}
case 8, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135:
if minor%16 == 0 {
return type_DISK
} else {
return type_PARTITION
}
case 259:
return type_PARTITION
}
return type_UNKNOWN
}
// Path - VolumeGroup/VolumeName
func lvmLVGetSize(path string) uint64 {
buf := &bytes.Buffer{}
cmd := exec.Command("lvs", "-o", "vg_name,lv_name,lv_size", "--units", "B", "--separator", "/", "--noheading")
cmd.Stdout = buf
cmd.Run()
needPrefix := path + "/"
for _, line := range strings.Split(buf.String(), "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, needPrefix) {
sizeString := line[len(needPrefix) : len(line)-1]
lvmSize, err := strconv.ParseUint(sizeString, 10, 64)
if err != nil {
log.Println("Can't parse lvm size: ", path, sizeString, err)
return 0
}
return lvmSize
}
}
log.Println("Can't find lvm: " + path)
return 0
}
func lvmPVCalcSize(partitionSize, extentSize uint64) (pvSize uint64) {
extentCount := partitionSize / extentSize
if lvm_PV_METADATA_RESERVED > extentCount {
return 0
}
extentCount -= lvm_PV_METADATA_RESERVED
return extentCount * extentSize
}
// From time to time pvs no return size of the pvs and return it in next call or after few seconds.
func lvmPVGetSize(path string) uint64 {
res := lvmPVGetSizeTry(path)
if res == 0 {
log.Println("Error while get pvsize, try again: ", path)
time.Sleep(5 * time.Second)
res = lvmPVGetSizeTry(path)
}
return res
}
func lvmPVGetSizeTry(path string) uint64 {
buf := &bytes.Buffer{}
cmd := exec.Command("pvs", "-o", "pv_size", "--units", "B", "--separator", "|", "--noheading", path)
cmd.Stdout = buf
cmd.Run()
line := strings.TrimSuffix(strings.TrimSpace(buf.String()), "B")
pvSize, err := strconv.ParseUint(line, 10, 64)
if err != nil {
log.Println("Can't parse pv size: ", path, line, err)
return 0
}
return pvSize
log.Println("Can't find pv size: ", path, ":\n", buf.String())
return 0
}
func lvmVGGetSize(vgName string) (size, freeSize, extentSize uint64) {
res := cmdTrimLines("vgs", "--units", "B", "--separator", "/", "--noheading", "-o", "vg_name,vg_size,vg_free,vg_extent_size")
for _, line := range res {
lineParts := strings.Split(line, "/")
if lineParts[0] == vgName {
size, _ = parseUint(strings.TrimSuffix(lineParts[1], "B"))
freeSize, _ = parseUint(strings.TrimSuffix(lineParts[2], "B"))
extentSize, _ = parseUint(strings.TrimSuffix(lineParts[3], "B"))
return
}
}
log.Printf("Can't get VG size, can't find volume group: '%v'\n", vgName)
return 0, 0, 0
}
func parseUint(s string) (res uint64, err error) {
return strconv.ParseUint(s, 10, 64)
}
func readDiskInfo(path string) (disk diskInfo, err error) {
disk.Path = path
disk.Major, disk.Minor = getMajorMinor(path)
blockSizeString, _, _ := cmd("blockdev", "--getss", disk.Path)
disk.SectorSizeLogical, err = strconv.ParseUint(strings.TrimSpace(blockSizeString), 10, 64)
if err != nil {
log.Println("Can't get block size:", disk.Path, err)
return
}
disk.Size = getDiskSize(disk.Path)
if disk.Size == 0 {
log.Println("Can't get disk size:", disk.Path)
return
}
diskFile, err := os.Open(disk.Path)
defer diskFile.Close()
if err != nil {
log.Println("Error open disk: ", disk.Path, err)
return
}
var firstUsableDiskByte uint64
var lastUsableDiskByte uint64
// Try read mbr
mbrTable, err := mbr.Read(diskFile)
if err != nil && !(err == mbr.ErrorPartitionLastSectorHigh && mbrTable.IsGPT()) {
log.Println("Can't read mbr table")
}
switch {
case err != nil || mbrTable.IsGPT(): // If table is GPT or if can't read msdos table
disk.PartTable = "gpt"
var gptTable gpt.Table
gptTable, err = gpt.ReadTable(diskFile, disk.SectorSizeLogical)
if err != nil {
log.Println("Can't read gpt table: ", disk.Path)
return
}
firstUsableDiskByte = gptTable.Header.FirstUsableLBA * disk.SectorSizeLogical
lastUsableDiskByte = disk.Size - disk.SectorSizeLogical /*GPT Header sector*/ - uint64(gptTable.Header.PartitionEntrySize)*uint64(gptTable.Header.PartitionsArrLen) - 1
if (lastUsableDiskByte+1)%disk.SectorSizeLogical != 0 {
lastUsableDiskByte = lastUsableDiskByte/disk.SectorSizeLogical*disk.SectorSizeLogical - 1
}
for i, gptPart := range gptTable.Partitions {
if gptPart.IsEmpty() {
continue
}
part := partition{
Disk: &disk,
Number: uint32(i + 1),
FirstByte: gptPart.FirstLBA * disk.SectorSizeLogical,
LastByte: gptPart.LastLBA*disk.SectorSizeLogical + disk.SectorSizeLogical - 1,
}
part.Path = part.makePath()
disk.Partitions = append(disk.Partitions, part)
}
case err == nil && !mbrTable.IsGPT(): // If it is msdos table
disk.PartTable = "msdos"
firstUsableDiskByte = 512 * 63 // As parted - align for can convert to GPT in feauture.
lastUsableDiskByte = disk.Size - 1
for i, mbrPart := range mbrTable.GetAllPartitions() {
if mbrPart.IsEmpty() {
continue
}
part := partition{
Disk: &disk,
Number: uint32(i + 1),
FirstByte: uint64(mbrPart.GetLBAStart()) * disk.SectorSizeLogical,
LastByte: (uint64(mbrPart.GetLBAStart())+uint64(mbrPart.GetLBALen()))*disk.SectorSizeLogical - 1,
}
part.Path = part.makePath()
disk.Partitions = append(disk.Partitions, part)
}
}
if disk.PartTable == "" {
err = errors.New("Unknown partition table or read error")
return
}
// number of partition doesn't depend from order on disk. Sort it by disk order.
sort.Sort(partitionSortByFirstByte(disk.Partitions))
// Fix first usable byte (need for non aligned mgr partitions)
if len(disk.Partitions) > 0 && firstUsableDiskByte < disk.Partitions[0].FirstByte && disk.Partitions[0].FirstByte < min_SIZE_NEW_PARTITION {
firstUsableDiskByte = disk.Partitions[0].FirstByte
}
// make free space pseudo partitions
newPartitions := make([]partition, 0)
var lastByte = firstUsableDiskByte - 1
for i, part := range disk.Partitions {
switch {
case lastByte == part.FirstByte-1:
newPartitions = append(newPartitions, part)
case lastByte < part.FirstByte-1:
newPart := partition{
Disk: &disk,
Number: 0,
FirstByte: lastByte + 1,
LastByte: part.FirstByte - 1,
}
newPartitions = append(newPartitions, newPart, part)
default:
log.Printf("ERROR!!!! Have overlap partitions!!!!\n%v - %v\n%#v", disk.Path, i, disk.Partitions)
err = fmt.Errorf("OVERLAP PARTITIONS")
return
}
lastByte = part.LastByte
}
if lastByte < lastUsableDiskByte {
newPart := partition{
Disk: &disk,
Number: 0,
FirstByte: lastByte + 1,
LastByte: lastUsableDiskByte,
}
newPartitions = append(newPartitions, newPart)
}
disk.Partitions = newPartitions
return
}
// Scan LVM logical volumes and store major,minor numbers of them for strong detection of LVM/non LVM block device.
// Сканирует LVM_LV, запоминает их major,minor номера устройств. Для надёжного определения что блочное устройство это
// LVM/не LVM.
func scanLVM() {
buf := &bytes.Buffer{}
cmd := exec.Command("lvs", "-a", "-o", "vg_name,lv_name,lv_kernel_major,lv_kernel_minor,lv_size", "--units", "B", "--separator", "/", "--noheading")
cmd.Stdout = buf
cmd.Run()
for _, line := range strings.Split(buf.String(), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
lineParts := strings.Split(line, "/")
path := lineParts[0] + "/" + lineParts[1]
major, err := strconv.Atoi(lineParts[2])
if err != nil {
log.Println("scanLVM, Can't parse lvs (major): ", line, err)
continue
}
minor, err := strconv.Atoi(lineParts[3])
if err != nil {
log.Println("scanLVM, Can't parse lvs (minor): ", line, err)
continue
}
sizeString := lineParts[4][:len(lineParts[4])-1]
size, err := strconv.ParseUint(sizeString, 10, 64)
if err != nil {
log.Println("scanLVM, Can't parse lvs (size): ", line, err)
continue
}
majorMinorDeviceTypeCache[[2]int{major, minor}] = storageItem{Path: path, Type: type_LVM_LV, Size: size}
}
}
func readLink(path string) (res string, err error) {
stat, err := os.Lstat(path)
if err != nil {
log.Println("Can't stat while Readlink: ", path, err)
return path, err
}
if stat.Mode()&os.ModeSymlink == os.ModeSymlink {
res, err = os.Readlink(path)
if err != nil {
log.Println("Can't readlink:", path, err)
return
}
if !filepath.IsAbs(res) {
res = filepath.Join(filepath.Dir(path), res)
}
} else {
return path, nil
}
return
}