2020-05-30 12:13:04 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
size=1024
|
|
|
|
fstype=ext4
|
|
|
|
label=casper-rw
|
2021-01-15 06:09:59 +00:00
|
|
|
config=''
|
2021-05-14 04:12:07 +00:00
|
|
|
outputfile=persistence.dat
|
2020-05-30 12:13:04 +00:00
|
|
|
|
|
|
|
print_usage() {
|
2021-01-15 06:09:59 +00:00
|
|
|
echo 'Usage: CreatePersistentImg.sh [ -s size ] [ -t fstype ] [ -l LABEL ] [ -c CFG ]'
|
2020-05-30 12:13:04 +00:00
|
|
|
echo ' OPTION: (optional)'
|
|
|
|
echo ' -s size in MB, default is 1024'
|
|
|
|
echo ' -t filesystem type, default is ext4 ext2/ext3/ext4/xfs are supported now'
|
|
|
|
echo ' -l label, default is casper-rw'
|
2021-01-15 06:09:59 +00:00
|
|
|
echo ' -c configfile name inside the persistence file. File content is "/ union"'
|
2021-05-14 04:12:07 +00:00
|
|
|
echo ' -o outputfile name, default is persistence.dat'
|
2020-05-30 12:13:04 +00:00
|
|
|
echo ''
|
|
|
|
}
|
|
|
|
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
if [ "$1" = "-s" ]; then
|
|
|
|
shift
|
|
|
|
size=$1
|
|
|
|
elif [ "$1" = "-t" ]; then
|
|
|
|
shift
|
|
|
|
fstype=$1
|
|
|
|
elif [ "$1" = "-l" ]; then
|
|
|
|
shift
|
|
|
|
label=$1
|
2021-01-15 06:09:59 +00:00
|
|
|
elif [ "$1" = "-c" ]; then
|
|
|
|
shift
|
|
|
|
config=$1
|
2021-05-14 04:12:07 +00:00
|
|
|
elif [ "$1" = "-o" ]; then
|
|
|
|
shift
|
|
|
|
outputfile=$1
|
2020-09-02 11:29:19 +00:00
|
|
|
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
|
|
|
print_usage
|
|
|
|
exit 0
|
2020-05-30 12:13:04 +00:00
|
|
|
else
|
|
|
|
print_usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
# check label
|
|
|
|
if [ -z "$label" ]; then
|
|
|
|
echo "The label can NOT be empty."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check size
|
|
|
|
if echo $size | grep -q "^[0-9][0-9]*$"; then
|
|
|
|
if [ $size -le 1 ]; then
|
|
|
|
echo "Invalid size $size"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Invalid size $size"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# check file system type
|
|
|
|
# nodiscard must be set for ext2/3/4
|
|
|
|
# -K must be set for xfs
|
|
|
|
if echo $fstype | grep -q '^ext[234]$'; then
|
|
|
|
fsopt='-E nodiscard'
|
|
|
|
elif [ "$fstype" = "xfs" ]; then
|
|
|
|
fsopt='-K'
|
|
|
|
else
|
|
|
|
echo "unsupported file system $fstype"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-14 04:12:07 +00:00
|
|
|
if [ "$outputdir" != "persistence.dat" ]; then
|
|
|
|
mkdir -p "$(dirname "$outputfile")"
|
|
|
|
fi
|
|
|
|
|
2020-05-30 12:13:04 +00:00
|
|
|
# 00->ff avoid sparse file
|
2021-05-14 04:12:07 +00:00
|
|
|
dd if=/dev/zero bs=1M count=$size | tr '\000' '\377' > "$outputfile"
|
2020-05-30 12:13:04 +00:00
|
|
|
sync
|
|
|
|
|
|
|
|
freeloop=$(losetup -f)
|
|
|
|
|
2021-05-14 04:12:07 +00:00
|
|
|
losetup $freeloop "$outputfile"
|
2020-05-30 12:13:04 +00:00
|
|
|
|
|
|
|
mkfs -t $fstype $fsopt -L $label $freeloop
|
|
|
|
|
|
|
|
sync
|
|
|
|
|
2021-01-15 06:09:59 +00:00
|
|
|
if [ -n "$config" ]; then
|
|
|
|
if [ -d ./persist_tmp_mnt ]; then
|
|
|
|
rm -rf ./persist_tmp_mnt
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir ./persist_tmp_mnt
|
|
|
|
if mount $freeloop ./persist_tmp_mnt; then
|
|
|
|
echo '/ union' > ./persist_tmp_mnt/$config
|
|
|
|
sync
|
|
|
|
umount ./persist_tmp_mnt
|
|
|
|
fi
|
|
|
|
rm -rf ./persist_tmp_mnt
|
|
|
|
fi
|
2020-05-30 12:13:04 +00:00
|
|
|
|
2021-01-15 06:09:59 +00:00
|
|
|
losetup -d $freeloop
|