1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # Make USB key to save/restore HW configuration on a ProLiant DL 680 G7
|
---|
4 | #
|
---|
5 | #set -x
|
---|
6 | #
|
---|
7 | # Usage: ./mkusbkey.sh <device>
|
---|
8 | #
|
---|
9 | # <device> usually points to a USB pendrive (i.e. /dev/sdb) but it can also
|
---|
10 | # be a /dev/loop* device attached to a standard file
|
---|
11 | # $ dd if=/dev/zero of=/tmp/usbdev.img bs=1M count=400
|
---|
12 | # $ losetup -vf /tmp/usbdev.img
|
---|
13 | #
|
---|
14 | # Will associate usbdev.img to /dev/loop0 in most cases
|
---|
15 | # $ ./mkusbkey.sh /dev/loop0
|
---|
16 | #
|
---|
17 | # will create the bootable USB in usbdev.img file which can be dd'ed on a
|
---|
18 | # USB pendrive
|
---|
19 |
|
---|
20 | # Partition size in MB that will be created on the device
|
---|
21 | [ -f '@sysconfdir@/setupkey.conf' ] && . '@sysconfdir@/setupkey.conf'
|
---|
22 |
|
---|
23 | [ -z "$PARTITION_SIZE" ] && PARTITION_SIZE=400
|
---|
24 |
|
---|
25 | if [ ! -f "$DATA/ext3fs.img" ]; then
|
---|
26 | echo "Cannot find the ext3fs.img file system image"
|
---|
27 | echo "Please run 'rpmbootstrap.sh' script before running $0"
|
---|
28 | exit 1
|
---|
29 | fi
|
---|
30 |
|
---|
31 | if [ ! -f "$DATA/initramfs.img" ]; then
|
---|
32 | echo "Cannot find the initramfs.img initial RAM disk image."
|
---|
33 | echo "Please run 'rpmbootstrap.sh' script before running $0"
|
---|
34 | exit 1
|
---|
35 | fi
|
---|
36 |
|
---|
37 | if [ ! -f "$DATA/vmlinuz" ]; then
|
---|
38 | echo "Cannot find the boot kernel."
|
---|
39 | echo "Please run 'rpmbootstrap.sh' script before running $0"
|
---|
40 | exit 1
|
---|
41 | fi
|
---|
42 |
|
---|
43 | MBR=/usr/share/syslinux/mbr.bin
|
---|
44 |
|
---|
45 | if [ ! -f "$MBR" ]; then
|
---|
46 | echo "Outch! Cannot find $MBR. That should not happen!"
|
---|
47 | echo "Check 'syslinux' is installed, find where the 'mbr.bin' file is located and update the \$MBR variable in $0 accordingly."
|
---|
48 | exit 2
|
---|
49 | fi
|
---|
50 |
|
---|
51 | sync
|
---|
52 | DRIVE=$1
|
---|
53 | [ -z $DRIVE ] && echo "Usage: $0 device." && exit -1
|
---|
54 | [ ! -b $DRIVE ] && echo "$1 is not a block device. Exiting." && exit -1
|
---|
55 | KPARTX=`which kpartx`
|
---|
56 | [ $? -ne 0 ] && echo "kpartx is missing. exiting" && exit -1
|
---|
57 | SYSLINUX=`which syslinux`
|
---|
58 | [ $? -ne 0 ] && echo "syslinux is missing. exiting" && exit -1
|
---|
59 |
|
---|
60 |
|
---|
61 | [[ $DRIVE == /dev/sda* ]] && echo "For security, the script does not work on /dev/sda* devices" && exit -1
|
---|
62 |
|
---|
63 | [[ `cat /proc/mounts | grep $DRIVE` != "" ]] && echo "$DRIVE is mounted. Exit now." && exit -1
|
---|
64 |
|
---|
65 | # Wipe the partition table.
|
---|
66 | dd if=/dev/zero of=$DRIVE bs=4096 count=1 > /dev/null 2>&1
|
---|
67 | [ $? -ne 0 ] && echo "WARNING: Unable to erase MBR on $DRIVE."
|
---|
68 | sync
|
---|
69 |
|
---|
70 | # Create partition
|
---|
71 | echo "Creating the partition on $DRIVE"
|
---|
72 | sfdisk -uM -q $DRIVE 2>/dev/null << EOF
|
---|
73 | 0,$PARTITION_SIZE,b,*
|
---|
74 | EOF
|
---|
75 | sync
|
---|
76 | [ $? -ne 0 ] && echo "Unable to partition the key $DRIVE. Exiting" && exit -1
|
---|
77 | sfdisk -q -R $DRIVE 2>/dev/null
|
---|
78 |
|
---|
79 | sync
|
---|
80 | if [ ! "${DRIVE##/dev/loop*}" = "$DRIVE" ]; then
|
---|
81 | echo "Loopback device detected. Special handling required"
|
---|
82 | $KPARTX -d $DRIVE
|
---|
83 | $KPARTX -a $DRIVE
|
---|
84 | PART="/dev/mapper/${DRIVE#/dev/}p1"
|
---|
85 | else
|
---|
86 | PART=${DRIVE}1
|
---|
87 | fi
|
---|
88 |
|
---|
89 | echo "Creating the master boot record on $DRIVE"
|
---|
90 | dd if=$MBR of=$DRIVE > /dev/null 2>&1
|
---|
91 | [ $? -ne 0 ] && echo "Unable to copy MBR on $DRIVE. Please check the key. Exiting" && exit -1
|
---|
92 |
|
---|
93 | # FAT32 filesystem labeled 'LiveUSB'
|
---|
94 | sync
|
---|
95 | echo "Creating FAT filesystem on $PART"
|
---|
96 | mkfs.vfat -n LiveUSB $PART > /dev/null
|
---|
97 | [ $? -ne 0 ] && echo "Unable to format the partition $PART as VFAT. Exiting" && exit -1
|
---|
98 | sync
|
---|
99 |
|
---|
100 | TMPDIR=`mktemp -d /tmp/mkusb.XXXX`
|
---|
101 | if [ ! -d "$TMPDIR" ]; then
|
---|
102 | echo "$TMPDIR is not a directory. Exiting"
|
---|
103 | exit -1
|
---|
104 | else
|
---|
105 | if [[ "$TMPDIR" == "/" ]]; then
|
---|
106 | echo "$TMPDIR is /. Exiting"
|
---|
107 | exit -1
|
---|
108 | fi
|
---|
109 | fi
|
---|
110 |
|
---|
111 | ret=0
|
---|
112 | mount $PART $TMPDIR
|
---|
113 | if [ $? -ne 0 ] ; then
|
---|
114 | echo "Unable to mount $PART under $TMPDIR. Exiting"
|
---|
115 | ret=1
|
---|
116 | else
|
---|
117 | # The SQUASHFS containing the EXT3 filesystem with the
|
---|
118 | # minimum OS and the SSTK tools
|
---|
119 | SQUASHFS=$(mktemp -d /tmp/squashfs.XXXX)
|
---|
120 | mkdir $SQUASHFS/LiveOS
|
---|
121 | dd if=$DATA/ext3fs.img of=$SQUASHFS/LiveOS/ext3fs.img > /dev/null 2>&1
|
---|
122 | [ $? -ne 0 ] && echo "Unable to copy the squashfs FS. Exiting" && exit -1
|
---|
123 | mkdir $TMPDIR/syslinux $TMPDIR/LiveOS $TMPDIR/data_files $TMPDIR/config $TMPDIR/fw_files $TMPDIR/log
|
---|
124 |
|
---|
125 | echo "Copying system files..."
|
---|
126 | $SYSLINUX -i -d syslinux $PART
|
---|
127 | if [ $? -ne 0 ]; then
|
---|
128 | echo "Unable to syslinux $PART. Exiting"
|
---|
129 | ret=1
|
---|
130 | else
|
---|
131 | echo $VERSION > $TMPDIR/.version
|
---|
132 | cp $DATA/vmlinuz $DATA/initramfs.img $DATA/syslinux.cfg $TMPDIR/syslinux
|
---|
133 | cp $DATA/fw_files/* $TMPDIR/fw_files
|
---|
134 | cp $DATA/data_files/* $TMPDIR/data_files
|
---|
135 | cp $DATA/config/* $TMPDIR/config
|
---|
136 | mksquashfs $SQUASHFS $TMPDIR/LiveOS/squashfs.img
|
---|
137 | if [ $? -ne 0 ]; then
|
---|
138 | echo "Unable to recreate the squash FS $SQUASHFS. Exiting"
|
---|
139 | ret=1
|
---|
140 | fi
|
---|
141 | fi
|
---|
142 | rm -rf $SQUASHFS
|
---|
143 | fi
|
---|
144 | if [ $ret -eq 1 ]; then
|
---|
145 | umount $TMPDIR
|
---|
146 | exit $ret
|
---|
147 | fi
|
---|
148 |
|
---|
149 | echo "Done"
|
---|
150 |
|
---|
151 | echo "Unmounting file system. It may take some time to flush the buffers..."
|
---|
152 | umount $TMPDIR
|
---|
153 |
|
---|
154 | sync
|
---|
155 |
|
---|
156 | if [ ! "${PART##/dev/mapper/*}" = "$PART" ]; then
|
---|
157 | echo "Releasing loopback device"
|
---|
158 | $KPARTX -d $DRIVE
|
---|
159 | fi
|
---|
160 |
|
---|
161 | rm -rf $TMPDIR
|
---|
162 |
|
---|
163 | exit 0
|
---|