From: Michael Tremer Date: Sun, 7 Jun 2009 17:40:16 +0000 (+0200) Subject: Added functionality to mkinitramfs to fetch remote urls. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e79e1f306fa84980c28bc4f407eb90c42afc025e;p=ipfire-3.x.git Added functionality to mkinitramfs to fetch remote urls. It will support http, https, ftp, ftps, sftp and scp. --- diff --git a/src/mkinitramfs/functions b/src/mkinitramfs/functions index f470eff7a..b4f44ab9c 100644 --- a/src/mkinitramfs/functions +++ b/src/mkinitramfs/functions @@ -493,7 +493,8 @@ mkdir -p bin sbin dev sys proc sysroot \ etc/udev/rules.d lib/udev/rules.d # Install some essential binaries -install bash blkid chmod cat cut dd dmesg env grep head ip kbd_mode kill killall5 less ln \ +install bash blkid chmod cat cut dd dmesg env find grep head ip kbd_mode \ + kill killall5 less ln \ ls lsmod mkdir mknod modprobe mount mountpoint openvt pidof ps rm sed \ setfont sh sleep switchroot udevadm udevd umount \ /lib/udev/console_init diff --git a/src/mkinitramfs/mkliveramfs b/src/mkinitramfs/mkliveramfs index 479473c2e..44766e3d5 100644 --- a/src/mkinitramfs/mkliveramfs +++ b/src/mkinitramfs/mkliveramfs @@ -26,11 +26,13 @@ MODULES="$MODULES aufs squashfs loop vfat ehci-hcd ohci-hcd uhci-hcd usb-storage # NFS if [ "$WITH_NET" = "1" ]; then - MODULES="$MODULES nfs" + MODULES="$MODULES af_packet" for module in /lib/modules/$KERNEL/kernel/drivers/net/{,*/}*; do MODULES="$MODULES $(basename ${module/.ko})" done - install arping curl dhclient dhclient-script ping + install arping curl dhclient dhclient-script ping sha1sum /etc/nsswitch.conf \ + /usr/lib/libnsl.so /usr/lib/libnss_{dns,files}.so + mkdir -p $TMPDIR/var/lib/dhclient fi # Add all storage modules @@ -78,13 +80,14 @@ if [ -z $root ] ; then else case $root in /dev/disk/by-label/*) + type="block" LABEL=${root#/dev/disk/by-label/} echo "SUBSYSTEM==\"block\", PROGRAM=\"/sbin/blkid -s LABEL -o value %N\", RESULT==\"$LABEL\", SYMLINK+=\"root\"" > /etc/udev/rules.d/00-label.rules if is_verbose; then echo "Added udev rule 00-label.rules:" cat /etc/udev/rules.d/00-label.rules fi - thingtomount=/dev/root + type="block" ;; CDLABEL=*) CDLABEL=${root#CDLABEL=} @@ -96,7 +99,7 @@ else echo "Added udev rule 00-cdlabel.rules:" cat /etc/udev/rules.d/00-cdlabel.rules fi - thingtomount=/dev/root + type="block" ;; LABEL=*) LABEL=${root#LABEL=} @@ -105,7 +108,7 @@ else echo "Added udev rule 00-label.rules:" cat /etc/udev/rules.d/00-label.rules fi - thingtomount=/dev/root + type="block" ;; /dev/disk/by-id/*) UUID=${root#/dev/disk/by-id/} @@ -114,7 +117,7 @@ else echo "Added udev rule 01-uuid.rules:" cat /etc/udev/rules.d/01-uuid.rules fi - thingtomount=/dev/root + type="block" ;; UUID=*) UUID=${root#UUID=} @@ -123,18 +126,20 @@ else echo "Added udev rule 01-uuid.rules:" cat /etc/udev/rules.d/01-uuid.rules fi - thingtomount=/dev/root + type="block" ;; - NFS=*) - NFS=${root#NFS=} - vecho "Going on to mount $NFS" - #mount -t nfs $NFS /mnt/source - #rootfstype=loop - #thingtomount=$(ls -1 /mnt/source/*.iso | head -n 1) + http://*|https://*|ftp*://*|ftps://*|scp://*|sftp://*) + thingtofetch="${root}" + type="download" + + # When doing http(s) transfers, compress the data + if [ "${thingtofetch:0:4}" = "http" ]; then + curloptions="--compressed" + fi ;; /dev/*) ln -s $root /dev/root - thingtomount=$root + type="block" ;; *) thingtomount=$root @@ -142,6 +147,20 @@ else esac fi +if [ "$type" = "block" ]; then + thingtomount=/dev/root + +elif [ "$type" = "download" ]; then + rootflags="loop" + thingtomount=/${thingtofetch##*/} + + # If no network configuration was given, + # we'll try dhcp + if [ -z "${net}" ]; then + net="dhcp" + fi +fi + echo "udev_log=\"error\"" >> /etc/udev/udev.conf # rules for loading modules @@ -152,8 +171,10 @@ echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"0|7|14\", RUN+ echo "ACTION==\"add\", SUBSYSTEM==\"scsi_device\", SYSFS{type}==\"[45]\", RUN+=\"/sbin/modprobe sr_mod\"" >> /etc/udev/rules.d/10-modprobe.rules echo "SUBSYSTEM==\"mmc\", RUN+=\"/sbin/modprobe mmc_block\"" >> /etc/udev/rules.d/10-modprobe.rules -# FIXME: hack since sr_mod seems to fail to get loaded sometimes (#239657) -modprobe sr_mod +if [ "${type}" = "block" ]; then + # FIXME: hack since sr_mod seems to fail to get loaded sometimes (#239657) + modprobe sr_mod +fi modprobe loop max_loop=16 @@ -163,7 +184,7 @@ udevd --daemon vecho "Creating devices..." udevadm trigger -if [ -n "$net" ]; then +if [ "${type}" = "download" ]; then # Wait 30 seconds for network to appear. COUNTDOWN=30 while [ "x$COUNTDOWN" != "x0" ] ; do @@ -191,9 +212,38 @@ if [ -n "$net" ]; then vecho "Setting default gateway $gateway..." ip route add default via $gateway fi - if ! ping -c3 -w10 ping.ipfire.org 2>/dev/null; then - echo "This box does not seem to have an internet connection." - echo "You may fix this now and continue then:" + if [ -n "$dns" ]; then + vecho "Setting up dns server $dns..." + echo "nameserver $dns" > /etc/resolv.conf + fi + if [ -n "$gateway" ]; then + vecho "Pinging gateway $gateway..." + if ! ping -c3 -w10 $gateway &>/dev/null; then + echo "This box does not seem to have a connection" + echo "to the default gateway $gateway." + echo "You may fix this now and continue then:" + echo + bash + fi + fi +fi + +if [ "$type" = "download" ]; then + curloptions="${curloptions} --connect-timeout 30 --progress-bar --output ${thingtomount}" + #curloptions="${curloptions} --write-out=\"Fetched %{url_effective} in %{time_total}s (%{speed_download} bytes/s).\"" + + vecho "Running donwload..." + vecho " $thingtofetch" + + curl ${curloptions} ${thingtofetch} + + if [ "$?" != "0" ]; then + echo "An error occured when fetching ${thingtomount}:" + echo " Command: ${curl} ${thingtofetch}" + echo + echo "You now get a shell to download the file manually." + echo "Exit to continue booting." + echo bash fi fi @@ -204,26 +254,28 @@ if [ "$SHELL" == "1" ] ; then bash fi -# Wait 90 seconds for $thingtomount to appear. -COUNTDOWN=90 -while [ "x$COUNTDOWN" != "x0" ] ; do - is_verbose && echo -n "." - - if [ -e $thingtomount ]; then - COUNTDOWN=0 - continue - fi - # this is kind of lame, but we could have had a situation - # where we were unable to read the volume id. so trigger - # another run through the block devs - if [ "x$COUNTDOWN" = "x30" ]; then - udevadm trigger --subsystem-match=block - fi +if [ "${type}" = "block" ]; then + # Wait 90 seconds for $thingtomount to appear. + COUNTDOWN=90 + while [ "x$COUNTDOWN" != "x0" ] ; do + is_verbose && echo -n "." - COUNTDOWN=$(($COUNTDOWN - 1)) - sleep 1 -done -vecho # Blank line + if [ -e $thingtomount ]; then + COUNTDOWN=0 + continue + fi + # this is kind of lame, but we could have had a situation + # where we were unable to read the volume id. so trigger + # another run through the block devs + if [ "x$COUNTDOWN" = "x30" ]; then + udevadm trigger --subsystem-match=block + fi + + COUNTDOWN=$(($COUNTDOWN - 1)) + sleep 1 + done + vecho # Blank line +fi if [ ! -e $thingtomount ] ; then echo @@ -239,7 +291,7 @@ fi if is_verbose; then vecho "Mounting $thingtomount..." - ls -l $thingtomount + ls -lh $thingtomount fi if [ "x$READONLY" == "x1" ] ; then @@ -252,10 +304,11 @@ if [ -n $rootflags ]; then mountoptions=" -o$rootflags" fi -mount -n -t $rootfstype $mountoptions $thingtomount /mnt/source -RES=$? +[ -n "$rootfstype" ] && rootfstype="-t $rootfstype" + +mount -n $rootfstype $mountoptions $thingtomount /mnt/source -if [ "$RES" != "0" ]; then +if [ "$?" != "0" ]; then echo "---------------------------------" echo "WARNING: Cannot mount rootfs!" echo "---------------------------------" @@ -269,19 +322,22 @@ fi mount -n -t tmpfs none /mnt/tmpfs aufsmountoptions="br:/mnt/tmpfs=rw" -OVERLAY=$(ls /mnt/source/*.overlay 2>/dev/null || true) -if [ -n "$OVERLAY" ]; then - vecho "Setting up overlay for squashfs..." - mount -t squashfs -o loop,ro $OVERLAY /mnt/overlayfs - aufsmountoptions="$aufsmountoptions:/mnt/overlayfs=rr" -fi +count=0 +for overlay in $(find /mnt/source -name "*.overlay" 2>/dev/null); do + vecho "Setting up overlay ${overlay}..." + mkdir -p /mnt/overlay${count} 2>/dev/null + mount -n -t squashfs -o loop,ro ${overlay} /mnt/overlay${count} + aufsmountoptions="$aufsmountoptions:/mnt/overlay${count}=rr" + count=$((${count} + 1)) +done -SQUASHED=$(ls /mnt/source/*.sfs 2>/dev/null || true) -if [ -n "$SQUASHED" ] ; then - vecho "Setting up embedded squashfs..." - mount -t squashfs -o loop,ro $SQUASHED /mnt/squashfs +count=0 +for sfs in $(find /mnt/source -name "*.sfs" 2>/dev/null); do + vecho "Setting up squashed fs ${sfs}..." + mount -n -t squashfs -o loop,ro ${sfs} /mnt/squashfs aufsmountoptions="$aufsmountoptions:/mnt/squashfs=rr" -fi + break +done mount -t aufs -o $aufsmountoptions none /sysroot @@ -291,6 +347,17 @@ for i in $(cd /mnt; ls); do mount --move /mnt/$i /sysroot/mnt/$i done +# Shutting down network +if [ "${type}" = "download" ]; then + if [ "${net}" = "dhcp" ]; then + kill $(pidof dhclient) + else + ip address flush dev ${netdevice} + fi + ip link set ${netdevice} down + ip route flush table main +fi + if [ "$ESHELL" == "1" ]; then echo "Shell requested on kernel commandline." echo "Rootfs is mounted ro on /sysroot. Exit to continue booting."