Skip to content

Commit 5f3e3cb

Browse files
Add files via upload
1 parent ec943df commit 5f3e3cb

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed

cvt2f2fs/cvt2f2fs

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#!/bin/bash
2+
3+
trap '{ stty sane; printf "\n\nAborting\n\n"; exit 1; }' SIGINT SIGTERM
4+
5+
# Ensure root User
6+
if [ $(id -u) -ne 0 ]; then
7+
echo ""
8+
echo "Must be run as root user: sudo $0"
9+
echo ""
10+
exit 1
11+
fi
12+
13+
# Delete Profile Script
14+
if [ -f /etc/profile.d/cvt2f2fs-ps.sh ]; then
15+
rm /etc/profile.d/cvt2f2fs-ps.sh
16+
fi
17+
18+
# Check Init System
19+
if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
20+
SYSTEMD=1
21+
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
22+
SYSTEMD=0
23+
else
24+
echo ""
25+
echo "Unrecognized init system"
26+
echo ""
27+
exit 1
28+
fi
29+
30+
# Get Root Partition
31+
if [ ${SYSTEMD} -eq 1 ]; then
32+
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
33+
else
34+
if [ ! -h /dev/root ]; then
35+
echo ""
36+
echo "/dev/root does not exist or is not a symlink"
37+
echo ""
38+
exit 1
39+
fi
40+
ROOT_PART=$(readlink /dev/root)
41+
fi
42+
43+
# Install realpath
44+
realpath --version &> /dev/null
45+
if [ $? -ne 0 ]; then
46+
apt-get -y install realpath
47+
fi
48+
49+
# Get Program Path
50+
PGMPATH=$(realpath $0)
51+
52+
echo ""
53+
df
54+
echo ""
55+
sleep 3
56+
echo ""
57+
if [ "$1" = "" ]; then
58+
echo -n "Ok to convert SD card to F2FS (y/n)? "
59+
while read -r -n 1 -s answer; do
60+
if [[ "${answer}" = [yYnN] ]]; then
61+
echo "${answer}"
62+
if [[ "${answer}" = [yY] ]]; then
63+
break
64+
else
65+
echo ""
66+
echo "Aborted"
67+
echo ""
68+
exit 1
69+
fi
70+
fi
71+
done
72+
echo ""
73+
echo ""
74+
echo "Convert to F2FS: Phase 1"
75+
echo ""
76+
77+
#
78+
if [ "${ROOT_PART}" = "mmcblk0p2" ]; then
79+
PARTUUID="$(sed -n 's|^.*root=\(\S\+\)\s.*|\1|p' /boot/cmdline.txt)"
80+
PARTUUID="${PARTUUID:0:(${#PARTUUID} - 1)}"
81+
sed -i 's|root=\S\+\s|root=/dev/mmcblk0p2 |' /boot/cmdline.txt
82+
sed -i "s|$PARTUUID|/dev/mmcblk0p|" /etc/fstab
83+
else
84+
echo ""
85+
echo "Not running from SD card: Can't convert to F2FS"
86+
echo ""
87+
exit 1
88+
fi
89+
90+
# Update/Upgrade Raspbian
91+
echo ""
92+
echo "Updating/Upgrading Raspbian"
93+
if [ -e /etc/apt/listchanges.conf ]; then
94+
sed -i 's/frontend=pager/frontend=text/' /etc/apt/listchanges.conf
95+
fi
96+
apt-get -y update
97+
apt-get -y upgrade
98+
apt-get -y dist-upgrade
99+
if [ -e /etc/apt/listchanges.conf ]; then
100+
sed -i 's/frontend=text/frontend=pager/' /etc/apt/listchanges.conf
101+
fi
102+
103+
104+
# Create Profile Script
105+
cat <<EOF > /etc/profile.d/cvt2f2fs-ps.sh &&
106+
#!/bin/bash
107+
108+
ps cax | grep cvt2f2fs-ps.sh > /dev/null
109+
if [[ \$? -ne 0 && \$(id -u) -eq 0 ]]; then
110+
sudo ${PGMPATH} --phase2
111+
fi
112+
EOF
113+
114+
# Reboot
115+
shutdown -r now
116+
elif [ "$1" = "--phase2" ]; then
117+
echo "Convert to F2FS: Phase 2"
118+
echo ""
119+
echo ""
120+
121+
# Install rsync
122+
rsync --version &> /dev/null
123+
if [ $? -ne 0 ]; then
124+
apt-get -y install rsync
125+
fi
126+
127+
apt-get -y install f2fs-tools
128+
umount /dev/sda1 &> /dev/null
129+
umount /dev/sda2 &> /dev/null
130+
echo "Partitioning USB flash drive"
131+
fdisk /dev/sda <<EOF &> /dev/null
132+
o
133+
n
134+
p
135+
1
136+
137+
138+
w
139+
EOF
140+
partprobe
141+
echo "Creating ext4 filesystem on USB flash drive"
142+
mke2fs -F -q -t ext4 -L rootfs /dev/sda1 &> /dev/null
143+
echo "Copying root filesystem to USB flash drive (this will take a while)"
144+
mount /dev/sda1 /mnt
145+
rsync -ax / /mnt
146+
sed -i 's|/dev/mmcblk0p2|#/dev/mmcblk0p2|' /mnt/etc/fstab
147+
echo "/dev/sda1 / ext4 defaults,noatime 0 1" >> /mnt/etc/fstab
148+
149+
# Create Profile Script
150+
cat <<EOF > /mnt/etc/profile.d/cvt2f2fs-ps.sh &&
151+
#!/bin/bash
152+
153+
ps cax | grep cvt2f2fs-ps.sh > /dev/null
154+
if [[ \$? -ne 0 && \$(id -u) -eq 0 ]]; then
155+
sudo ${PGMPATH} --phase3
156+
fi
157+
EOF
158+
159+
umount /mnt
160+
sed -i 's|root=/dev/mmcblk0p2|root=/dev/sda1|' /boot/cmdline.txt
161+
sed -i 's|rootwait|rootwait rootdelay=5|' /boot/cmdline.txt
162+
echo "Rebooting to USB flash drive"
163+
echo ""
164+
165+
# Reboot
166+
shutdown -r now
167+
elif [ "$1" = "--phase3" ]; then
168+
echo "Convert to F2FS: Phase 3"
169+
echo ""
170+
echo ""
171+
172+
echo "Creating F2FS filesystem on SD card"
173+
wipefs --force -a /dev/mmcblk0p2 > /dev/null
174+
mkfs.f2fs /dev/mmcblk0p2 &> /dev/null
175+
echo "Copying root filesystem to SD card (this will take a while)"
176+
mount /dev/mmcblk0p2 /mnt
177+
rsync -ax / /mnt
178+
sed -i '/\/dev\/sda1/d' /mnt/etc/fstab
179+
sed -i 's|#/dev/mmcblk0p2|/dev/mmcblk0p2|' /mnt/etc/fstab
180+
sed -i 's|ext4|f2fs|' /mnt/etc/fstab
181+
sed -i 's|defaults,noatime|defaults,noatime,discard|' /mnt/etc/fstab
182+
183+
# Create Profile Script
184+
cat <<EOF > /mnt/etc/profile.d/cvt2f2fs-ps.sh &&
185+
#!/bin/bash
186+
187+
ps cax | grep cvt2f2fs-ps.sh > /dev/null
188+
if [[ \$? -ne 0 && \$(id -u) -eq 0 ]]; then
189+
sudo ${PGMPATH} --phase4
190+
fi
191+
EOF
192+
193+
umount /mnt
194+
sed -i 's|root=/dev/sda1|root=/dev/mmcblk0p2|' /boot/cmdline.txt
195+
sed -i 's|rootfstype=ext4|rootfstype=f2fs|' /boot/cmdline.txt
196+
sed -i 's|rootwait rootdelay=5|rootwait|' /boot/cmdline.txt
197+
echo "Rebooting to SD card"
198+
echo ""
199+
200+
# Reboot
201+
shutdown -r now
202+
elif [ "$1" = "--phase4" ]; then
203+
echo "Convert to F2FS: Phase 4"
204+
echo ""
205+
echo ""
206+
207+
# Cleanup
208+
apt-get -y autoremove
209+
apt-get -y clean
210+
rm -r /boot.bak/ &> /dev/null
211+
212+
echo ""
213+
echo "Convert to F2FS Complete"
214+
echo ""
215+
fi

cvt2f2fs/cvt2f2fs.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The cvt2f2fs script will convert a Raspian ext4 filesystem on an SD card to F2FS.
2+
3+
!!! MAKE A BACKUP COPY OF THE SD CARD BEFORE ATTEMPTING THIS PROCEDURE !!!
4+
5+
1. Disconnect all USB devices from the Raspberry Pi.
6+
7+
2. Connect a USB flash drive with sufficient capacity to hold the SD card contents.
8+
!!! ALL EXISTING DATA ON THE USB FLASH DRIVE WILL BE DESTROYED !!!
9+
10+
3. Log in as root
11+
12+
4. Run ./cvt2f2fs
13+
14+
5. Log in as root following the reboots which occur at the completion of Phases 1, 2, and 3
15+
16+
6. When Phase 4 completes, the SD card should be using an F2FS filesystem instead of ext4

0 commit comments

Comments
 (0)