Ticket #681: test.sh

File test.sh, 2.5 KB (added by victor gattegno, 11 years ago)

shell-script that use the old detection mode

Line 
1#!/bin/bash
2# Scan vmlinuz and detect fs
3# Inspired by mindi
4# 20130429
5
6Die(){
7echo $1
8exit -1
9}
10
11detection(){
12echo -e "--- Kernel fs detection routine\n"
13# local constants (filesystem magic strings)
14local lcMagicCramfs="<3>cramfs: wrong magic"
15local lcMagicExt2fs="EXT2-fs: blocksize too small for device."
16local lcMagicExt3fs="<3>EXT3-fs: blocksize too small for journal device."
17local lcMagicInitfs="<6>checking if image is initramfs...|<6>Unpacking initramfs...|<6>Trying to unpack rootfs image as initramfs"
18
19echo "`cat /etc/*release`"
20lvKernelImage=/boot/vmlinuz-`uname -r`
21echo -e "Kernel is $lvKernelImage\n"
22
23 # verify that file exists
24 [ ! -f $lvKernelImage ] && Die "File $lvKernelImage not found. Terminating."
25
26 # Kernel may be gzip compressed
27 file $lvKernelImage 2>&1 | grep -q gzip
28 if [ $? -eq 0 ]; then
29 lvScanRes=`gzip -cd $lvKernelImage | strings | grep -E "$lcMagicCramfs|$lcMagicExt2fs|$lcMagicExt3fs|$lcMagicInitfs"`
30 echo -e "\n$lvKernelImage is a gzipped file\n"
31 else
32 echo -e "\n$lvKernelImage is not a gzipped file"
33 echo "`file $lvKernelImage`"
34 echo -e "Scan $lvKernelImage for gzip embedded and initrd filessystem support\n"
35 # get offet of gzip magic "1f8b0800" in file
36 lvOffset=`od -vA n -t x1 $lvKernelImage | tr -d '[:space:]' | awk '{ print match($0, "1f8b0800")}'`
37 [ $lvOffset -eq 0 ] && Die "gzip magic not found in file $lvKernelImage. Terminating."
38 lvOffset=`expr $lvOffset / 2`
39 echo " GetInitrdFilesystemToUse(): gzip magic found at lvOffset $lvOffset."
40
41 # scan kernel image for initrd filessystem support
42 lvScanRes=`dd ibs=1 skip=$lvOffset if=$lvKernelImage obs=1M 2>/dev/null | gunzip -c 2> /dev/null | strings | grep -E "$lcMagicCramfs|$lcMagicExt2fs|$lcMagicExt3fs|$lcMagicInitfs"`
43 fi
44 echo -e "\nHere are the fs detected :"
45 echo -e "lvScanRes=$lvScanRes\n"
46
47 # determine which filesystem to use for initrd image: ext2fs, gzip'ed cpio (initramfs ) or cramfs
48 if [ `echo $lvScanRes | grep -Ec "$lcMagicExt2fs"` -eq 1 ]; then
49 lvUseFilesystem="ext2fs"
50 elif [ `echo $lvScanRes | grep -Ec "$lcMagicExt3fs"` -eq 1 ]; then
51 lvUseFilesystem="ext3fs"
52 elif [ `echo $lvScanRes | grep -Ec "$lcMagicInitfs"` -eq 1 ]; then
53 lvUseFilesystem="initramfs"
54 elif [ `echo $lvScanRes | grep -Ec "$lcMagicCramfs"` -eq 1 ]; then
55 lvUseFilesystem="cramfs"
56 else
57 # In that case, we are after 2.6.30 and use the supported initramfs
58 echo "In that case, we are after 2.6.30 and use the supported initramfs"
59 lvUseFilesystem="initramfs"
60 fi
61 echo -e "lvUseFilesystem=$lvUseFilesystem\n"
62}
63
64echo -e "--- Main"
65detection