rootfsinstall: added many new options to customize rootfs target

This commit is contained in:
Silvan Calarco 2013-09-24 13:15:21 +02:00
parent 1c8cdbb131
commit 2f231a3c9b

View File

@ -6,12 +6,17 @@
SYSTEMFSTYPE=ext4 SYSTEMFSTYPE=ext4
function usage() { function usage() {
echo "Usage: $0 device rootfs-archive [-y] [-rpi] [-fs fstype]" echo "Usage: $0 device rootfs-archive [-y] [-rpi] [-fs fstype] [-a archive] [-c command] [-k kernel] [-p password]"
echo echo
echo "Options:" echo "Options:"
echo "-fs filesystem type for system partition (default: ext4)" echo "-a archive : additional archive to unpack in rootfs root"
echo "-rpi make a RaspberryPi SD with boot partition" echo "-c command : command to run through chroot in rootfs"
echo "-y assume yes to all questions (script mode)" echo "-fs fstype : filesystem type for system partition (default: ext4)"
echo "-h hostname : set given hostname"
echo "-k kernel : extract/use kernel file from ROOTFS/boot/ for boot"
echo "-p password : set given password for root user"
echo "-rpi : make a RaspberryPi SD with boot partition"
echo "-y : assume yes to all questions (script mode)"
echo echo
echo "Example: $0 /dev/sdb /tmp/openmamba-rootfs-light.arm.tar.xz" echo "Example: $0 /dev/sdb /tmp/openmamba-rootfs-light.arm.tar.xz"
exit 1 exit 1
@ -26,20 +31,59 @@ while [ "$1" ]; do
case $1 in case $1 in
-y) ASSUMEYES=1 ;; -y) ASSUMEYES=1 ;;
-rpi) RPIMODE=1 ;; -rpi) RPIMODE=1 ;;
-fs) [ $2 ] || { -fs) [ "$2" ] || {
echo "Error: -fs option requires an argument; aborting." echo "Error: -fs option requires an argument; aborting."
exit 1 exit 1
} }
SYSTEMFSTYPE=$2 SYSTEMFSTYPE=$2
shift shift
;; ;;
-a) [ "$2" ] || {
echo "Error: -a option requires an archive as argument; aborting."
exit 1
}
ADDITIONALARCHIVE=`realpath $2`
[ -r "$ADDITIONALARCHIVE" ] || {
echo "Error: file $ADDITIONALARCHIVE not found; aborting."
exit 1
}
shift
;;
-c) [ "$2" ] || {
echo "Error: -c option requires a command string as argument; aborting."
exit 1
}
CHROOTCOMMAND=$2
shift
;;
-k) [ "$2" ] || {
echo "Error: -k option requires a kernel file name as argument; aborting."
exit 1
}
USEKERNEL=$2
shift
;;
-h) [ "$2" ] || {
echo "Error: -h option requires a hostname as argument; aborting."
exit 1
}
SETHOSTNAME=$2
shift
;;
-p) [ "$2" ] || {
echo "Error: -p option requires a password as argument; aborting."
exit 1
}
ROOTPASSWORD=$2
shift
;;
-*) echo "Error: invalid option $1; aborting." -*) echo "Error: invalid option $1; aborting."
exit 1 exit 1
;; ;;
*) if [ ! "$DRIVE" ]; then *) if [ ! "$DRIVE" ]; then
DRIVE=$1 DRIVE=$1
elif [ ! "$ROOTFSARCHIVE" ]; then elif [ ! "$ROOTFSARCHIVE" ]; then
ROOTFSARCHIVE=$1 ROOTFSARCHIVE=`realpath $1`
else else
echo "Error: invalid argument $1; aborting." echo "Error: invalid argument $1; aborting."
exit 1 exit 1
@ -90,6 +134,7 @@ else
echo "ERROR: parted exited with error $? while creating system partition on ${DRIVE}; aborting." echo "ERROR: parted exited with error $? while creating system partition on ${DRIVE}; aborting."
fi fi
fi fi
sync
if [ "$RPIMODE" ]; then if [ "$RPIMODE" ]; then
echo "Formatting $PARTITION1 (vfat) ..." echo "Formatting $PARTITION1 (vfat) ..."
@ -117,15 +162,19 @@ mount $PARTITION2 $ROOTMP || {
echo "ERROR: unable to mount partition $PARTITION2; aborting." echo "ERROR: unable to mount partition $PARTITION2; aborting."
exit 1 exit 1
} }
trap "[ -e $ROOTMP ] && umount $ROOTMP;[ -e $BOOTMP ] && umount $BOOTMP" 0 HUP INT QUIT ABRT KILL TERM
if [ "$RPIMODE" ]; then if [ "$RPIMODE" ]; then
### SD-Card: install software ### SD-Card: install software
echo "Fetching firmware from git..." if [ ! "$USEKERNEL" ]; then
SWHOME=`mktemp -d` SWHOME=`mktemp -d`
echo "Cloning firmware repository from git..."
cd $SWHOME cd $SWHOME
git clone git://github.com/raspberrypi/firmware.git --depth=1 git clone git://github.com/raspberrypi/firmware.git --depth=1
else
echo "Fetching firmware boot files from git..."
fi
mount $PARTITION1 $BOOTMP || { mount $PARTITION1 $BOOTMP || {
echo "ERROR: unable to mount partition $PARTITION1; aborting." echo "ERROR: unable to mount partition $PARTITION1; aborting."
@ -137,7 +186,13 @@ if [ "$RPIMODE" ]; then
# start.elf : The GPU binary firmware image, provided by the foundation # start.elf : The GPU binary firmware image, provided by the foundation
# kernel.img : The OS kernel to load on the ARM processor. # kernel.img : The OS kernel to load on the ARM processor.
for f in bootcode.bin fixup_x.dat kernel.img start_x.elf LICENCE.broadcom; do for f in bootcode.bin fixup_x.dat kernel.img start_x.elf LICENCE.broadcom; do
if [ ! "$USEKERNEL" ]; then
cp $SWHOME/firmware/boot/$f $BOOTMP/ cp $SWHOME/firmware/boot/$f $BOOTMP/
else
curl -s -L -k https://github.com/raspberrypi/firmware/raw/master/boot/$f -o $BOOTMP/$f || {
echo "ERROR: could not fetch $f from raspberrypi git; aborting."
}
fi
done done
# cmdline.txt : parameters passed to the kernel on boot # cmdline.txt : parameters passed to the kernel on boot
@ -171,28 +226,88 @@ framebuffer_ignore_alpha=1" > $BOOTMP/config.txt
# vlls directory: Additional GPU code, e.g. extra codecs. # vlls directory: Additional GPU code, e.g. extra codecs.
# Not present in the initial release. # Not present in the initial release.
if [ ! "$USEKERNEL" ]; then
# copy kernel modules # copy kernel modules
mkdir -p $ROOTMP/lib/modules mkdir -p $ROOTMP/lib/modules
cp -a $SWHOME/firmware/modules/* $ROOTMP/lib/modules/ cp -a $SWHOME/firmware/modules/* $ROOTMP/lib/modules/
umount $PARTITION1 fi
rmdir $BOOTMP
[ "$SWHOME" -a "${SWHOME:0:4}" = "/tmp" ] && rm -rf $SWHOME [ "$SWHOME" -a "${SWHOME:0:4}" = "/tmp" ] && rm -rf $SWHOME
fi # rpimode fi # rpimode
(cd $ROOTMP >/dev/null || { (
cd $ROOTMP >/dev/null || {
echo "ERROR: unable to access system partition mount point; aborting." echo "ERROR: unable to access system partition mount point; aborting."
umount $PARTITION2 umount $PARTITION2
rmdir $ROOTMP rmdir $ROOTMP
exit 1 exit 1
} }
echo "Uncompressing $ROOTFSARCHIVE..." echo "Uncompressing $ROOTFSARCHIVE..."
echo "This can take a while, please wait..." echo -n "This can take a while, please wait..."
tar xf $ROOTFSARCHIVE tar xf $ROOTFSARCHIVE || {
echo
echo "ERROR: unable to uncompress rootfs archive; aborting."
exit 1
}
echo "DONE"
if [ "$ADDITIONALARCHIVE" ]; then
echo "Uncompressing $ADDITIONALARCHIVE..."
echo -n "This can take a while, please wait..."
tar xf $ADDITIONALARCHIVE || {
echo
echo "ERROR: unable to uncompress additional archive; aborting."
exit 1
}
echo "DONE"
fi
) )
[ $? -eq 0 ] || exit $?
if [ "$USEKERNEL" ]; then
if [ "$RPIMODE" ]; then
KERNELDEST=$BOOTMP/kernel.img
else
KERNELDEST=/tmp/$USEKERNEL
fi
if [ -r boot/$USEKERNEL ]; then
cp boot/$USEKERNEL $KERNELDEST
if [ -e /tmp/$USEKERNEL ]; then
echo "Extracted kernel available as $KERNELDEST"
else
echo "ERROR: unable to find or copy kernel $USEKERNEL; aborting."
exit 1
fi
fi
fi
if [ "$SETHOSTNAME" ]; then
echo "$SETHOSTNAME" > $ROOTMP/etc/hostname
sed -i "s|HOSTNAME=.*|HOSTNAME=$SETHOSTNAME|" $ROOTMP/etc/sysconfig/network
sed -i "s|\(127\.0\.0\.1\W*\)\(.*\)|\1 $SETHOSTNAME.localdomain $SETHOSTNAME localhost.localdomain localhost|" $ROOTMP/etc/hosts
fi
if [ "$ROOTPASSWORD" ]; then
echo "$ROOTPASSWORD" | chroot $ROOTMP passwd root --stdin || {
echo "ERROR: unable to set password for root; aborting."
exit 1
}
fi
if [ "$CHROOTCOMMAND" ]; then
chroot $ROOTMP $CHROOTCOMMAND || {
echo "ERROR: error running chroot command; aborting."
exit 1
}
fi
sync sync
if [ "$RPIMODE" ]; then
umount $PARTITION1
rmdir $BOOTMP
fi
umount $PARTITION2 umount $PARTITION2
rmdir $ROOTMP rmdir $ROOTMP