archiso 8.7 KB
Newer Older
1
#!/bin/ash
2
3
#
# SPDX-License-Identifier: GPL-3.0-or-later
4

5
# args: source, newroot, mountpoint
6
_mnt_dmsnapshot() {
7
    local img="${1}"
8
9
    local newroot="${2}"
    local mnt="${3}"
10
11
    local img_fullname="${img##*/}";
    local img_name="${img_fullname%%.*}"
12
    local dm_snap_name="${dm_snap_prefix}_${img_name}"
13
    local ro_dev ro_dev_size rw_dev
14

15
16
    ro_dev="$(losetup --find --show --read-only -- "${img}")"
    echo "${ro_dev}" >> /run/archiso/used_block_devices
17
    ro_dev_size="$(blockdev --getsz "${ro_dev}")"
18

19
20
    if [ "${cow_persistent}" = "P" ]; then
        if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
21
            msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow', using as persistent."
22
        else
23
            msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as persistent."
24
            truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
25
26
        fi
    else
27
        if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
28
29
            msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' but non-persistent requested, removing."
            rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
30
        fi
31
        msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as non-persistent."
32
        truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
33
34
    fi

35
36
    rw_dev="$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")"
    echo "${rw_dev}" >> /run/archiso/used_block_devices
37

38
39
    dmsetup create "${dm_snap_name}" --table \
        "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}"
40

41
    if [ "${cow_persistent}" != "P" ]; then
42
43
44
        rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
    fi

45
    _mnt_dev "/dev/mapper/${dm_snap_name}" "${newroot}${mnt}" "-w" "defaults"
46
    readlink -f "/dev/mapper/${dm_snap_name}" >> /run/archiso/used_block_devices
Simo Leone's avatar
Simo Leone committed
47
48
}

49
50
51
52
53
# args: source, newroot, mountpoint
_mnt_overlayfs() {
    local src="${1}"
    local newroot="${2}"
    local mnt="${3}"
54
55
56
57
    mkdir -p "/run/archiso/cowspace/${cow_directory}/upperdir" "/run/archiso/cowspace/${cow_directory}/workdir"
    mount -t overlay -o \
    "lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir" \
    airootfs "${newroot}${mnt}"
58
59
60
}


61
# args: /path/to/image_file, mountpoint
62
63
64
_mnt_sfs() {
    local img="${1}"
    local mnt="${2}"
65
    local img_fullname="${img##*/}"
66
    local sfs_dev
67

68
69
    # shellcheck disable=SC2154
    # defined via initcpio's parse_cmdline()
70
    if [ "${copytoram}" = "y" ]; then
71
        msg -n ":: Copying squashfs image to RAM..."
72
        if ! cp -- "${img}" "/run/archiso/copytoram/${img_fullname}" ; then
73
            echo "ERROR: while copy '${img}' to '/run/archiso/copytoram/${img_fullname}'"
74
75
            launch_interactive_shell
        fi
76
        img="/run/archiso/copytoram/${img_fullname}"
77
        msg "done."
78
    fi
79
80
    sfs_dev="$(losetup --find --show --read-only -- "${img}")"
    echo "${sfs_dev}" >> /run/archiso/used_block_devices
81
    _mnt_dev "${sfs_dev}" "${mnt}" "-r" "defaults"
Simo Leone's avatar
Simo Leone committed
82
83
}

84
# args: device, mountpoint, flags, opts
85
86
87
88
_mnt_dev() {
    local dev="${1}"
    local mnt="${2}"
    local flg="${3}"
89
    local opts="${4}"
90

91
92
    mkdir -p "${mnt}"

93
94
95
96
97
98
99
100
101
    msg ":: Mounting '${dev}' to '${mnt}'"

    while ! poll_device "${dev}" 30; do
        echo "ERROR: '${dev}' device did not show up after 30 seconds..."
        echo "   Falling back to interactive prompt"
        echo "   You can try to fix the problem manually, log out when you are finished"
        launch_interactive_shell
    done

102
    if mount -o "${opts}" "${flg}" "${dev}" "${mnt}"; then
103
        msg ":: Device '${dev}' mounted successfully."
104
    else
105
        echo "ERROR; Failed to mount '${dev}'"
106
107
108
109
110
111
        echo "   Falling back to interactive prompt"
        echo "   You can try to fix the problem manually, log out when you are finished"
        launch_interactive_shell
    fi
}

112
113
_verify_checksum() {
    local _status
114
    cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
115
    sha512sum -c airootfs.sha512 > /tmp/checksum.log 2>&1
116
    _status=$?
117
118
    cd -- "${OLDPWD}" || exit 1
    return "${_status}"
119
120
}

121
122
_verify_signature() {
    local _status
123
    cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
124
125
    gpg --homedir /gpg --status-fd 1 --verify airootfs.sfs.sig 2>/dev/null | grep -qE '^\[GNUPG:\] GOODSIG'
    _status=$?
126
    cd -- "${OLDPWD}" || exit 1
127
128
129
    return ${_status}
}

130
run_hook() {
131
132
133
134
    [ -z "${arch}" ] && arch="$(uname -m)"
    [ -z "${copytoram_size}" ] && copytoram_size="75%"
    [ -z "${archisobasedir}" ] && archisobasedir="arch"
    [ -z "${dm_snap_prefix}" ] && dm_snap_prefix="arch"
135
136
    # shellcheck disable=SC2154
    # defined via initcpio's parse_cmdline()
137
138
    [ -z "${archisodevice}" ] && archisodevice="/dev/disk/by-label/${archisolabel}"
    [ -z "${cow_spacesize}" ] && cow_spacesize="256M"
139
140
    # shellcheck disable=SC2154
    # defined via initcpio's parse_cmdline()
141
    if [ -n "${cow_label}" ]; then
142
        cow_device="/dev/disk/by-label/${cow_label}"
143
144
145
        [ -z "${cow_persistent}" ] && cow_persistent="P"
    elif [ -n "${cow_device}" ]; then
        [ -z "${cow_persistent}" ] && cow_persistent="P"
146
147
148
149
    else
        cow_persistent="N"
    fi

150
151
152
    [ -z "${cow_flags}" ] && cow_flags="defaults"
    [ -z "${cow_directory}" ] && cow_directory="persistent_${archisolabel}/${arch}"
    [ -z "${cow_chunksize}" ] && cow_chunksize="8"
153

154
    # set mount handler for archiso
155
    export mount_handler="archiso_mount_handler"
156
157
}

158
159
160
# This function is called normally from init script, but it can be called
# as chain from other mount handlers.
# args: /path/to/newroot
161
archiso_mount_handler() {
162
163
    local newroot="${1}"

164
    if ! mountpoint -q "/run/archiso/bootmnt"; then
165
        _mnt_dev "${archisodevice}" "/run/archiso/bootmnt" "-r" "defaults"
166
167
        if [ "${copytoram}" != "y" ]; then
            readlink -f "${archisodevice}" >> /run/archiso/used_block_devices
168
169
170
        fi
    fi

171
172
    # shellcheck disable=SC2154
    # defined via initcpio's parse_cmdline()
173
174
    if [ "${checksum}" = "y" ]; then
        if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]; then
175
176
177
178
179
            msg -n ":: Self-test requested, please wait..."
            if _verify_checksum; then
                msg "done. Checksum is OK, continue booting."
            else
                echo "ERROR: one or more files are corrupted"
180
                echo "see /tmp/checksum.log for details"
181
182
183
                launch_interactive_shell
            fi
        else
184
            echo "ERROR: checksum=y option specified but ${archisobasedir}/${arch}/airootfs.sha512 not found"
185
186
187
188
            launch_interactive_shell
        fi
    fi

189
190
    # shellcheck disable=SC2154
    # defined via initcpio's parse_cmdline()
191
192
    if [ "${verify}" = "y" ]; then
        if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]; then
193
194
195
196
197
198
199
200
201
202
203
204
205
            msg -n ":: Signature verification requested, please wait..."
            if _verify_signature; then
                msg "done. Signature is OK, continue booting."
            else
                echo "ERROR: one or more files are corrupted"
                launch_interactive_shell
            fi
        else
            echo "ERROR: verify=y option specified but ${archisobasedir}/${arch}/airootfs.sfs.sig not found"
            launch_interactive_shell
        fi
    fi

206
    if [ "${copytoram}" = "y" ]; then
207
208
209
        msg ":: Mounting /run/archiso/copytoram (tmpfs) filesystem, size=${copytoram_size}"
        mkdir -p /run/archiso/copytoram
        mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /run/archiso/copytoram
210
211
    fi

212
    if [ -n "${cow_device}" ]; then
213
        _mnt_dev "${cow_device}" "/run/archiso/cowspace" "-r" "${cow_flags}"
214
        readlink -f "${cow_device}" >> /run/archiso/used_block_devices
215
        mount -o remount,rw "/run/archiso/cowspace"
216
    else
217
        msg ":: Mounting /run/archiso/cowspace (tmpfs) filesystem, size=${cow_spacesize}..."
218
        mkdir -p /run/archiso/cowspace
219
        mount -t tmpfs -o "size=${cow_spacesize}",mode=0755 cowspace /run/archiso/cowspace
220
    fi
221
222
    mkdir -p "/run/archiso/cowspace/${cow_directory}"
    chmod 0700 "/run/archiso/cowspace/${cow_directory}"
223

224
    _mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs" "/run/archiso/sfs/airootfs"
225
    if [ -f "/run/archiso/sfs/airootfs/airootfs.img" ]; then
226
227
228
229
        _mnt_dmsnapshot "/run/archiso/sfs/airootfs/airootfs.img" "${newroot}" "/"
    else
        _mnt_overlayfs "/run/archiso/sfs/airootfs" "${newroot}" "/"
    fi
230

231
    if [ "${copytoram}" = "y" ]; then
232
        umount -d /run/archiso/bootmnt
Aaron Griffin's avatar
Aaron Griffin committed
233
234
    fi
}
Dan McGee's avatar
Dan McGee committed
235

236
# vim: set ft=sh: