summarylogtreecommitdiffstats
path: root/btrfs_hook
blob: 3d0e97c9e2a3de56477d8c8a6eed8b620fb8398e (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
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
#!/bin/ash
# BTRFS repair/bootstrap initramfs hook
#
# Copyright (c) 2010-2012, C Anthony Risinger
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met: 
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer. 
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution. 
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# hook name
: ${BTRFS_HOOK_NAME:="btrfs_advanced"}

# output handlers
btrfs_info () { [ "${quiet}" != "y" ] && echo ":: ${BTRFS_HOOK_NAME} [INFO]: ${@}"; }
btrfs_warn () { echo ":: ${BTRFS_HOOK_NAME} [WARN]: ${@}"; }
btrfs_error () { echo ":: ${BTRFS_HOOK_NAME} [ERROR]: ${@}"; }
btrfs_fatal () { echo ":: ${BTRFS_HOOK_NAME} [FATAL]: ${@}"; break=y; }

# source config
if [ -f /etc/default/btrfs_advanced ]; then
    . /etc/default/btrfs_advanced
    btrfs_info "Configuration file /etc/default/btrfs_advanced loaded"
fi

# set defaults if not specified in config
: ${BTRFS_DIR_ACTIVE:="/__active"}
: ${BTRFS_DIR_SNAPSHOT:="/__snapshot"}
: ${BTRFS_DIR_ROLLBACK:="/__rollback"}

# strip trailing / if needed
BTRFS_DIR_ACTIVE="${BTRFS_DIR_ACTIVE%/}"
BTRFS_DIR_SNAPSHOT="${BTRFS_DIR_SNAPSHOT%/}"
BTRFS_DIR_ROLLBACK="${BTRFS_DIR_ROLLBACK%/}"

# internal variables
: ${BTRFS_HAS_ROLLBACK:=true}
: ${BTRFS_DIR_WORK:="/new_root"}
: ${BTRFS_BOOT_SUBVOL:="."}
: ${BTRFS_BOOT_SUBVOLID:=0}
: ${BTRFS_ROLLBACK_DIALOG:=true}
: ${BTRFS_ROLLBACK_CHOICE:="default"}
: ${BTRFS_ROLLBACK_LIST:="default"}
: ${BTRFS_ROLLBACK_LIST_COUNT:=1}
: ${BTRFS_ROLLBACK_KERNEL:=true}
: ${BTRFS_KERNEL:="/boot/vmlinuz-linux"}
: ${BTRFS_INITRD:="/boot/initramfs-linux.img"}
: ${BTRFS_KVER_CHECK:=true}
: ${BTRFS_KVER_DIFFER:=true}
: ${BTRFS_ENABLE_DEGRADED:=false}
: ${BTRFS_ENABLE_DEGRADED_ROLLBACK:=false}
: ${BTRFS_BOOT_DELAY:=0}

root_is_btrfs() {
    [ "$(blkid -s TYPE -o value "$root" )" != "btrfs" ] &&
        [ "$(blkid -s TYPE -o value -lt "$root" )" != "btrfs" ]
} 2>/dev/null

kver() {
    # extract kernel_version from header in kernel image
    # according to kernel documentation:
    # https://www.kernel.org/doc/Documentation/x86/boot.txt

    local offset=$(hexdump -s 526 -n 2 -e '"%d"' "$1")
    hexdump -s $(( 0x200 + offset )) -n 128 -e '"%.2s"' "$1" | strings -n10
}

btrfs_mount_handler () {
    local x i=1 work=${1%/}
    BTRFS_DIR_WORK=${work}

    if ! root_is_btrfs; then
        default_mount_handler "$@"
        return
    fi

    # temporary mount btrfs root subvolume
    if ! mount -t btrfs -o subvolid=0,rw ${root} ${work}; then

        # try to mount in degraded mode
        if ${BTRFS_ENABLE_DEGRADED}; then
            if ! mount -t btrfs -o subvolid=0,rw,degraded ${root} ${work}; then
                btrfs_fatal "Unable to mount root subvolume in degraded mode"
                return 1
            else
                btrfs_warn "Mounted root subvolume in degraded mode"
                sleep 3
                rootflags="${rootflags},degraded"

                # supress rollback choice in degraded mode
                if ! ${BTRFS_ENABLE_DEGRADED_ROLLBACK}; then
                    btrfs_info "Disabled rollback feature in degraded mode"
                    kexeced=1
                fi
            fi
        else
            btrfs_fatal "Unable to mount root subvolume"
            return 1
        fi
    fi

    # check if we already ran kexec
    if [ -z "$kexeced" ]; then

        if ! [ -d ${work}${BTRFS_DIR_ACTIVE} ]; then

            # ask to initialize active subvolume
            if ! btrfs_ask_volatile && btrfs_set_volatile; then
                BTRFS_HAS_ROLLBACK=false
            fi
        fi

        if ${BTRFS_HAS_ROLLBACK}; then

            # ask for rollback
            if ${BTRFS_ROLLBACK_DIALOG} && btrfs_ask_rollback; then

                # collect available snapshots
                for x in $(btrfs subvolume list ${work} --sort=gen | awk '{print $2 ",/" $9}' | grep -E "^[0-9]*,${BTRFS_DIR_SNAPSHOT}/"); do
                    i=$((${i}+1))
                    BTRFS_ROLLBACK_LIST_COUNT=${i}
                    BTRFS_ROLLBACK_LIST="${BTRFS_ROLLBACK_LIST} ${x}"
                done; i=1

                # print available snapshots
                echo "Available rollback snapshots:"
                for x in ${BTRFS_ROLLBACK_LIST}; do
                    echo -e "${i})\t${x#*,}"
                    i=$((${i}+1))
                done; i=1

                # let the user choose a snapshot
                BTRFS_ROLLBACK_CHOICE="$(btrfs_get_rollback_choice)"

                if [ "${BTRFS_ROLLBACK_CHOICE}" != "default" ]; then
                    # choose snapshots subvolume
                    BTRFS_BOOT_SUBVOL=${BTRFS_ROLLBACK_CHOICE#*,}

                    # snapshot to rollback
                    btrfs_setup_rollback

                    # get new subvolid of rollback subvolume
                    x="$(btrfs subvolume list ${work} | awk '{print $2 ",/" $9}' | grep -E "^[0-9]*,${BTRFS_DIR_ROLLBACK}$")"
                    BTRFS_BOOT_SUBVOL=${x#*,}
                    BTRFS_BOOT_SUBVOLID=${x%,*}
                else
                    # get subvolid of active subvolume
                    x="$(btrfs subvolume list ${work} | awk '{print $2 ",/" $9}' | grep -E "^[0-9]*,${BTRFS_DIR_ACTIVE}$")"
                    BTRFS_BOOT_SUBVOL=${x#*,}
                    BTRFS_BOOT_SUBVOLID=${x%,*}
                fi
            else
                # get subvolid of active subvolume
                x="$(btrfs subvolume list ${work} | awk '{print $2 ",/" $9}' | grep -E "^[0-9]*,${BTRFS_DIR_ACTIVE}$")"
                BTRFS_BOOT_SUBVOL=${x#*,}
                BTRFS_BOOT_SUBVOLID=${x%,*}
            fi

            # btrfs subvolume set-default
            btrfs_info "Booting from subvolume [${BTRFS_BOOT_SUBVOL}]..."
            btrfs subvolume set-default ${BTRFS_BOOT_SUBVOLID} ${work}

            # if kernel rollback is enabled (so it is by default)
            if ${BTRFS_ROLLBACK_KERNEL}; then

                # test if a new kernel is present
                if [ -f ${work}${BTRFS_BOOT_SUBVOL}${BTRFS_KERNEL} -a -f ${work}${BTRFS_BOOT_SUBVOL}${BTRFS_INITRD} ]; then

                    # compare running kernel's version against kernel image
                    if ${BTRFS_CHECK_KVER}; then
                        if kver "${work}${BTRFS_BOOT_SUBVOL}${BTRFS_KERNEL}" | grep -F "$(uname -r)" | grep -Fq "$(uname -v)"; then
                            BTRFS_KVER_DIFFER=false
                        fi
                    fi

                    # only load new kernel if versions differ
                    # if the above check failes they differ by default anyway
                    if ${BTRFS_KVER_DIFFER}; then

                        # copy kernel to initramfs
                        cp "${work}${BTRFS_BOOT_SUBVOL}${BTRFS_KERNEL}" "/tmp/linux"
                        cp "${work}${BTRFS_BOOT_SUBVOL}${BTRFS_INITRD}" "/tmp/initrd"

                        # unmount btrfs root subvolume
                        if ! umount ${BTRFS_DIR_WORK}; then
                            btrfs_fatal "Unable to umount root subvolume"
                            return 1
                        fi

                        # force reload kernel
                        btrfs_info "Reloading kernel from [${BTRFS_BOOT_SUBVOL}]..."
                        kexec -f "/tmp/linux" --initrd="/tmp/initrd" --reuse-cmdline --append="kexeced"

                        # at this point the kernel reloads the initramfs and
                        # restarts init - no further code is executed
                    fi
                else
                    btrfs_warn "Unable to load kernel from new root subvolume"
                    btrfs_warn "Check your config in /etc/default/btrfs_advanced"
                fi
            fi
        fi
    fi

    # unmount btrfs root subvolume
    if ! umount ${BTRFS_DIR_WORK}; then
        btrfs_fatal "Unable to umount root subvolume"
        return 1
    fi

    btrfs_process_mount ${work}
}

btrfs_process_mount () {

    rootfstype=btrfs

    # remove subvol/subvolid mount option
    rootflags="$(echo ${rootflags} | sed 's/subvol[id]*=[^,]*,\?//g;s/,$//g')"

    # sleep a short period of time to read console messages
    [ "${quiet}" != "y" ] && sleep ${BTRFS_BOOT_DELAY}

    # call standard mount handler
    default_mount_handler ${1}

}

btrfs_ask_volatile () {
    local r

    read -s -p "Press any key to setup BTRFS rollback support..." -n1 -t3
    r=$?

    echo
    return $r
}

btrfs_set_volatile () {

    cat <<MESSAGE

--------------------------------- INFORMATION ----------------------------------

Either this is your first time using this hook, or you have not prepared your
system for non-volatile rollback support.  Support can easily be enabled right
now, else you can continue using this hook without true non-volatile rollback
support.  To enable support, a snapshot of your system's current state must be
made to ${BTRFS_DIR_ACTIVE}.
This snapshot will subsequently be used as the primary boot device from now on.
This operation is safe. Your original / will remain intact and unused. It is
up to you to rm -rf the stagnant files from your old /{var,usr,lib,etc},
and reclaim what would in time become dead space.

ATTENTION: When deleting your old /boot, it may be necessary to reconfigure AND
           reinstall your bootloader, to search its configuration under
           ${BTRFS_DIR_ACTIVE}/boot! Otherwise your system may become
           unbootable!

DO NOT remove ${BTRFS_DIR_ACTIVE}, ${BTRFS_DIR_SNAPSHOT}, or ${BTRFS_DIR_ROLLBACK}.

The following commands will be executed:

# btrfs subvolume snapshot / ${BTRFS_DIR_ACTIVE}
# mkdir ${BTRFS_DIR_SNAPSHOT}

If you do not understand the above, are afraid, or become clammy/panicky when
presented with decisions, answer no, and seek the maintainer for clarification.

--------------------------------- INFORMATION ----------------------------------

MESSAGE

    local x i=0 ans prompt="Continue setup rollback support? (y/[n]) "
    while true; do
        read -p "${prompt}" ans
        if [ -z "${ans}" ]; then
            ans=n; break
        elif ! echo "${ans}" | grep -q -E "^[yn]$"; then
            prompt="Try again, this time choose y or n: "
        else
            break
        fi
    done

    # return 1 to DISABLE NON-VOLATILE SUPPORT, and boot / instead
    if [ ${ans} = y ]; then
        btrfs_info "Snapshotting system state to ${BTRFS_DIR_ACTIVE}..."
        if ! btrfs subvolume snapshot ${BTRFS_DIR_WORK} ${BTRFS_DIR_WORK}${BTRFS_DIR_ACTIVE}; then
            btrfs_error "Unknown problem creating ${BTRFS_DIR_ACTIVE}, fallback to /"
            return 1
        fi
        if ! mkdir ${BTRFS_DIR_WORK}${BTRFS_DIR_SNAPSHOT}; then
            btrfs_error "Unknown problem creating ${BTRFS_DIR_SNAPSHOT}, fallback to /"
            return 1
        fi
        return 0
    else
        btrfs_warn "Skipping setup of non-volatile rollback support"
        return 1
    fi

}

btrfs_ask_rollback () {
    local r

    read -s -p "Press any key to prepare BTRFS rollback..." -n1 -t3;
    r=$?

    echo
    return $r
}

btrfs_get_rollback_choice () {

    local x i=1 ans prompt="Select snapshot [${BTRFS_ROLLBACK_LIST_COUNT}]: "
    while true; do
        read -p "${prompt}" ans
        if [ -z "${ans}" ]; then
            ans=${BTRFS_ROLLBACK_LIST_COUNT}
            break
        elif ! echo "${ans}" | grep -q -E "^[0-9]+$"; then
            prompt="Invalid choice (not a number), please try again: "
        elif [ ${ans} -lt 1 ]; then
            prompt="Selection (${ans}) is too low, please try again: "
        elif [ ${ans} -gt ${BTRFS_ROLLBACK_LIST_COUNT} ]; then
            prompt="Selection (${ans}) is too high, please try again: "
        else
            break
        fi
    done

    for x in ${BTRFS_ROLLBACK_LIST}; do
        if [ ${ans} -eq ${i} ]; then
            echo "${x}"
            return 0
        fi
        i=$((${i}+1))
    done

    echo "default"
}

btrfs_setup_rollback () {

    local source=${BTRFS_DIR_WORK}${BTRFS_BOOT_SUBVOL}
    local target=${BTRFS_DIR_WORK}${BTRFS_DIR_ROLLBACK}
    if [ -d ${target} ]; then
        btrfs_info "Delete stagnant rollback environment in [${BTRFS_DIR_ROLLBACK}]..."
        btrfs subvolume delete ${target} || rm -rf ${target}
    fi
    btrfs_info "Creating new rollback environment in [${BTRFS_DIR_ROLLBACK}] from [${BTRFS_BOOT_SUBVOL}]..."
    btrfs subvolume snapshot ${source} ${target}

}

# main
run_hook () {
    mount_handler="btrfs_mount_handler"
}

# vim:set syntax=sh: