source: ProjectBuilder/projects/proliantusbkey/0.9.6/mkusbkey.in@ 1436

Last change on this file since 1436 was 1436, checked in by Bruno Cornec, 12 years ago
  • Upload the 0.9.6 version of the PUSK (ProLiant USB Setup Key)
File size: 4.4 KB
Line 
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
25if [ ! -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
29fi
30
31if [ ! -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
35fi
36
37if [ ! -f "$DATA/vmlinuz" ]; then
38 echo "Cannot find the boot kernel."
39 echo "Please run 'rpmbootstrap.sh' script before running $0"
40 exit 1
41fi
42
43MBR=/usr/share/syslinux/mbr.bin
44
45if [ ! -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
49fi
50
51sync
52DRIVE=$1
53[ -z $DRIVE ] && echo "Usage: $0 device." && exit -1
54[ ! -b $DRIVE ] && echo "$1 is not a block device. Exiting." && exit -1
55KPARTX=`which kpartx`
56[ $? -ne 0 ] && echo "kpartx is missing. exiting" && exit -1
57SYSLINUX=`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.
66dd if=/dev/zero of=$DRIVE bs=4096 count=1 > /dev/null 2>&1
67[ $? -ne 0 ] && echo "WARNING: Unable to erase MBR on $DRIVE."
68sync
69
70# Create partition
71echo "Creating the partition on $DRIVE"
72sfdisk -uM -q $DRIVE 2>/dev/null << EOF
730,$PARTITION_SIZE,b,*
74EOF
75sync
76[ $? -ne 0 ] && echo "Unable to partition the key $DRIVE. Exiting" && exit -1
77sfdisk -q -R $DRIVE 2>/dev/null
78
79sync
80if [ ! "${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"
85else
86 PART=${DRIVE}1
87fi
88
89echo "Creating the master boot record on $DRIVE"
90dd 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'
94sync
95echo "Creating FAT filesystem on $PART"
96mkfs.vfat -n LiveUSB $PART > /dev/null
97[ $? -ne 0 ] && echo "Unable to format the partition $PART as VFAT. Exiting" && exit -1
98sync
99
100TMPDIR=`mktemp -d /tmp/mkusb.XXXX`
101if [ ! -d "$TMPDIR" ]; then
102 echo "$TMPDIR is not a directory. Exiting"
103 exit -1
104else
105 if [[ "$TMPDIR" == "/" ]]; then
106 echo "$TMPDIR is /. Exiting"
107 exit -1
108 fi
109fi
110
111ret=0
112mount $PART $TMPDIR
113if [ $? -ne 0 ] ; then
114 echo "Unable to mount $PART under $TMPDIR. Exiting"
115 ret=1
116else
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
143fi
144if [ $ret -eq 1 ]; then
145 umount $TMPDIR
146 exit $ret
147fi
148
149echo "Done"
150
151echo "Unmounting file system. It may take some time to flush the buffers..."
152umount $TMPDIR
153
154sync
155
156if [ ! "${PART##/dev/mapper/*}" = "$PART" ]; then
157 echo "Releasing loopback device"
158 $KPARTX -d $DRIVE
159fi
160
161rm -rf $TMPDIR
162
163exit 0
Note: See TracBrowser for help on using the repository browser.