prepare-volatile-img.sh 685 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/bin/sh
  2. if [ "`id -u`" != "0" ]; then
  3. exec sudo $0 $*
  4. fi
  5. set -e
  6. FILENAME=$1
  7. ROOT_SIZE=$2
  8. SWAP_SIZE=$[ 1024 ]
  9. if [ -z "$ROOT_SIZE" -o -z "$FILENAME" ]; then
  10. echo "Usage: $0 <filename> <root.img size in MB>"
  11. exit 1
  12. fi
  13. if [ -e "$FILENAME" ]; then
  14. echo "$FILENAME already exists, not overriding"
  15. exit 1
  16. fi
  17. TOTAL_SIZE=$[ $ROOT_SIZE + $SWAP_SIZE + 512 ]
  18. truncate -s ${TOTAL_SIZE}M "$FILENAME"
  19. sfdisk --no-reread -u M "$FILENAME" > /dev/null 2> /dev/null <<EOF
  20. 0,${SWAP_SIZE},S
  21. ,${ROOT_SIZE},L
  22. EOF
  23. loopdev=`losetup -f --show --partscan "$FILENAME"`
  24. udevadm settle
  25. mkswap -f ${loopdev}p1 > /dev/null
  26. losetup -d ${loopdev} || :
  27. chown --reference `dirname "$FILENAME"` "$FILENAME"