]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - IMG/cpio/ventoy/loop/openwrt/ventoy-disk.sh
Optimization for booting OpenWrt
[Ventoy.git] / IMG / cpio / ventoy / loop / openwrt / ventoy-disk.sh
1 #!/ventoy/busybox/sh
2 #************************************************************************************
3 # Copyright (c) 2020, longpanda <admin@ventoy.net>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #
18 #************************************************************************************
19
20 . /ventoy/hook/ventoy-hook-lib.sh
21
22 if is_ventoy_hook_finished; then
23 exit 0
24 fi
25
26 vtlog "####### $0 $* ########"
27
28 VTPATH_OLD=$PATH; PATH=$BUSYBOX_PATH:$VTOY_PATH/tool:$PATH
29
30
31 check_insmod() {
32 if [ -f "$1" ]; then
33 vtlog "insmod $1"
34 insmod "$1" >> $VTOY_PATH/log 2>&1
35 else
36 vtlog "$1 not exist"
37 fi
38 }
39
40 wrt_insmod() {
41 kbit=$1
42 kv=$(uname -r)
43
44 vtlog "insmod $kv $kbit"
45
46 check_insmod /ventoy_openwrt/$kv/$kbit/dax.ko
47 check_insmod /ventoy_openwrt/$kv/$kbit/dm-mod.ko
48 }
49
50 insmod_dm_mod() {
51 if grep -q "device-mapper" /proc/devices; then
52 vtlog "device-mapper enabled by system 0"
53 return
54 fi
55
56 check_insmod /ventoy/modules/dax.ko
57 check_insmod /ventoy/modules/dm-mod.ko
58
59 if grep -q "device-mapper" /proc/devices; then
60 vtlog "device-mapper enabled by system 1"
61 return
62 fi
63
64 if [ -f /ventoy_openwrt.xz ]; then
65 tar xf /ventoy_openwrt.xz -C /
66 rm -f /ventoy_openwrt.xz
67 fi
68
69 if uname -m | egrep -q "amd64|x86_64"; then
70 wrt_insmod 64
71 else
72 wrt_insmod generic
73 if lsmod | grep -q 'dm-mod'; then
74 vterr "insmod generic failed"
75 else
76 wrt_insmod legacy
77 fi
78 fi
79 }
80
81 insmod_dm_mod
82
83 for i in $(ls /sys/class/block/); do
84 if ! [ -e /dev/$i ]; then
85 blkdev_num=$(sed 's/:/ /g' /sys/class/block/$i/dev)
86 vtlog "mknod -m 0666 /dev/$i b $blkdev_num"
87 mknod -m 0666 /dev/$i b $blkdev_num
88 fi
89 done
90
91 wait_for_usb_disk_ready
92
93 vtdiskname=$(get_ventoy_disk_name)
94 if [ "$vtdiskname" = "unknown" ]; then
95 vtlog "ventoy disk not found"
96 PATH=$VTPATH_OLD
97 exit 0
98 fi
99
100 ventoy_udev_disk_common_hook "${vtdiskname#/dev/}2" "noreplace"
101
102 blkdev_num=$($VTOY_PATH/tool/dmsetup ls | grep ventoy | sed 's/.*(\([0-9][0-9]*\),.*\([0-9][0-9]*\).*/\1:\2/')
103 vtDM=$(ventoy_find_dm_id ${blkdev_num})
104 echo -n $vtDM > /ventoy/vtDM
105
106 ventoy_create_dev_ventoy_part
107 mdev -s
108
109 mkdir /ventoy_rdroot
110 mount /dev/ventoy2 /ventoy_rdroot
111
112 PATH=$VTPATH_OLD
113
114 set_ventoy_hook_finish