archiso 8.24 KB
Newer Older
1
2
3
# Initialize loopback device logic (we using on-demand mode)
# args: none
_init_loop_dev() {
4
5
6
    loop_dev_count=99
    loop_dev_range=$(cat /sys/block/loop0/range)
    loop_dev_minor=$((loop_dev_count*loop_dev_range))
7
8
9
10
11
}

# Call this function before _make_loop_dev() each time.
# args: none
_next_loop_dev() {
12
13
    loop_dev_count=$((loop_dev_count+1))
    loop_dev_minor=$((loop_dev_count*loop_dev_range))
14
15
}

16
# Setup a loopback device for image passed as argument and echo the path to loopback device used.
17
18
19
# args: /path/to/image_file
_make_loop_dev() {
    local img="${1}"
20
21
22
    mknod /dev/loop${loop_dev_count} b 7 ${loop_dev_minor} &> /dev/null
    losetup /dev/loop${loop_dev_count} "${img}" &> /dev/null
    echo /dev/loop${loop_dev_count}
23
24
}

25
# args: source, newroot, mountpoint
26
27
_mnt_fs() {
    local img="${1}"
28
29
    local newroot="${2}"
    local mnt="${3}"
30
31
    local img_fullname="${img##*/}";
    local img_name="${img_fullname%%.*}"
32
    local dm_snap_name="${dm_snap_prefix}_${img_name}"
33
    local ro_dev ro_dev_size rw_dev
34

35
    mkdir -p "${newroot}${mnt}"
36
37
38
39

    _next_loop_dev
    ro_dev=$(_make_loop_dev "${img}")
    ro_dev_size=$(blockdev --getsz ${ro_dev})
40
41

    if [[ "${cow_persistent}" == "P" ]]; then
42
43
        if [[ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]]; then
            msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow', using as persistent."
44
        else
45
46
            msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as persistent."
            dd of="/run/archiso/cowspace/${cow_directory}/${img_name}.cow" count=0 seek=${ro_dev_size} &> /dev/null
47
48
        fi
    else
49
50
51
        if [[ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]]; then
            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"
52
        fi
53
54
        msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as non-persistent."
        dd of="/run/archiso/cowspace/${cow_directory}/${img_name}.cow" count=0 seek=${ro_dev_size} &> /dev/null
55
56
    fi

57
    _next_loop_dev
58
    rw_dev=$(_make_loop_dev "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")
59

60
    echo "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} 8" | dmsetup create ${dm_snap_name}
61

62
63
    msg ":: Mounting '/dev/mapper/${dm_snap_name}' to '${newroot}${mnt}'"
    if ! mount "/dev/mapper/${dm_snap_name}" "${newroot}${mnt}" ; then
64
        echo "ERROR: while mounting '/dev/mapper/${dm_snap_name}' to '${newroot}${mnt}'"
65
        launch_interactive_shell
66
67
    else
        echo "/dev/mapper/${dm_snap_name} ${mnt} auto defaults 0 0" >> ${newroot}/etc/fstab
68
    fi
Simo Leone's avatar
Simo Leone committed
69
70
}

71
# args: /path/to/image_file, mountpoint
72
73
74
75
76
77
78
79
_mnt_sfs() {
    local img="${1}"
    local mnt="${2}"
    local img_fullname="${img##*/}";

    mkdir -p "${mnt}"

    if [[ "${copytoram}" == "y" ]]; then
80
        msg -n ":: Copying squashfs image to RAM..."
81
82
        if ! cp "${img}" "/run/archiso/copytoram/${img_fullname}" ; then
            echo "ERROR: while copy '${img}' to '/run/archiso/copytoram/${img_fullname}'"
83
84
            launch_interactive_shell
        fi
85
        img="/run/archiso/copytoram/${img_fullname}"
86
        msg "done."
87
    fi
88
89
    _next_loop_dev
    msg ":: Mounting '${img}' (SquashFS) to '${mnt}'"
90
    if ! mount -r $(_make_loop_dev "${img}") "${mnt}" &> /dev/null ; then
91
        echo "ERROR: while mounting '${img}' to '${mnt}'"
92
93
        launch_interactive_shell
    fi
Simo Leone's avatar
Simo Leone committed
94
95
}

96
97
98
99
100
101
# args: device, mountpoint, flags
_mnt_dev() {
    local dev="${1}"
    local mnt="${2}"
    local flg="${3}"

102
103
    mkdir -p "${mnt}"

104
105
106
107
108
109
110
111
112
    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

113
114
    if mount "${flg}" "${dev}" "${mnt}"; then
        msg ":: Device '${dev}' mounted successfully."
115
    else
116
        echo "ERROR; Failed to mount '${dev}'"
117
118
119
120
121
122
        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
}

123
124
_verify_checksum() {
    local _status
125
    cd "/run/archiso/bootmnt/${archisobasedir}"
126
    md5sum -c checksum.${arch}.md5 > /tmp/checksum.${arch}.log 2>&1
127
128
129
130
131
    _status=$?
    cd "${OLDPWD}"
    return ${_status}
}

132
run_hook() {
133
134
    modprobe loop

135
136
137
138
    [[ -z "${arch}" ]] && arch="$(uname -m)"
    [[ -z "${cowspace_size}" ]] && cowspace_size="75%"
    [[ -z "${copytoram_size}" ]] && copytoram_size="75%"
    [[ -z "${archisobasedir}" ]] && archisobasedir="arch"
139
    [[ -z "${dm_snap_prefix}" ]] && dm_snap_prefix="arch"
140
    [[ -z "${archisodevice}" ]] && archisodevice="/dev/disk/by-label/${archisolabel}"
141

142
    if [[ -z "${aitab}" ]]; then
143
        aitab="/run/archiso/bootmnt/${archisobasedir}/aitab"
144
    else
145
        aitab="/run/archiso/bootmnt/${archisobasedir}/${aitab}"
146
    fi
147
148
149
150
151
152
153
154
155
156
157
158

    if [[ -n "${cow_label}" ]]; then
        cow_device="/dev/disk/by-label/${cow_label}"
        [[ -z "${cow_persistent}" ]] && cow_persistent="P"
    elif [[ -n "${cow_device}" ]]; then
        [[ -z "${cow_persistent}" ]] && cow_persistent="P"
    else
        cow_persistent="N"
    fi

    [[ -z "${cow_directory}" ]] && cow_directory="persistent_${archisolabel}/${arch}"

159
160
161
162
    # set mount handler for archiso
    mount_handler="archiso_mount_handler"
}

163
164
165
# This function is called normally from init script, but it can be called
# as chain from other mount handlers.
# args: /path/to/newroot
166
archiso_mount_handler() {
167
168
169
    local newroot="${1}"

    _init_loop_dev
170

171
    mountpoint -q "/run/archiso/bootmnt" || _mnt_dev "${archisodevice}" "/run/archiso/bootmnt" "-r"
172

173
174
    if [[ ! -f "${aitab}" ]]; then
        echo "ERROR: '${aitab}' file does not exist."
175
176
177
        echo "   Falling back to interactive prompt"
        echo "   You can try to fix the problem manually, log out when you are finished"
        launch_interactive_shell
Aaron Griffin's avatar
Aaron Griffin committed
178
179
    fi

180
    if [[ "${checksum}" == "y" ]]; then
181
        if [[ -f "/run/archiso/bootmnt/${archisobasedir}/checksum.${arch}.md5" ]]; then
182
183
184
185
186
            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"
187
                echo "see /tmp/checksum.${arch}.log for details"
188
189
190
                launch_interactive_shell
            fi
        else
191
            echo "ERROR: checksum=y option specified but checksum.${arch}.md5 not found"
192
193
194
195
            launch_interactive_shell
        fi
    fi

196
    if [[ "${copytoram}" == "y" ]]; then
197
198
199
        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
200
201
    fi

202
    if [[ -n "${cow_device}" ]]; then
203
204
        _mnt_dev "${cow_device}" "/run/archiso/cowspace" "-r"
        mount -o remount,rw "/run/archiso/cowspace"
205
    else
206
207
208
        msg ":: Mounting /run/archiso/cowspace (tmpfs) filesystem, size=${cowspace_size}..."
        mkdir -p /run/archiso/cowspace
        mount -t tmpfs -o "size=${cowspace_size}",mode=0755 cowspace /run/archiso/cowspace
209
    fi
210
    mkdir -p "/run/archiso/cowspace/${cow_directory}"
211

212
213
214
215
216
    local aitab_img aitab_mnt aitab_arch aitab_sfs_comp aitab_fs_type aitab_fs_size
    while read aitab_img aitab_mnt aitab_arch aitab_sfs_comp aitab_fs_type aitab_fs_size; do
        [[ "${aitab_img#\#}" != "${aitab_img}" ]] && continue
        [[ "${aitab_arch}" != "any" && "${aitab_arch}" != "${arch}" ]] && continue
        if [[ "${aitab_fs_type}" != "none" ]]; then
217
            _mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${aitab_arch}/${aitab_img}.fs.sfs" "/run/archiso/sfs/${aitab_img}"
218
            _mnt_fs "/run/archiso/sfs/${aitab_img}/${aitab_img}.fs" "${newroot}" "${aitab_mnt}"
219
        else
220
            _mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${aitab_arch}/${aitab_img}.sfs" "${newroot}${aitab_mnt}"
Aaron Griffin's avatar
Aaron Griffin committed
221
        fi
222
    done < "${aitab}"
223

224
    if [[ "${copytoram}" == "y" ]]; then
225
        umount /run/archiso/bootmnt
Aaron Griffin's avatar
Aaron Griffin committed
226
227
    fi
}
Dan McGee's avatar
Dan McGee committed
228
229

# vim:ft=sh:ts=4:sw=4:et: