Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Last active June 14, 2021 00:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanpuyue/d0e7313b7696a971e9ce2d4466d7993f to your computer and use it in GitHub Desktop.
Save nanpuyue/d0e7313b7696a971e9ce2d4466d7993f to your computer and use it in GitHub Desktop.
Generate the initrd for Debian/Deepin/Ubuntu Live that supported boot from iso on an exfat partition, then you can generate the new iso by “ISO Master”. Tested for Deepin 15.3, Debian 8.5 & Ubuntu 16.04.1.
#!/bin/bash
# (c) 2016 nanpuyue <nanpuyue@gmail.com>
# usage:sudo ./live_initrd_on_exfat.sh deepin-15.3-amd64.iso
NAMESERVER="1.2.4.8"
EXFAT_BIN="sbin/mount.exfat"
FILE_LIST="sbin/mount.exfat sbin/mount.exfat-fuse lib/x86_64-linux-gnu/libfuse.so.*"
ISO=$(readlink -f "$1")
OUTPUT_DIR=$(pwd)
TMP_DIR=$(mktemp -d) && echo $TMP_DIR
mount_fs(){
echo "mount filesystem"
cd "$TMP_DIR"
mkdir iso initrd squashfs
if [ -f "$ISO" ];then
mount -o loop "$ISO" iso
for LIVE in live casper;do
if [ -f iso/$LIVE/filesystem.squashfs ];then
mount -t squashfs -o loop iso/$LIVE/filesystem.squashfs squashfs
break 1
fi
done
else
echo "no such file:$ISO" && exit 1
fi
}
extract_initrd(){
echo "extract initrd"
cd "$TMP_DIR/initrd"
for EXT in img gz lz;do
if [ -f ../iso/$LIVE/initrd.$EXT ];then
COMPRESS=$(file -b --mime-type ../iso/$LIVE/initrd.$EXT | grep -Eo "(gzip|lzma)$") && echo compress:$COMPRESS || {
echo "can't extract initrd" && exit 1
}
eval $COMPRESS -dcS $EXT ../iso/$LIVE/initrd.$EXT | cpio -id
break 1
fi
done
}
install_exfat_fuse(){
echo "install exfat-fuse"
cd "$TMP_DIR"
mkdir overlay work rootfs
mount -t overlay overlay -o lowerdir=squashfs,upperdir=overlay,workdir=work rootfs
cat > rootfs/install_exfat_fuse.sh << EOF
#!/bin/bash
export LC_ALL=C
echo "nameserver $NAMESERVER" > /etc/resolv.conf
lsb_release -d | grep Ubuntu && add-apt-repository universe
apt-get update
apt-get -y install exfat-fuse
EOF
bash -c "chroot rootfs /bin/bash /install_exfat_fuse.sh"
}
copy_files_from(){
cd "$TMP_DIR/initrd"
for file in $FILE_LIST;do
cp -avf "$1"/$file ./$(dirname $file)
done
}
copy_files(){
if [ -f "$TMP_DIR/squashfs/$EXFAT_BIN" ];then
copy_files_from "$TMP_DIR/squashfs"
else
install_exfat_fuse && copy_files_from "$TMP_DIR/rootfs"
umount "$TMP_DIR/rootfs"
fi
}
patch_scripts(){
cd "$TMP_DIR/initrd"
if [ -f ./bin/boot/9990-misc-helpers.sh ];then
patch -p0 --no-backup-if-mismatch << "EOF"
--- ./bin/boot/9990-misc-helpers.sh 2016-09-08 22:24:25.582875865 +0800
+++ ./bin/boot/9990-misc-helpers.sh 2016-09-08 22:27:23.765579826 +0800
@@ -420,6 +420,14 @@
return 1
fi
+ # exfat support
+ if [ "${fstype}" = "exfat" ]
+ then
+ modprobe -q -b fuse
+ alias mount='mount.util-linux'
+ return 0
+ fi
+
# Try to look if it is already supported by the kernel
if grep -q ${fstype} /proc/filesystems
then
EOF
cp -avf ./bin/boot/9990-misc-helpers.sh ./lib/live/boot/9990-misc-helpers.sh
elif [ -f ./scripts/casper-helpers ];then
patch -p0 --no-backup-if-mismatch << "EOF"
--- ./scripts/casper-helpers 2016-09-09 22:18:34.223899178 +0800
+++ ./scripts/casper-helpers 2016-09-09 22:33:46.964777273 +0800
@@ -31,7 +31,7 @@
# FIXME: do something better like the scan of supported filesystems
fstype="${1}"
case ${fstype} in
- vfat|iso9660|udf|ext2|ext3|ext4|btrfs|ntfs)
+ vfat|iso9660|udf|ext2|ext3|ext4|btrfs|ntfs|exfat)
return 0
;;
esac
--- ./scripts/lupin-helpers 2016-09-09 23:30:26.822777653 +0800
+++ ./scripts/lupin-helpers 2016-09-09 23:32:52.033543153 +0800
@@ -3,7 +3,7 @@
is_supported_fs(){
[ -z "${1}" ] && return 1
case ${1} in
- ext2|ext3|ext4|xfs|jfs|reiserfs|vfat|ntfs|iso9660|btrfs)
+ ext2|ext3|ext4|xfs|jfs|reiserfs|vfat|ntfs|exfat|iso9660|btrfs)
return 0
;;
esac
EOF
else
echo "no file to patch"
fi
}
compress_initrd(){
echo "compress initrd ..."
cd "$TMP_DIR/initrd"
find . | cpio --quiet -o -H newc | eval $COMPRESS $([ $COMPRESS = "gzip" ] && echo "--rsyncable") > "$OUTPUT_DIR/initrd.$EXT"
}
clear_up(){
echo "clear up"
umount "$TMP_DIR/squashfs" && umount "$TMP_DIR/iso"
cd / && rm -rf "$TMP_DIR"
}
mount_fs
extract_initrd
copy_files
patch_scripts
compress_initrd
clear_up
echo "done"
@guitarbug
Copy link

guitarbug commented Jun 14, 2021

run this script in ubuntu-16.04.7-desktop-amd64 and get the below errors:, did not get the initrd.


wwww@M6100T:~/exfat$ sudo ./live_initrd_on_exfat.sh ./ubuntu-16.04.7-desktop-amd64.iso
[sudo] password for wwww:
/tmp/tmp.NRkylkfOo5
mount filesystem
mount: /dev/loop0 is write-protected, mounting read-only
extract initrd
install exfat-fuse
Description: Ubuntu 16.04.7 LTS
'universe' distribution component enabled for all sources.
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [1648 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/main Translation-en [380 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 DEP-11 Metadata [93.7 kB]
Get:5 http://security.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [111 kB]
Get:6 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [9824 B]
Get:7 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [785 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [225 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 DEP-11 Metadata [130 kB]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [206 kB]
Hit:11 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:12 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [7532 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial/universe Translation-en [4354 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial/universe amd64 DEP-11 Metadata [3410 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial/universe DEP-11 64x64 Icons [7448 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [2049 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [482 kB]
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 DEP-11 Metadata [326 kB]
Get:20 http://archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [252 kB]
Get:21 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [10.2 kB]
Get:22 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [1219 kB]
Get:23 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [358 kB]
Get:24 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 DEP-11 Metadata [281 kB]
Get:25 http://archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [437 kB]
Fetched 32.0 MB in 18s (1744 kB/s)

** (appstreamcli:2703): CRITICAL **: Error while moving old database out of the way.
AppStream cache update failed.
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
exfat-utils
The following NEW packages will be installed:
exfat-fuse exfat-utils
0 upgraded, 2 newly installed, 0 to remove and 200 not upgraded.
Need to get 64.9 kB of archives.
After this operation, 281 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial/universe amd64 exfat-fuse amd64 1.2.3-1 [24.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/universe amd64 exfat-utils amd64 1.2.3-1 [40.1 kB]
Fetched 64.9 kB in 7s (8548 B/s)
E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)
Selecting previously unselected package exfat-fuse.
(Reading database ... 196344 files and directories currently installed.)
Preparing to unpack .../exfat-fuse_1.2.3-1_amd64.deb ...
Unpacking exfat-fuse (1.2.3-1) ...
Selecting previously unselected package exfat-utils.
Preparing to unpack .../exfat-utils_1.2.3-1_amd64.deb ...
Unpacking exfat-utils (1.2.3-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up exfat-fuse (1.2.3-1) ...
Setting up exfat-utils (1.2.3-1) ...
'/tmp/tmp.NRkylkfOo5/rootfs/sbin/mount.exfat' -> './sbin'
'/tmp/tmp.NRkylkfOo5/rootfs/sbin/mount.exfat-fuse' -> './sbin'
cp: not writing through dangling symlink './sbin'
cp: target './lib/x86_64-linux-gnu' is not a directory
no file to patch
compress initrd ...
./live_initrd_on_exfat.sh: line 133: [: =: unary operator expected
clear up
done
wwww@M6100T:/exfat$ uname -a
Linux M6100T 4.15.0-112-generic #113
16.04.1-Ubuntu SMP Fri Jul 10 04:37:08 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
wwww@M6100T:~/exfat$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment