Last active
December 17, 2020 14:57
-
-
Save akhilman/8df63b1c37701039273de65fe59bbf19 to your computer and use it in GitHub Desktop.
Convert antix live USB to f2fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 1 ] || ! [ -b $1 ]; then | |
echo Usage: | |
echo $(basename $0) boot_part | |
exit 1 | |
fi | |
if [ $(whoami) != root ]; then | |
echo you are not root | |
exit 1 | |
fi | |
old_wd=$(pwd) | |
boot_part=$1 | |
crypt_part= | |
loop_dev= | |
tmp_dir=$(mktemp -d) | |
mapper_name=$(head -c 5 /dev/urandom | base32) | |
function clean_exit() { | |
# ==== | |
echo Cleaning up | |
cd $old_wd | |
if ! [ -z "$loop_dev" ]; then | |
umount $tmp_dir/root | |
losetup -d $loop_dev | |
fi | |
if ! [ -z "$crypt_part" ]; then | |
umount $tmp_dir/crypt | |
cryptsetup close $mapper_name | |
fi | |
umount $tmp_dir/boot | |
[ -d $tmp/initrd ] && rm -rf $tmp_dir/initrd | |
rm -rf $tmp_dir/crypt_copy | |
[ -f $tmp_dir/initrd.gz ] && rm $tmp_dir/initrd.gz | |
[ -f $tmp_dir/initrd.gz.md5 ] && rm $tmp_dir/initrd.gz.md5 | |
rmdir $tmp_dir/root | |
rmdir $tmp_dir/crypt | |
rmdir $tmp_dir/boot | |
rmdir $tmp_dir | |
exit $@ | |
} | |
# ==== | |
echo Preparing | |
mkdir $tmp_dir/boot | |
mkdir $tmp_dir/crypt | |
mkdir $tmp_dir/root | |
mkdir $tmp_dir/crypt_copy | |
mount $boot_part $tmp_dir/boot \ | |
|| clean_exit $? | |
crypt_part=/dev/disk/by-uuid/$(cat $tmp_dir/boot/antiX/encrypted) \ | |
|| clean_exit $? | |
[ -b "$crypt_part" ] \ | |
|| (echo Can not find encrypted partition $crypt_part; exit 1) | |
cryptsetup --key-file=$tmp_dir/boot/antiX/passphrase open $crypt_part $mapper_name \ | |
|| cryptsetup open $crypt_part $mapper_name \ | |
|| clean_exit $? | |
mount /dev/mapper/$mapper_name $tmp_dir/crypt \ | |
|| clean_exit $? | |
loop_dev=$(losetup --find --show $tmp_dir/crypt/antiX/linuxfs) | |
mount $loop_dev $tmp_dir/root \ | |
|| clean_exit $? | |
# ==== | |
# Patching initrd | |
function patch_initrd() { | |
initrd=$1 | |
echo Patching initrd $initrd | |
mkdir $tmp_dir/initrd | |
cd $tmp_dir/initrd | |
zcat $initrd | cpio -i \ | |
|| clean_exit $? | |
kernel_name=$(ls -d lib/modules/* | tail -n 1 | sed 's/.*\///') | |
cd $tmp_dir/root | |
for f in \ | |
crypto/crc32_generic.ko \ | |
arch/x86/crypto/crc32-pclmul.ko | |
do | |
f=lib/modules/$kernel_name/kernel/$f | |
cp -v $f $tmp_dir/initrd/$f \ | |
|| clean_exit $? | |
done | |
# find lib/modules/$kernel_name -type f -name 'crc32*' | while read f; do | |
# cp -v $f $tmp_dir/initrd/$f \ | |
# || clean_exit $? | |
# done | |
cd $tmp_dir/initrd | |
find . | cpio -H newc -o | gzip > $tmp_dir/initrd.gz | |
cd $tmp_dir | |
md5sum initrd.gz > initrd.gz.md5 \ | |
|| clean_exit $? | |
mv -v initrd.gz $initrd \ | |
|| clean_exit $? | |
mv -v initrd.gz.md5 ${initrd}.md5 \ | |
|| clean_exit $? | |
rm -rf initrd | |
} | |
patch_initrd $tmp_dir/boot/antiX/initrd.gz | |
patch_initrd $tmp_dir/crypt/antiX/initrd.gz | |
# ==== | |
echo Reformatting encrypted partition | |
umount $tmp_dir/root | |
losetup -d $loop_dev | |
loop_dev= | |
rsync -raP $tmp_dir/crypt/* $tmp_dir/crypt_copy/ \ | |
|| clean_exit $? | |
umount $tmp_dir/crypt \ | |
|| clean_exit $? | |
crypt_label=$(blkid -s LABEL -o value /dev/mapper/$mapper_name) \ | |
|| clean_exit $? | |
mkfs.f2fs -f -l "$crypt_label" /dev/mapper/$mapper_name \ | |
|| clean_exit $? | |
mount /dev/mapper/$mapper_name $tmp_dir/crypt \ | |
|| clean_exit $? | |
rsync -raP $tmp_dir/crypt_copy/* $tmp_dir/crypt/ \ | |
|| clean_exit $? | |
# ==== | |
clean_exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment