Browse Source

img2persistant: utilisation de parted et de -P

decouverte-jessie
Fabien Givors 7 years ago
parent
commit
51dcb63b52
  1. 32
      img2persistant

32
img2persistant

@ -6,34 +6,38 @@ kio=1024
mio=$((1024*$kio))
gio=$((1024*$mio))
DATASIZE=$((100 * $mio))
IMGFILE=$1
LOOPDEV=$(losetup -f)
# Vérifier le nombre de partitions
losetup $LOOPDEV $IMGFILE
FDISK_OUT="$(fdisk -l -u $LOOPDEV 2>&1)"
losetup -P $LOOPDEV $IMGFILE
# Si une seule, on crée la seconde
if [ ! $(echo "$FDISK_OUT" | grep -s "${LOOPDEV}p2") ]
if [ ! -e "${LOOPDEV}p2" ]
then
IMGSIZE=$(( $(echo "$FDISK_OUT" | sed -ne "s|^${LOOPDEV}p1[ *]\+[0-9]\+[ ]\+\([0-9]*\).*|\1|p") * 512 ))
NEWSIZE=$(($IMGSIZE + 100*$mio))
PARTED_OUT=$(parted "${LOOPDEV}" "unit B p" | grep '^[[:space:]]*[[:digit:]]*' 2>/dev/null)
PARTED_IMG="$(echo "$PARTED_OUT" | grep '^[[:space:]]*1\>')"
PARTED_DATA="$(echo "$PARTED_OUT" | grep '^[[:space:]]*2\>' || true)"
IMGSIZE=$(echo $PARTED_IMG | cut -d' ' -f3 | sed 's/B$//')
NEWSIZE=$(($IMGSIZE + $DATASIZE))
dd if=/dev/zero of=$IMGFILE bs=1c count=0 seek=$NEWSIZE
losetup -c $LOOPDEV
parted -a optimal -s $LOOPDEV mkpart primary ext4 $(($IMGSIZE + 512))B $(($NEWSIZE - 4*$mio))B
losetup -d $LOOPDEV
losetup -P $LOOPDEV $IMGFILE
fi
# Formatage et préparation de la partition de persistance
FDISK_OUT="$(fdisk -l -u $LOOPDEV 2>&1)"
losetup -d $LOOPDEV
SECTORS="$(echo "$FDISK_OUT" | sed -ne "s|^${LOOPDEV}p2[ *]*\([0-9]*\).*|\1|p")"
OFFSET="$(expr ${SECTORS} '*' 512)"
losetup -o $OFFSET $LOOPDEV $IMGFILE
mkfs.ext4 -L persistence-usb $LOOPDEV
# 250000 inodes pour 1Go / pour agrandir, il faut augmenter la taille data
#mkfs.ext4 -L persistence-usb -N $((250000)) ${LOOPDEV}p2
mkfs.ext4 -L persistence-usb ${LOOPDEV}p2
DIR=$(mktemp -d)
mount $LOOPDEV $DIR
mount ${LOOPDEV}p2 $DIR
echo "/ union" > "$DIR/persistence.conf"
umount $DIR
rmdir $DIR
losetup -d $LOOPDEV