File: partition.go

package info (click to toggle)
golang-github-mendersoftware-mender-artifact 3.9.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,212 kB
  • sloc: sh: 128; makefile: 128
file content (1063 lines) | stat: -rw-r--r-- 26,679 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
// Copyright 2022 Northern.tech AS
//
//    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.

package cli

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"syscall"

	"github.com/pkg/errors"

	"github.com/mendersoftware/mender-artifact/areader"
	"github.com/mendersoftware/mender-artifact/artifact"
	"github.com/mendersoftware/mender-artifact/utils"
)

const (
	fat = iota
	ext
	unsupported

	// empty placeholder, so that we can write virtualImage.Open()
	// as the API call (better semantics).
	virtualImage vImage = 1
)

var errFsTypeUnsupported = errors.New("mender-artifact can only modify ext4 and vfat payloads")
var errBlkidNotFound = errors.New("`blkid` binary not found on the system")

type VPImage interface {
	io.Closer
	Open(fpath string) (VPFile, error)
	OpenDir(fpath string) (VPDir, error)
	dirtyImage()
}

// V(irtual)P(artition)File mimicks a file in an Artifact or on an sdimg.
type VPFile interface {
	io.ReadWriteCloser
	Delete(recursive bool) error
	CopyTo(hostFile string) error
	CopyFrom(hostFile string) error
}

// VPDir V(irtual)P(artition)Dir mimics a directory in an Artifact or on an sdimg.
type VPDir interface {
	io.Closer
	Create() error
}

type partition struct {
	offset string
	size   string
	path   string
}

type ModImageBase struct {
	path  string
	dirty bool
}

type ModImageArtifact struct {
	ModImageBase
	*unpackedArtifact
	comp artifact.Compressor
	key  SigningKey
}

type ModImageSdimg struct {
	ModImageBase
	candidates []partition
}

type ModImageRaw struct {
	ModImageBase
}

type vImage int

type vImageAndFile struct {
	image VPImage
	file  VPFile
}

type vImageAndDir struct {
	image VPImage
	dir   VPDir
}

// Open is a utility function that parses an input image and returns a
// V(irtual)P(artition)Image.
func (v vImage) Open(
	key SigningKey,
	imgname string,
	overrideCompressor ...artifact.Compressor,
) (VPImage, error) {
	// first we need to check  if we are having artifact or image file
	art, err := os.Open(imgname)
	if err != nil {
		return nil, errors.Wrap(err, "can not open artifact")
	}
	defer art.Close()

	aReader := areader.NewReader(art)
	err = aReader.ReadArtifact()
	if err == nil {
		// we have VALID artifact,

		// First check if it is a module-image type
		inst := aReader.GetHandlers()

		if len(inst) > 1 {
			return nil, errors.New(
				"Modifying artifacts with more than one payload is not supported",
			)
		}

		unpackedArtifact, err := unpackArtifact(imgname)
		if err != nil {
			return nil, errors.Wrap(err, "can not process artifact")
		}

		var comp artifact.Compressor
		if len(overrideCompressor) == 1 {
			comp = overrideCompressor[0]
		} else {
			comp = unpackedArtifact.ar.Compressor()
		}

		return &ModImageArtifact{
			ModImageBase: ModImageBase{
				path:  imgname,
				dirty: false,
			},
			unpackedArtifact: unpackedArtifact,
			comp:             comp,
			key:              key,
		}, nil
	} else {
		return processSdimg(imgname)
	}
}

// Shortcut to use an image with one file. This is inefficient if you are going
// to write more than one file, since it writes out the entire image
// afterwards. In that case use VPImage and VPFile instead.
func (v vImage) OpenFile(key SigningKey, imgAndPath string) (VPFile, error) {
	imagepath, filepath, err := parseImgPath(imgAndPath)
	if err != nil {
		return nil, err
	}

	image, err := v.Open(key, imagepath)
	if err != nil {
		return nil, err
	}

	file, err := image.Open(filepath)
	if err != nil {
		image.Close()
		return nil, err
	}

	return &vImageAndFile{
		image: image,
		file:  file,
	}, nil
}

// Shortcut to use an image with one directory.
func (v vImage) OpenDir(key SigningKey, imgAndPath string) (VPDir, error) {
	imagepath, dirpath, err := parseImgPath(imgAndPath)
	if err != nil {
		return nil, err
	}

	image, err := v.Open(key, imagepath)
	if err != nil {
		return nil, err
	}

	dir, err := image.OpenDir(dirpath)
	if err != nil {
		image.Close()
		return nil, err
	}

	return &vImageAndDir{
		image: image,
		dir:   dir,
	}, nil
}

// Shortcut to open a file in the image, write into it, and close it again.
func CopyIntoImage(hostFile string, image VPImage, imageFile string) error {
	imageFd, err := image.Open(imageFile)
	if err != nil {
		return err
	}

	err = imageFd.CopyTo(hostFile)
	if err != nil {
		imageFd.Close()
		return err
	}
	return imageFd.Close()
}

// Shortcut to open a file in the image, read from it, and close it again.
func CopyFromImage(image VPImage, imageFile string, hostFile string) error {
	imageFd, err := image.Open(imageFile)
	if err != nil {
		return err
	}

	err = imageFd.CopyFrom(hostFile)
	if err != nil {
		imageFd.Close()
		return err
	}
	return imageFd.Close()
}

func (v *vImageAndFile) Read(buf []byte) (int, error) {
	return v.file.Read(buf)
}

func (v *vImageAndFile) Write(buf []byte) (int, error) {
	v.image.dirtyImage()
	return v.file.Write(buf)
}

func (v *vImageAndFile) Delete(recursive bool) error {
	v.image.dirtyImage()
	return v.file.Delete(recursive)
}

func (v *vImageAndFile) CopyTo(hostFile string) error {
	v.image.dirtyImage()
	return v.file.CopyTo(hostFile)
}

func (v *vImageAndFile) CopyFrom(hostFile string) error {
	return v.file.CopyFrom(hostFile)
}

func (v *vImageAndFile) Close() error {
	fileErr := v.file.Close()
	imageErr := v.image.Close()

	if fileErr != nil {
		return fileErr
	} else {
		return imageErr
	}
}

func (v *vImageAndDir) Create() error {
	v.image.dirtyImage()
	return v.dir.Create()
}

func (v *vImageAndDir) Close() error {
	dirErr := v.dir.Close()
	imageErr := v.image.Close()

	if dirErr != nil {
		return dirErr
	} else {
		return imageErr
	}
}

// Opens a file inside the image(s) represented by the ModImageArtifact
func (i *ModImageArtifact) Open(fpath string) (VPFile, error) {
	return newArtifactExtFile(i, i.comp, fpath, i.files[0])
}

// Opens a dir inside the image(s) represented by the ModImageArtifact
func (i *ModImageArtifact) OpenDir(fpath string) (VPDir, error) {
	return newArtifactExtDir(i, i.comp, fpath, i.files[0])
}

// Closes and repacks the artifact or sdimg.
func (i *ModImageArtifact) Close() error {
	if i.unpackDir != "" {
		defer os.RemoveAll(i.unpackDir)
	}
	if i.dirty {
		return repackArtifact(i.comp, i.key, i.unpackedArtifact)
	}
	return nil
}

func (i *ModImageArtifact) dirtyImage() {
	i.dirty = true
}

// Opens a file inside the image(s) represented by the ModImageSdimg
func (i *ModImageSdimg) Open(fpath string) (VPFile, error) {
	return newSDImgFile(i, fpath, i.candidates)
}

func (i *ModImageSdimg) OpenDir(fpath string) (VPDir, error) {
	return newSDImgDir(i, fpath, i.candidates)
}

func (i *ModImageSdimg) Close() error {
	for _, cand := range i.candidates {
		if cand.path != "" && cand.path != i.path {
			defer os.RemoveAll(cand.path)
		}
	}
	if i.dirty {
		return repackSdimg(i.candidates, i.path)
	}
	return nil
}

func (i *ModImageSdimg) dirtyImage() {
	i.dirty = true
}

func (i *ModImageRaw) Open(fpath string) (VPFile, error) {
	return newExtFile(i.path, fpath)
}

func (i *ModImageRaw) OpenDir(fpath string) (VPDir, error) {
	return newExtDir(i.path, fpath)
}

func (i *ModImageRaw) Close() error {
	return nil
}

func (i *ModImageRaw) dirtyImage() {
	i.dirty = true
}

// parseImgPath parses cli input of the form
// path/to/[sdimg,mender]:/path/inside/img/file
// into path/to/[sdimg,mender] and path/inside/img/file
func parseImgPath(imgpath string) (imgname, fpath string, err error) {
	paths := strings.SplitN(imgpath, ":", 2)
	if len(paths) != 2 {
		return "", "", fmt.Errorf("failed to parse image path %q", imgpath)
	}
	if len(paths[1]) == 0 {
		return "", "", errors.New("please enter a path into the image")
	}
	return paths[0], paths[1], nil
}

// imgFilesystemtype returns the filesystem type of a partition.
// Currently only distinguishes ext from fat.
func imgFilesystemType(imgpath string) (int, error) {
	bin, err := utils.GetBinaryPath("blkid")
	if err != nil {
		return unsupported, errBlkidNotFound
	}
	cmd := exec.Command(bin, "-s", "TYPE", imgpath)
	buf := bytes.NewBuffer(nil)
	cmd.Stdout = buf
	if err := cmd.Run(); err != nil {
		return unsupported, errors.Wrap(err, "imgFilesystemType: blkid command failed")
	}
	if strings.Contains(buf.String(), `TYPE="vfat"`) {
		return fat, nil
	} else if strings.Contains(buf.String(), `TYPE="ext`) {
		return ext, nil
	}
	return unsupported, nil
}

// From the fsck man page:
// The exit code returned by fsck is the sum of the following conditions:
//
//              0      No errors
//              1      Filesystem errors corrected
//              2      System should be rebooted
//              4      Filesystem errors left uncorrected
//              8      Operational error
//              16     Usage or syntax error
//              32     Checking canceled by user request
//              128    Shared-library error
func runFsck(image, fstype string) error {
	bin, err := utils.GetBinaryPath("fsck." + fstype)
	if err != nil {
		return errors.Wrap(err, "fsck command not found")
	}
	cmd := exec.Command(bin, "-a", image)
	if err := cmd.Run(); err != nil {
		// try to get the exit code
		if exitError, ok := err.(*exec.ExitError); ok {
			ws := exitError.Sys().(syscall.WaitStatus)
			if ws.ExitStatus() == 0 || ws.ExitStatus() == 1 {
				return nil
			}
			if ws.ExitStatus() == 8 {
				return errFsTypeUnsupported
			}
			return errors.Wrap(err, "fsck error")
		}
		return errors.New("fsck returned unparsed error")
	}
	return nil
}

// sdimgFile is a virtual file for files on an sdimg.
// It can write and read to and from all partitions (boot,rootfsa,roofsb,data),
// where if a file is located on the rootfs, a write will be duplicated to both
// partitions.
type sdimgFile []VPFile

// sdimgDir is a virtual directory for files on an sdimg.
type sdimgDir []VPDir

func isSparsePartition(part partition) bool {
	// NOTE: Basically just checking for a filesystem
	_, err := debugfsExecuteCommand("stat /", part.path)
	return err != nil
}

// filterSparsePartitions returns partitions with data from an array of partitions
func filterSparsePartitions(parts []partition) []partition {
	ps := []partition{}
	for _, part := range parts {
		if isSparsePartition(part) {
			continue
		}
		ps = append(ps, part)
	}
	return ps
}

// getFilesystems extracts only the partitions we want to modify.
// for {data,/[u]boot} this is one partition.
// for rootfs{a,b}, this is the two partitions (unless one of them is unpopulated,
// then only the one with data is returned)
func getFilesystems(fpath string, modcands []partition) ([]partition, string) {

	reg := regexp.MustCompile("/(uboot|boot/(efi|grub))[/]")

	var filesystems []partition
	if strings.HasPrefix(fpath, "/data") {
		// The data dir is not a directory in the data partition
		fpath = strings.TrimPrefix(fpath, "/data/")
		filesystems = append(filesystems, modcands[3])
	} else if reg.MatchString(fpath) {
		// /uboot, /boot/efi, /boot/grub are not directories on the boot partition.
		fpath = reg.ReplaceAllString(fpath, "")
		filesystems = append(filesystems, modcands[0])
	} else {
		filesystems = append(filesystems, filterSparsePartitions(modcands[1:3])...)
	}

	return filesystems, fpath
}

func newSDImgFile(image *ModImageSdimg, fpath string, modcands []partition) (sdimgFile, error) {
	if len(modcands) < 4 {
		return nil, fmt.Errorf("newSDImgFile: %d partitions found, 4 needed", len(modcands))
	}

	filesystems, pfpath := getFilesystems(fpath, modcands)

	// Since boot partitions can be either fat or ext, return a
	// readWriteCloser dependent upon the underlying filesystem type.
	var sdimgFile sdimgFile
	for _, fs := range filesystems {
		fstype, err := imgFilesystemType(fs.path)
		if err != nil {
			return nil, errors.Wrap(err, "partition: error reading file-system type on partition")
		}
		var f VPFile
		switch fstype {
		case fat:
			f, err = newFatFile(fs.path, pfpath)
		case ext:
			f, err = newExtFile(fs.path, pfpath)
		case unsupported:
			err = errors.New("partition: unsupported filesystem")

		}
		if err != nil {
			sdimgFile.Close()
			return nil, err
		}
		sdimgFile = append(sdimgFile, f)
	}
	return sdimgFile, nil
}

func newSDImgDir(image *ModImageSdimg, fpath string, modcands []partition) (sdimgDir, error) {
	if len(modcands) < 4 {
		return nil, fmt.Errorf("newSDImgDir: %d partitions found, 4 needed", len(modcands))
	}

	filesystems, pfpath := getFilesystems(fpath, modcands)

	// Since boot partitions can be either fat or ext, return a
	// Closer dependent upon the underlying filesystem type.
	var sdimgDir sdimgDir
	for _, fs := range filesystems {
		fstype, err := imgFilesystemType(fs.path)
		if err != nil {
			return nil, errors.Wrap(err, "partition: error reading file-system type on partition")
		}
		var d VPDir
		switch fstype {
		case fat:
			d, err = newFatDir(fs.path, pfpath)
		case ext:
			d, err = newExtDir(fs.path, pfpath)
		case unsupported:
			err = errors.New("partition: unsupported filesystem")

		}
		if err != nil {
			sdimgDir.Close()
			return nil, err
		}
		sdimgDir = append(sdimgDir, d)
	}
	return sdimgDir, nil
}

// Write forces a write from the underlying writers sdimgFile wraps.
func (p sdimgFile) Write(b []byte) (int, error) {
	for _, part := range p {
		n, err := part.Write(b)
		if err != nil {
			return n, err
		}
		if n != len(b) {
			return n, io.ErrShortWrite
		}
	}
	return len(b), nil
}

// Read reads a file from an sdimg.
func (p sdimgFile) Read(b []byte) (int, error) {
	// A read from the first partition wrapped should suffice in all cases.
	if len(p) == 0 {
		return 0, errors.New("No partition set to read from")
	}
	return p[0].Read(b)
}

func (p sdimgFile) CopyTo(hostFile string) error {
	for _, part := range p {
		err := part.CopyTo(hostFile)
		if err != nil {
			return err
		}
	}
	return nil
}

func (p sdimgFile) CopyFrom(hostFile string) error {
	if len(p) == 0 {
		return errors.New("No partition set to copy from")
	}
	return p[0].CopyFrom(hostFile)
}

// Read reads a file from an sdimg.
func (p sdimgFile) Delete(recursive bool) (err error) {
	for _, part := range p {
		err = part.Delete(recursive)
		if err != nil {
			return err
		}
	}
	return nil
}

// Close closes the underlying closers.
func (p sdimgFile) Close() (err error) {
	if p == nil {
		return nil
	}
	for _, part := range p {
		err = part.Close()
		if err != nil {
			return err
		}
	}
	return nil
}

func (p sdimgDir) Create() (err error) {
	for _, part := range p {
		err := part.Create()
		if err != nil {
			return err
		}
	}
	return nil
}

// Close closes the underlying closers.
func (p sdimgDir) Close() (err error) {
	if p == nil {
		return nil
	}
	for _, part := range p {
		err = part.Close()
		if err != nil {
			return err
		}
	}
	return nil
}

func newArtifactExtFile(
	image *ModImageArtifact,
	comp artifact.Compressor,
	fpath,
	imgpath string,
) (VPFile, error) {
	reg := regexp.MustCompile("/(uboot|boot/(efi|grub))")
	if reg.MatchString(fpath) {
		return nil, errors.New(
			"newArtifactExtFile: A mender artifact does not contain a boot partition," +
				" only a rootfs",
		)
	}
	if strings.HasPrefix(fpath, "/data") {
		return nil, errors.New(
			"newArtifactExtFile: A mender artifact does not contain a data partition," +
				" only a rootfs",
		)
	}

	return newExtFile(imgpath, fpath)
}

func newArtifactExtDir(
	image *ModImageArtifact,
	comp artifact.Compressor,
	fpath string,
	imgpath string,
) (VPDir, error) {
	reg := regexp.MustCompile("/(uboot|boot/(efi|grub))")
	if reg.MatchString(fpath) {
		return nil, errors.New(
			"newArtifactExtDir: A mender artifact does not contain a boot partition, only a rootfs",
		)
	}
	if strings.HasPrefix(fpath, "/data") {
		return nil, errors.New(
			"newArtifactExtDir: A mender artifact does not contain a data partition, only a rootfs",
		)
	}

	return newExtDir(imgpath, fpath)
}

// extFile wraps partition and implements ReadWriteCloser
type extFile struct {
	imagePath     string
	imageFilePath string
	flush         bool     // True if Close() needs to copy the file to the image
	tmpf          *os.File // Used as a buffer for multiple write operations
}

// extDir wraps partition
type extDir struct {
	imagePath     string
	imageFilePath string
}

func newExtFile(imagePath, imageFilePath string) (e *extFile, err error) {
	if err := runFsck(imagePath, "ext4"); err != nil {
		return nil, err
	}

	// Check that the given directory exists.
	_, err = debugfsExecuteCommand(fmt.Sprintf("cd %s", filepath.Dir(imageFilePath)), imagePath)
	if err != nil {
		return nil, fmt.Errorf(
			"The directory: %s does not exist in the image", filepath.Dir(imageFilePath),
		)
	}
	tmpf, err := ioutil.TempFile("", "mendertmp-extfile")
	// Cleanup resources in case of error.
	e = &extFile{
		imagePath:     imagePath,
		imageFilePath: imageFilePath,
		tmpf:          tmpf,
	}
	return e, err
}

func newExtDir(imagePath, imageFilePath string) (e *extDir, err error) {
	if err := runFsck(imagePath, "ext4"); err != nil {
		return nil, err
	}

	// Cleanup resources in case of error.
	e = &extDir{
		imagePath:     imagePath,
		imageFilePath: imageFilePath,
	}
	return e, err
}

// Write reads all bytes from b into the partitionFile using debugfs.
func (ef *extFile) Write(b []byte) (int, error) {
	n, err := ef.tmpf.Write(b)
	ef.flush = true
	return n, err
}

// Read reads all bytes from the filepath on the partition image into b
func (ef *extFile) Read(b []byte) (int, error) {
	str, err := debugfsCopyFile(ef.imageFilePath, ef.imagePath)
	defer os.RemoveAll(str) // ignore error removing tmp-dir
	if err != nil {
		return 0, errors.Wrap(err, "extFile: ReadError: debugfsCopyFile failed")
	}
	data, err := ioutil.ReadFile(filepath.Join(str, filepath.Base(ef.imageFilePath)))
	if err != nil {
		return 0, errors.Wrapf(
			err,
			"extFile: ReadError: ioutil.Readfile failed to read file: %s",
			filepath.Join(str, filepath.Base(ef.imageFilePath)),
		)
	}
	return copy(b, data), io.EOF
}

func (ef *extFile) CopyTo(hostFile string) error {
	if err := debugfsReplaceFile(ef.imageFilePath, hostFile, ef.imagePath); err != nil {
		return err
	}
	return nil
}

func (ef *extFile) CopyFrom(hostFile string) error {
	// Get the file permissions
	d, err := debugfsExecuteCommand(fmt.Sprintf("stat %s", ef.imageFilePath), ef.imagePath)
	if err != nil {
		if strings.Contains(err.Error(), "File not found by ext2_lookup") {
			return fmt.Errorf("The file: %s does not exist in the image", ef.imageFilePath)
		}
		return err
	}
	// Extract the Mode: oooo octal code
	reg := regexp.MustCompile(`Mode: +(0[0-9]{3})`)
	m := reg.FindStringSubmatch(d.String())
	if m == nil || len(m) != 2 {
		return fmt.Errorf(
			"Could not extract the filemode information from the file: %s\n",
			ef.imageFilePath,
		)
	}
	mode, err := strconv.ParseInt(m[1], 8, 32)
	if err != nil {
		return fmt.Errorf(
			"Failed to extract the file permissions for the file: %s\nerr: %s",
			ef.imageFilePath,
			err,
		)
	}
	_, err = debugfsExecuteCommand(
		fmt.Sprintf("dump %s %s\nclose", ef.imageFilePath, hostFile),
		ef.imagePath,
	)
	if err != nil {
		if strings.Contains(err.Error(), "File not found by ext2_lookup") {
			return fmt.Errorf("The file: %s does not exist in the image", ef.imageFilePath)
		}
		return err
	}
	if err = os.Chmod(hostFile, os.FileMode(mode)); err != nil {
		return err
	}
	return nil
}

func (ef *extFile) Delete(recursive bool) (err error) {
	err = debugfsRemoveFileOrDir(ef.imageFilePath, ef.imagePath, recursive)
	if err != nil {
		return err
	}
	return nil
}

// Close closes the temporary file held by partitionFile path.
func (ef *extFile) Close() (err error) {
	if ef == nil {
		return nil
	}
	if ef.tmpf != nil {
		defer func() {
			// Ignore tmp-errors
			ef.tmpf.Close()
			os.Remove(ef.tmpf.Name())
		}()
		if ef.flush {
			err = debugfsReplaceFile(ef.imageFilePath, ef.tmpf.Name(), ef.imagePath)
			if err != nil {
				return err
			}
		}
	}
	return err
}

func (ed *extDir) Create() error {
	err := debugfsMakeDir(ed.imageFilePath, ed.imagePath)
	return err
}

// Close closes the temporary file held by partitionFile path.
func (ed *extDir) Close() (err error) {
	if ed == nil {
		return nil
	}
	return err
}

// fatFile wraps a partition struct with a reader/writer for fat filesystems
type fatFile struct {
	imagePath     string
	imageFilePath string // The local filesystem path to the image
	flush         bool
	tmpf          *os.File
}

type fatDir struct {
	imagePath     string
	imageFilePath string
}

func newFatFile(imagePath, imageFilePath string) (*fatFile, error) {
	if err := runFsck(imagePath, "vfat"); err != nil {
		return nil, err
	}

	tmpf, err := ioutil.TempFile("", "mendertmp-fatfile")
	ff := &fatFile{
		imagePath:     imagePath,
		imageFilePath: imageFilePath,
		tmpf:          tmpf,
	}
	return ff, err
}

func newFatDir(imagePath, imageFilePath string) (fd *fatDir, err error) {
	if err := runFsck(imagePath, "vfat"); err != nil {
		return nil, err
	}

	fd = &fatDir{
		imagePath:     imagePath,
		imageFilePath: imageFilePath,
	}
	return fd, err
}

// Read Dump the file contents to stdout, and capture, using MTools' mtype
func (f *fatFile) Read(b []byte) (n int, err error) {
	cmd := exec.Command("mtype", "-n", "-i", f.imagePath, "::"+f.imageFilePath)
	dbuf := bytes.NewBuffer(nil)
	cmd.Stdout = dbuf // capture Stdout
	if err = cmd.Run(); err != nil {
		return 0, errors.Wrap(err, "fatPartitionFile: Read: MTools mtype dump failed")
	}
	return copy(b, dbuf.Bytes()), io.EOF
}

// Write Writes to the underlying fat image, using MTools' mcopy
func (f *fatFile) Write(b []byte) (n int, err error) {
	n, err = f.tmpf.Write(b)
	if err != nil {
		return n, err
	}
	f.flush = true
	return n, nil
}

func (f *fatFile) CopyTo(hostFile string) error {
	cmd := exec.Command("mcopy", "-oi", f.imagePath, hostFile, "::"+f.imageFilePath)
	data := bytes.NewBuffer(nil)
	cmd.Stdout = data
	if err := cmd.Run(); err != nil {
		return errors.Wrap(err, "fatFile: Write: MTools execution failed")
	}
	return nil
}

func (f *fatFile) CopyFrom(hostFile string) error {
	cmd := exec.Command("mcopy", "-n", "-i", f.imagePath, "::"+f.imageFilePath, hostFile)
	dbuf := bytes.NewBuffer(nil)
	cmd.Stdout = dbuf // capture Stdout
	if err := cmd.Run(); err != nil {
		return errors.Wrap(err, "fatPartitionFile: Read: MTools mcopy failed")
	}
	return nil
}

func (f *fatFile) Delete(recursive bool) (err error) {
	isDir := filepath.Dir(f.imageFilePath) == strings.TrimRight(f.imageFilePath, "/")
	var deleteCmd string
	if isDir {
		deleteCmd = "mdeltree"
	} else {
		deleteCmd = "mdel"
	}
	cmd := exec.Command(deleteCmd, "-i", f.imagePath, "::"+f.imageFilePath)
	if err = cmd.Run(); err != nil {
		return errors.Wrap(err, "fatFile: Delete: execution failed: "+deleteCmd)
	}
	return nil
}

func (f *fatFile) Close() (err error) {
	if f == nil {
		return nil
	}
	if f.tmpf != nil {
		defer func() {
			f.tmpf.Close()
			os.Remove(f.tmpf.Name())
		}()
		if f.flush {
			cmd := exec.Command(
				"mcopy",
				"-n",
				"-i",
				f.imagePath,
				f.tmpf.Name(),
				"::"+f.imageFilePath,
			)
			data := bytes.NewBuffer(nil)
			cmd.Stdout = data
			if err = cmd.Run(); err != nil {
				return errors.Wrap(err, "fatFile: Write: MTools execution failed")
			}
		}
	}
	return err
}

func (fd *fatDir) Create() (err error) {
	cmd := exec.Command("mmd", fd.imageFilePath)
	data := bytes.NewBuffer(nil)
	cmd.Stdout = data
	if err := cmd.Run(); err != nil {
		return errors.Wrap(err, "fatDir: Create: MTools execution failed")
	}
	return err
}

func (fd *fatDir) Close() (err error) {
	if fd == nil {
		return nil
	}
	os.Remove(fd.imagePath)
	return err
}

func processSdimg(image string) (VPImage, error) {
	bin, err := utils.GetBinaryPath("parted")
	if err != nil {
		return nil, errors.Wrap(err, "`parted` binary not found on the system")
	}
	out, err := exec.Command(bin, image, "unit s", "print").Output()
	if err != nil {
		return nil, errors.Wrap(err, "can not execute `parted` command or image is broken; "+
			"make sure parted is available in your system and is in the $PATH")
	}

	reg := regexp.MustCompile(
		`(?m)^[[:blank:]][0-9]+[[:blank:]]+([0-9]+)s[[:blank:]]+[0-9]+s[[:blank:]]+([0-9]+)s`,
	)
	partitionMatch := reg.FindAllStringSubmatch(string(out), -1)

	if len(partitionMatch) == 4 {
		partitions := make([]partition, 0)
		// we will have three groups per each entry in the partition table
		for i := 0; i < 4; i++ {
			single := partitionMatch[i]
			partitions = append(partitions, partition{offset: single[1], size: single[2]})
		}
		if partitions, err = extractFromSdimg(partitions, image); err != nil {
			return nil, err
		}
		return &ModImageSdimg{
			ModImageBase: ModImageBase{
				path:  image,
				dirty: false,
			},
			candidates: partitions,
		}, nil
		// if we have single ext file there is no need to mount it

	} else if len(partitionMatch) == 1 && partitionMatch[0][1] == "0" {
		// For one partition match which has an offset of zero, we
		// assume it is a raw filesystem image.
		return &ModImageRaw{
			ModImageBase: ModImageBase{
				path:  image,
				dirty: false,
			},
		}, nil
	}
	return nil, fmt.Errorf("invalid partition table: %s", string(out))
}

func extractFromSdimg(partitions []partition, image string) ([]partition, error) {
	for i, part := range partitions {
		tmp, err := ioutil.TempFile("", "mender-modify-image")
		if err != nil {
			return nil, errors.Wrap(err, "can not create temp file for storing image")
		}
		if err = tmp.Close(); err != nil {
			return nil, errors.Wrapf(err, "can not close temporary file: %s", tmp.Name())
		}
		cmd := exec.Command("dd", "if="+image, "of="+tmp.Name(),
			"skip="+part.offset, "count="+part.size)
		if err = cmd.Run(); err != nil {
			return nil, errors.Wrap(err, "can not extract image from sdimg")
		}
		partitions[i].path = tmp.Name()
	}
	return partitions, nil
}

func repackSdimg(partitions []partition, image string) error {
	for _, part := range partitions {
		if err := exec.Command("dd", "if="+part.path, "of="+image,
			"seek="+part.offset, "count="+part.size,
			"conv=notrunc").Run(); err != nil {
			return errors.Wrap(err, "can not copy image back to sdimg")
		}
	}
	return nil
}