ventoy-disk.sh 2.88 KB
Newer Older
longpanda's avatar
update  
longpanda committed
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
#!/ventoy/busybox/sh
#************************************************************************************
# Copyright (c) 2020, longpanda <admin@ventoy.net>
# 
# 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; either version 3 of the
# License, or (at your option) any later version.
# 
# 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, see <http://www.gnu.org/licenses/>.
# 
#************************************************************************************

. /ventoy/hook/ventoy-hook-lib.sh

if is_ventoy_hook_finished; then
    exit 0
fi

vtlog "####### $0 $* ########"

VTPATH_OLD=$PATH; PATH=$BUSYBOX_PATH:$VTOY_PATH/tool:$PATH

longpanda's avatar
longpanda committed
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

check_insmod() {
    if [ -f "$1" ]; then
        vtlog "insmod $1"
        insmod "$1" >> $VTOY_PATH/log 2>&1
    else
        vtlog "$1 not exist"
    fi
}

wrt_insmod() {
    kbit=$1
    kv=$(uname -r)
    
    vtlog "insmod $kv $kbit"
    
    check_insmod /ventoy_openwrt/$kv/$kbit/dax.ko
    check_insmod /ventoy_openwrt/$kv/$kbit/dm-mod.ko    
}

insmod_dm_mod() {
    if grep -q "device-mapper" /proc/devices; then
        vtlog "device-mapper enabled by system 0"
        return
    fi
    
    check_insmod /ventoy/modules/dax.ko
    check_insmod /ventoy/modules/dm-mod.ko

    if grep -q "device-mapper" /proc/devices; then
        vtlog "device-mapper enabled by system 1"
        return
    fi
    
    if [ -f /ventoy_openwrt.xz ]; then
        tar xf /ventoy_openwrt.xz -C /
        rm -f  /ventoy_openwrt.xz
    fi

    if uname -m | egrep -q "amd64|x86_64"; then
        wrt_insmod 64
    else
        wrt_insmod generic    
        if lsmod | grep -q 'dm-mod'; then
            vterr "insmod generic failed"
        else
            wrt_insmod legacy
        fi
    fi
}

insmod_dm_mod

longpanda's avatar
longpanda committed
83
for i in $(ls /sys/class/block/); do
longpanda's avatar
longpanda committed
84
    if ! [ -e /dev/$i ]; then
longpanda's avatar
longpanda committed
85
        blkdev_num=$(sed 's/:/ /g' /sys/class/block/$i/dev)
longpanda's avatar
longpanda committed
86
87
88
89
90
        vtlog "mknod -m 0666 /dev/$i b $blkdev_num"
        mknod -m 0666 /dev/$i b $blkdev_num
    fi
done

longpanda's avatar
update  
longpanda committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
wait_for_usb_disk_ready

vtdiskname=$(get_ventoy_disk_name)
if [ "$vtdiskname" = "unknown" ]; then
    vtlog "ventoy disk not found"
    PATH=$VTPATH_OLD
    exit 0
fi

ventoy_udev_disk_common_hook "${vtdiskname#/dev/}2" "noreplace"

blkdev_num=$($VTOY_PATH/tool/dmsetup ls | grep ventoy | sed 's/.*(\([0-9][0-9]*\),.*\([0-9][0-9]*\).*/\1:\2/')
vtDM=$(ventoy_find_dm_id ${blkdev_num})
echo -n $vtDM > /ventoy/vtDM

ventoy_create_dev_ventoy_part
mdev -s

longpanda's avatar
longpanda committed
109
110
111
mkdir /ventoy_rdroot
mount /dev/ventoy2 /ventoy_rdroot

longpanda's avatar
update  
longpanda committed
112
113
114
PATH=$VTPATH_OLD

set_ventoy_hook_finish