2013-02-27 05:29:27 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
if [ "`id -u`" != "0" ]; then
|
|
|
|
exec sudo $0 $*
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2015-07-31 00:29:09 +02:00
|
|
|
if ! echo $PATH | grep -q sbin; then
|
|
|
|
PATH=$PATH:/sbin:/usr/sbin
|
|
|
|
fi
|
|
|
|
|
2013-02-27 05:29:27 +01:00
|
|
|
FILENAME=$1
|
|
|
|
ROOT_SIZE=$2
|
|
|
|
SWAP_SIZE=$[ 1024 ]
|
|
|
|
|
|
|
|
if [ -z "$ROOT_SIZE" -o -z "$FILENAME" ]; then
|
|
|
|
echo "Usage: $0 <filename> <root.img size in MB>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e "$FILENAME" ]; then
|
|
|
|
echo "$FILENAME already exists, not overriding"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TOTAL_SIZE=$[ $ROOT_SIZE + $SWAP_SIZE + 512 ]
|
|
|
|
truncate -s ${TOTAL_SIZE}M "$FILENAME"
|
|
|
|
sfdisk --no-reread -u M "$FILENAME" > /dev/null 2> /dev/null <<EOF
|
|
|
|
0,${SWAP_SIZE},S
|
|
|
|
,${ROOT_SIZE},L
|
|
|
|
EOF
|
|
|
|
|
2015-04-03 11:24:37 +02:00
|
|
|
loopdev=`losetup -f --show --partscan "$FILENAME"`
|
2015-03-30 21:30:58 +02:00
|
|
|
udevadm settle
|
2015-07-30 14:03:36 +02:00
|
|
|
created=
|
|
|
|
if [ ! -e ${loopdev}p1 ]; then
|
|
|
|
# device wasn't created automatically, probably udev isn't running;
|
|
|
|
# create devs manually
|
|
|
|
for partdev in /sys/block/$(basename ${loopdev})/loop*p*; do
|
|
|
|
mknod /dev/$(basename ${partdev}) b $(cat ${partdev}/dev | tr : ' ')
|
|
|
|
done
|
|
|
|
created=yes
|
|
|
|
fi
|
2015-05-13 01:44:35 +02:00
|
|
|
mkswap -f ${loopdev}p1 > /dev/null
|
2015-07-30 14:03:36 +02:00
|
|
|
if [ "$created" = "yes" ]; then
|
|
|
|
rm -f ${loopdev}p*
|
|
|
|
fi
|
2013-02-27 05:29:27 +01:00
|
|
|
losetup -d ${loopdev} || :
|
|
|
|
chown --reference `dirname "$FILENAME"` "$FILENAME"
|