prepare-volatile-img.sh 774 B

123456789101112131415161718192021222324252627282930313233343536
  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. kpartx -s -a "$FILENAME"
  24. loopdev=`losetup -j "$FILENAME"|tail -n 1 |cut -d: -f1`
  25. looppart=`echo $loopdev|sed 's:dev:dev/mapper:'`
  26. mkswap -f ${looppart}p1 > /dev/null
  27. kpartx -s -d ${loopdev}
  28. losetup -d ${loopdev} || :
  29. chown --reference `dirname "$FILENAME"` "$FILENAME"