2 #************************************************************************************
3 # Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
18 #************************************************************************************
21 BUSYBOX_PATH
=$VTOY_PATH/busybox
23 FIND
=$BUSYBOX_PATH/find
24 GREP
=$BUSYBOX_PATH/grep
25 EGREP
=$BUSYBOX_PATH/egrep
29 SLEEP
=$BUSYBOX_PATH/sleep
30 HEAD
=$BUSYBOX_PATH/head
31 VTOY_DM_PATH
=/dev
/mapper
/ventoy
32 VTOY_DEBUG_LEVEL
=$($BUSYBOX_PATH/hexdump -n 1 -s 450 -e '1/1 "%02x"' $VTOY_PATH/ventoy_os_param)
34 if [ "$VTOY_DEBUG_LEVEL" = "01" ]; then
35 if [ -e /dev
/console
]; then
41 if [ "$VTLOG" = "$VTOY_PATH/log" ]; then
44 echo -e "\033[32m $* \033[0m" > $VTLOG
50 if [ "$VTLOG" = "$VTOY_PATH/log" ]; then
53 echo -e "\n\033[31m $* \033[0m" > $VTLOG
59 is_ventoy_hook_finished
() {
60 [ -e $VTOY_PATH/hook_finish
]
63 set_ventoy_hook_finish
() {
64 echo 'Y' > $VTOY_PATH/hook_finish
67 get_ventoy_disk_name
() {
68 line
=$($VTOY_PATH/tool/vtoydump -f /ventoy/ventoy_os_param)
76 get_ventoy_iso_name
() {
77 line
=$($VTOY_PATH/tool/vtoydump -f /ventoy/ventoy_os_param)
85 wait_for_usb_disk_ready
() {
87 usb_disk
=$(get_ventoy_disk_name)
88 vtlog
"wait_for_usb_disk_ready $usb_disk ..."
90 if [ -e "${usb_disk}2" ]; then
91 vtlog
"wait_for_usb_disk_ready $usb_disk finish"
100 if $VTOY_PATH/tool
/vtoydump
-f $VTOY_PATH/ventoy_os_param
-c "$1"; then
108 if $VTOY_PATH/tool
/vtoydump
-f $VTOY_PATH/ventoy_os_param
-c "$1"; then
115 ventoy_get_vblade_bin
() {
116 if $VTOY_PATH/tool
/vblade_64
-t >>$VTLOG 2>&1; then
117 echo $VTOY_PATH/tool
/vblade_64
119 echo $VTOY_PATH/tool
/vblade_32
123 ventoy_find_bin_path
() {
124 if $BUSYBOX_PATH/which "$1" > /dev
/null
; then
125 $BUSYBOX_PATH/which "$1"; return
128 for vt_path
in '/bin' '/sbin' '/usr/bin' '/usr/sbin' '/usr/local/bin' '/usr/local/sbin' '/root/bin'; do
129 if [ -e "$vt_path/$1" ]; then
130 echo "$vt_path/$1"; return
138 ventoy_find_bin_run
() {
140 if [ "$1" = "sudo" ]; then
145 vtbinpath
=$(ventoy_find_bin_path "$1")
146 if [ -n "$vtbinpath" ]; then
149 if [ $vtsudo -eq 0 ]; then
150 vtlog
"$vtbinpath $*"
153 vtlog
"sudo $vtbinpath $*"
159 ventoy_get_module_postfix
() {
160 vtKerVer
=$($BUSYBOX_PATH/uname -r)
161 vtLine
=$($FIND /lib/modules/$vtKerVer/ -name *.ko* | $HEAD -n1)
162 vtComp
=${vtLine##*/*.ko}
166 ventoy_check_dm_module
() {
167 if $GREP -q 'device-mapper' /proc
/devices
; then
168 $BUSYBOX_PATH/true
; return
171 vtlog
"device-mapper NOT found in /proc/devices, try to load kernel module"
172 $BUSYBOX_PATH/modprobe dm_mod
>>$VTLOG 2>&1
173 $BUSYBOX_PATH/modprobe dm
-mod >>$VTLOG 2>&1
175 if ! $GREP -q 'device-mapper' /proc
/devices
; then
176 vtlog
"modprobe failed, now try to insmod ko..."
178 $FIND /lib
/modules
/ -name "dm-mod.ko*" | while read vtline
; do
179 vtlog
"insmode $vtline "
180 $BUSYBOX_PATH/insmod
$vtline >>$VTLOG 2>&1
184 if $GREP -q 'device-mapper' /proc
/devices
; then
185 vtlog
"device-mapper found in /proc/devices after retry"
186 $BUSYBOX_PATH/true
; return
188 vtlog
"device-mapper still NOT found in /proc/devices after retry"
189 $BUSYBOX_PATH/false
; return
193 create_ventoy_device_mapper
() {
194 vtlog
"create_ventoy_device_mapper $*"
196 VT_DM_BIN
=$(ventoy_find_bin_path dmsetup)
197 if [ -z "$VT_DM_BIN" ]; then
198 vtlog
"no dmsetup avaliable, lastly try inbox dmsetup"
199 VT_DM_BIN
=$VTOY_PATH/tool
/dmsetup
202 vtlog
"dmsetup avaliable in system $VT_DM_BIN"
204 if ventoy_check_dm_module
"$1"; then
205 vtlog
"device-mapper module check success"
207 vterr
"Error: no dm module avaliable"
210 $VTOY_PATH/tool
/vtoydm
-p -f $VTOY_PATH/ventoy_image_map
-d $1 > $VTOY_PATH/ventoy_dm_table
212 $VT_DM_BIN create ventoy
$VTOY_PATH/ventoy_dm_table
>>$VTLOG 2>&1
214 $VT_DM_BIN "$2" create ventoy
$VTOY_PATH/ventoy_dm_table
>>$VTLOG 2>&1
218 wait_for_ventoy_dm_disk_label
() {
219 DM
=$($BUSYBOX_PATH/readlink $VTOY_DM_PATH)
220 vtlog
"wait_for_ventoy_dm_disk_label $DM ..."
222 for i
in 0 1 2 3 4 5 6 7 8 9; do
223 vtlog
"i=$i ####### ls /dev/disk/by-label/"
224 ls -l /dev
/disk
/by
-label/ >> $VTLOG
226 if ls -l /dev
/disk
/by
-label/ | $GREP -q "$DM"; then
235 if ! [ -e "$1" ]; then
240 if [ -d /tmp
/vtoy_udeb
]; then
241 $BUSYBOX_PATH/rm -rf /tmp
/vtoy_udeb
244 $BUSYBOX_PATH/mkdir -p /tmp
/vtoy_udeb
245 $BUSYBOX_PATH/cp -a "$1" /tmp
/vtoy_udeb
/
247 CURDIR
=$($BUSYBOX_PATH/pwd)
250 $BUSYBOX_PATH/ar x
"$1"
252 if [ -e 'data.tar.gz' ]; then
253 $BUSYBOX_PATH/tar -xzf data.
tar.gz
-C /
254 elif [ -e 'data.tar.xz' ]; then
255 $BUSYBOX_PATH/tar -xJf data.
tar.xz
-C /
256 elif [ -e 'data.tar.bz2' ]; then
257 $BUSYBOX_PATH/tar -xjf data.
tar.bz2
-C /
258 elif [ -e 'data.tar.lzma' ]; then
259 $BUSYBOX_PATH/tar -xaf data.
tar.lzma
-C /
262 if [ -e 'control.tar.gz' ]; then
263 $BUSYBOX_PATH/tar -xzf control.
tar.gz
-C /
264 elif [ -e 'control.tar.xz' ]; then
265 $BUSYBOX_PATH/tar -xJf control.
tar.xz
-C /
266 elif [ -e 'control.tar.bz2' ]; then
267 $BUSYBOX_PATH/tar -xjf control.
tar.bz2
-C /
268 elif [ -e 'control.tar.lzma' ]; then
269 $BUSYBOX_PATH/tar -xaf control.
tar.lzma
-C /
273 $BUSYBOX_PATH/rm -rf /tmp
/vtoy_udeb
278 install_udeb_from_line
() {
279 vtlog
"install_udeb_from_line $1"
281 if ! [ -b "$2" ]; then
282 vterr
"disk #$2# not exist"
286 sector
=$(echo $1 | $AWK '{print $(NF-1)}')
287 length=$(echo $1 | $AWK '{print $NF}')
288 vtlog "sector=$sector length=$length"
290 $VTOY_PATH/tool/vtoydm -e -f $VTOY_PATH/ventoy_image_map -d ${2} -s $sector -l $length -o /tmp/xxx.udeb
291 if [ -e /tmp/xxx.udeb ]; then
292 vtlog "extract udeb file from iso success"
294 vterr "extract udeb file from iso fail"
298 install_udeb_pkg /tmp/xxx.udeb
299 $BUSYBOX_PATH/rm -f /tmp/xxx.udeb
302 extract_file_from_line() {
303 vtlog "extract_file_from_line $1 disk=#$2#"
304 if ! [ -b "$2" ]; then
305 vterr "disk #$2# not exist"
309 sector=$(echo $1 | $AWK '{print $(NF-1)}')
310 length
=$(echo $1 | $AWK '{print $NF}')
311 vtlog
"sector=$sector length=$length"
313 $VTOY_PATH/tool
/vtoydm
-e -f $VTOY_PATH/ventoy_image_map
-d ${2} -s $sector -l $length -o $3
315 vtlog
"extract file from iso success"
318 vterr
"extract file from iso fail"
323 install_rpm_from_line
() {
324 vtlog
"install_rpm_from_line $1 disk=#$2#"
326 if ! [ -b "$2" ]; then
327 vterr
"disk #$2# not exist"
331 sector
=$(echo $1 | $AWK '{print $(NF-1)}')
332 length=$(echo $1 | $AWK '{print $NF}')
333 vtlog "sector=$sector length=$length"
335 $VTOY_PATH/tool/vtoydm -e -f $VTOY_PATH/ventoy_image_map -d ${2} -s $sector -l $length -o /tmp/xxx.rpm
336 if [ -e /tmp/xxx.rpm ]; then
337 vtlog "extract rpm file from iso success"
339 vterr "extract rpm file from iso fail"
343 CURPWD=$($BUSYBOX_PATH/pwd)
346 vtlog "install rpm..."
347 $BUSYBOX_PATH/rpm2cpio /tmp/xxx.rpm | $BUSYBOX_PATH/cpio -idm 2>>$VTLOG
350 $BUSYBOX_PATH/rm -f /tmp/xxx.rpm
353 dump_whole_iso_file() {
354 $VTOY_PATH/tool/vtoydm -p -f $VTOY_PATH/ventoy_image_map -d $usb_disk | while read vtline; do
355 vtlog "dmtable line: $vtline"
356 vtcount=$(echo $vtline | $AWK '{print $2}')
357 vtoffset=$(echo $vtline | $AWK '{print $NF}')
358 $BUSYBOX_PATH/dd if=$usb_disk of="$1" bs=512 count=$vtcount skip=$vtoffset oflag=append conv=notrunc
362 ventoy_copy_device_mapper() {
363 if [ -L $VTOY_DM_PATH ]; then
364 vtlog "replace block device link $1..."
365 $BUSYBOX_PATH/mv "$1" $VTOY_PATH/dev_backup_${1#/dev/}
366 VT_MAPPER_LINK=$($BUSYBOX_PATH/readlink $VTOY_DM_PATH)
367 $BUSYBOX_PATH/cp -a "/dev/mapper/$VT_MAPPER_LINK" "$1"
368 elif [ -b $VTOY_DM_PATH ]; then
369 vtlog "replace block device $1..."
370 $BUSYBOX_PATH/mv "$1" $VTOY_PATH/dev_backup_${1#/dev/}
371 $BUSYBOX_PATH/cp -a "$VTOY_DM_PATH" "$1"
374 vtlog "$VTOY_DM_PATH not exist, now check /dev/dm-X ..."
375 VT_DM_BIN=$(ventoy_find_bin_path dmsetup)
376 if [ -z "$VT_DM_BIN" ]; then
377 vtlog "no dmsetup avaliable, lastly try inbox dmsetup"
378 VT_DM_BIN=$VTOY_PATH/tool/dmsetup
381 DM_VT_ID=$($VT_DM_BIN ls | $GREP ventoy | $SED 's/.*(\([0-9][0-9]*\),.*\([0-9][0-9]*\).*/\1 \2/')
382 vtlog
"DM_VT_ID=$DM_VT_ID ..."
383 $BUSYBOX_PATH/mv "$1" $VTOY_PATH/dev_backup_
${1#/dev/}
384 $BUSYBOX_PATH/mknod
-m 0666 "$1" b
$DM_VT_ID
388 ventoy_udev_disk_common_hook
() {
392 # create device mapper for iso image file
393 if create_ventoy_device_mapper
"/dev/$VTDISK" --readonly; then
394 vtlog
"==== create ventoy device mapper success ===="
396 vtlog
"==== create ventoy device mapper failed ===="
400 if $GREP -q "/dev/$VTDISK" /proc
/mounts
; then
401 $GREP "/dev/$VTDISK" /proc
/mounts
| while read vtLine
; do
402 vtPart
=$(echo $vtLine | $AWK '{print $1}')
403 vtMnt
=$(echo $vtLine | $AWK '{print $2}')
404 vtlog
"$vtPart is mounted on $vtMnt now umount it ..."
405 $BUSYBOX_PATH/umount
$vtMnt
409 if create_ventoy_device_mapper
"/dev/$VTDISK" --readonly; then
410 vtlog
"==== create ventoy device mapper success after retry ===="
412 vtlog
"==== create ventoy device mapper failed after retry ===="
417 if [ "$2" = "noreplace" ]; then
418 vtlog
"no need to replace block device"
420 ventoy_copy_device_mapper
"/dev/$1"
424 is_inotify_ventoy_part
() {
425 if echo $1 | grep -q "2$"; then
426 if ! [ -e /sys
/block
/$1 ]; then
427 if [ -e /sys
/class
/block
/$1 ]; then
428 if [ -e /dev
/${1:0:-1} ]; then
429 $VTOY_PATH/tool
/vtoydump
-f $VTOY_PATH/ventoy_os_param
-c ${1:0:-1}
439 ventoy_find_dm_id
() {
440 for vt
in $($BUSYBOX_PATH/ls /sys/block/); do
441 if [ "${vt:0:3}" = "dm-" ]; then
442 vtMajorMinor
=$($CAT /sys/block/$vt/dev)
443 if [ "$vtMajorMinor" = "$1" ]; then
452 ventoy_swap_device
() {
453 mv $1 $VTOY_PATH/swap_tmp_dev
455 mv $VTOY_PATH/swap_tmp_dev
$2