forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_device.go
141 lines (124 loc) · 4.38 KB
/
block_device.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
// Copyright 2017 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 main
import (
"os"
"github.com/mendersoftware/log"
"github.com/mendersoftware/mender/utils"
)
var (
BlockDeviceGetSizeOf BlockDeviceGetSizeFunc = getBlockDeviceSize
BlockDeviceGetSectorSizeOf BlockDeviceGetSectorSizeFunc = getBlockDeviceSectorSize
)
// BlockDeviceGetSizeFunc is a helper for obtaining the size of a block device.
type BlockDeviceGetSizeFunc func(file *os.File) (uint64, error)
// BlockDeviceGetSectorSizeFunc is a helper for obtaining the sector size of a block device.
type BlockDeviceGetSectorSizeFunc func(file *os.File) (int, error)
// BlockDevice is a low-level wrapper for a block device. The wrapper implements
// io.Writer and io.Closer interfaces.
type BlockDevice struct {
Path string // device path, ex. /dev/mmcblk0p1
out *os.File // os.File for writing
w *utils.LimitedWriter // wrapper for `out` limited the number of bytes written
typeUBI bool // Set to true if we are updating an UBI volume
ImageSize int64 // image size
}
// Write writes data `p` to underlying block device. Will automatically open
// the device in a write mode. Otherwise, behaves like io.Writer.
func (bd *BlockDevice) Write(p []byte) (int, error) {
if bd.out == nil {
log.Infof("opening device %s for writing", bd.Path)
out, err := os.OpenFile(bd.Path, os.O_WRONLY, 0)
if err != nil {
return 0, err
}
// From <mtd/ubi-user.h>
//
// UBI volume update
// ~~~~~~~~~~~~~~~~~
//
// Volume update should be done via the UBI_IOCVOLUP ioctl command of the
// corresponding UBI volume character device. A pointer to a 64-bit update
// size should be passed to the ioctl. After this, UBI expects user to write
// this number of bytes to the volume character device. The update is finished
// when the claimed number of bytes is passed. So, the volume update sequence
// is something like:
//
// fd = open("/dev/my_volume");
// ioctl(fd, UBI_IOCVOLUP, &image_size);
// write(fd, buf, image_size);
// close(fd);
if bd.typeUBI {
err := setUbiUpdateVolume(out, bd.ImageSize)
if err != nil {
log.Errorf("Failed to write images size to UBI_IOCVOLUP: %v", err)
return 0, err
}
}
size, err := BlockDeviceGetSizeOf(out)
if err != nil {
log.Errorf("failed to read block device size: %v", err)
out.Close()
return 0, err
}
log.Infof("partition %s size: %v", bd.Path, size)
bd.out = out
bd.w = &utils.LimitedWriter{
W: out,
N: size,
}
}
w, err := bd.w.Write(p)
if err != nil {
log.Errorf("written %v out of %v bytes to partition %s: %v",
w, len(p), bd.Path, err)
}
return w, err
}
// Close closes underlying block device automatically syncing any unwritten
// data. Othewise, behaves like io.Closer.
func (bd *BlockDevice) Close() error {
if bd.out != nil {
if err := bd.out.Sync(); err != nil {
log.Errorf("failed to fsync partition %s: %v", bd.Path, err)
return err
}
if err := bd.out.Close(); err != nil {
log.Errorf("failed to close partition %s: %v", bd.Path, err)
}
bd.out = nil
bd.w = nil
}
return nil
}
// Size queries the size of the underlying block device. Automatically opens a
// new fd in O_RDONLY mode, thus can be used in parallel to other operations.
func (bd *BlockDevice) Size() (uint64, error) {
out, err := os.OpenFile(bd.Path, os.O_RDONLY, 0)
if err != nil {
return 0, err
}
defer out.Close()
return BlockDeviceGetSizeOf(out)
}
// SectorSize queries the logical sector size of the underlying block device. Automatically opens a
// new fd in O_RDONLY mode, thus can be used in parallel to other operations.
func (bd *BlockDevice) SectorSize() (int, error) {
out, err := os.OpenFile(bd.Path, os.O_RDONLY, 0)
if err != nil {
return 0, err
}
defer out.Close()
return BlockDeviceGetSectorSizeOf(out)
}