Advertisement
metalx1000

Create Minimal Linux with busybox and initrd

Mar 3rd, 2016
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. pwd=`echo $PWD`
  4. dir="miniLinux"
  5. fs="initramfs"
  6. qemu="qemu-system-x86_64"
  7. kernel=`ls /boot/vmlinuz*|tail -n1`
  8.  
  9. if [ -d $dir ]
  10. then
  11.   echo "$dir exists"
  12.   echo "stopping script..."
  13.   exit 1
  14. fi
  15.  
  16. mkdir -p $dir/$fs
  17.  
  18. cd $dir/$fs
  19. mkdir -pv bin lib dev etc mnt/root proc root sbin sys etc/init.d
  20.  
  21. echo "Getting Busybox..."
  22. cd bin
  23. wget "https://busybox.net/downloads/binaries/latest/busybox-x86_64" -O busybox
  24. chmod +x busybox
  25.  
  26. echo "Linking Busybox Apps..."
  27. ./busybox  --help | \
  28. sed -e '1,/^Currently defined functions:/d' \
  29.     -e 's/[ \t]//g' -e 's/,$//' -e 's/,/\n/g' | \
  30. while read app ; do
  31.   if [ "$app" != "" ]; then
  32.     printf "linking %-12s ...\n" "$app"
  33.     ln -sf "./busybox" "$app"
  34.     ls -ld "$app"
  35.   fi
  36. done
  37. cp init /sbin/init
  38.  
  39. echo "Creating init script..."
  40. cd $pwd/$dir/$fs
  41. echo
  42. cat << EOF > init
  43. #!/bin/sh
  44.  
  45. mount -t devtmpfs none /dev
  46. /bin/mount -t proc none /proc
  47. /bin/mount -t sysfs sysfs /sys
  48. /bin/mdev -s
  49.  
  50. sleep 3
  51. clear
  52. echo "============Welcome============"
  53. /bin/ash --login
  54.  
  55. echo "Goodbye..."
  56. read
  57. # Clean up.
  58. umount /proc
  59. umount /sys
  60. umount /dev
  61. EOF
  62.  
  63. chmod 755 init
  64.  
  65. find . -print0 | cpio --null -ov --format=newc > ../initrd.cpio
  66.  
  67. cd $pwd/$dir
  68. cp $kernel ./kernel
  69.  
  70. $qemu -kernel kernel -initrd initrd.cpio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement