summaryrefslogtreecommitdiff
path: root/morphlib/recv-hole
blob: d6504bf6f3338e58fcc922079aed6ba65dab36eb (plain)
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
#!/bin/sh
#
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# =*= License: GPL-2 =*=


# Receive a data stream describing a sparse file, and reproduce it,
# either to a named file or stdout.
#
# The data stream is simple: it's a sequence of DATA or HOLE records:
#
#    DATA
#    123
#    <123 bytes of binary data, NOT including newline at the end>
#
#    HOLE
#    123
#
# This shell script can be executed over ssh (given to ssh as an arguemnt,
# with suitable escaping) on a different computer. This allows a large
# sparse file (e.g., disk image) be transferred quickly.
#
# This script should be called in one of the following ways:
#
#    recv-hole file FILENAME
#    recv-hole vbox FILENAME DISKSIZE
#
# In both cases, FILENAME is the pathname of the disk image on the
# receiving end. DISKSIZE is the size of the disk image in bytes. The
# first form is used when transferring a disk image to become an
# identical file on the receiving end.
#
# The second form is used when the disk image should be converted for
# use by VirtualBox. In this case, we want to avoid writing a
# temporary file on disk, and then calling the VirtualBox VBoxManage
# tool to do the conversion, since that would involve large amounts of
# unnecessary I/O and disk usage. Instead we pipe the file directly to
# VBoxManage, avoiding those issues. The piping is done here in this
# script, instead of in the caller, to make it easier to run things
# over ssh.
#
# However, since it's not possible seek in a Unix pipe, we have to
# explicitly write the zeroes into the pipe. This is not
# super-efficient, but the way to avoid that would be to avoid sending
# a sparse file, and do the conversion to a VDI on the sending end.
# That is out of scope for xfer-hole and recv-hole.


set -eu


die()
{
    echo "$@" 1>&2
    exit 1
}


recv_hole_to_file()
{
    local n

    read n
    truncate --size "+$n" "$1"
}


recv_data_to_file()
{
    local n
    read n

    local blocksize=1048576
    local blocks=$(($n / $blocksize))
    local extra=$(($n % $blocksize))

    xfer_data_to_stdout "$blocksize" "$blocks" >> "$1"
    xfer_data_to_stdout 1 "$extra" >> "$1"
}


recv_hole_to_stdout()
{
    local n
    read n
    (echo "$n"; cat /dev/zero) | recv_data_to_stdout
}


recv_data_to_stdout()
{
    local n
    read n

    local blocksize=1048576
    local blocks=$(($n / $blocksize))
    local extra=$(($n % $blocksize))

    xfer_data_to_stdout "$blocksize" "$blocks"
    xfer_data_to_stdout 1 "$extra"
}


xfer_data_to_stdout()
{
    local log="$(mktemp)"
    if ! dd "bs=$1" count="$2" iflag=fullblock status=noxfer 2> "$log"
    then
        cat "$log" 1>&2
        rm -f "$log"
        exit 1
    else
        rm -f "$log"
    fi
}


type="$1"
case "$type" in
    file)
        output="$2"
        truncate --size=0 "$output"
        while read what
        do
            case "$what" in
                DATA) recv_data_to_file "$output" ;;
                HOLE) recv_hole_to_file "$output" ;;
                *) die "Unknown instruction: $what" ;;
            esac
        done
        ;;
    vbox)
        output="$2"
        disk_size="$3"
        while read what
        do
            case "$what" in
                DATA) recv_data_to_stdout ;;
                HOLE) recv_hole_to_stdout ;;
                *) die "Unknown instruction: $what" ;;
            esac
        done |
        VBoxManage convertfromraw stdin "$output" "$disk_size"
        ;;
esac