Verified Commit 37b9dbfd authored by Michal Hrusecky's avatar Michal Hrusecky 🐉
Browse files

turris-btrfs-migrate: Initial version of btrfs migration script

parent 122331c1
#
# Copyright (C) 2017 CZ.NIC z.s.p.o. (http://www.nic.cz/)
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=turris-btrfs-migrate
PKG_VERSION:=1.0
PKG_RELEASE:=1
PKG_SOURCE_SUBDIR:=$(PKG_NAME)
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
PKG_MAINTAINER:=Michal Hrusecky <Michal.Hrusecky@nic.cz>
PKG_LICENSE:=GPL-2.0
include $(INCLUDE_DIR)/package.mk
define Package/turris-btrfs-migrate
SECTION:=utils
CATEGORY:=Utilities
SUBMENU:=Filesystem
DEPENDS:=+btrfs-progs +uboot-envtools +fdisk +mkdosfs +schnapps @TARGET_mpc85xx_p2020_nand
TITLE:=Btrfs migration tool for old Turris routers
endef
define Package/turris-btrfs-migrate/description
This tool will allow you to plug SD card into your Turris, format it to Btrfs
and use it for your root filesystem.
endef
define Build/Compile
true
endef
define Package/turris-btrfs-migrate/install
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) ./files/btrfs_migrate.sh $(1)/usr/bin/btrfs_migrate
endef
$(eval $(call BuildPackage,turris-btrfs-migrate))
#!/bin/sh
#
## Copyright (C) 2017 CZ.NIC z.s.p.o. (http://www.nic.cz/)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
die() {
echo "$1" >&2
exit 1
}
[ -b /dev/mmcblk0 ] || die "No MicroSD card present!"
[ -n "`mount | grep ubi`" ] || die "1.1 firmware required!"
echo 'ROOT_DEV="/dev/mmcblk0p2"' > /etc/schnapps
# Deploy schnapps hook
mkdir -p /etc/schnapps/rollback.d/
[ -f /etc/schnapps/rollback.d/kernel-rollback ] || cat > /etc/schnapps/rollback.d/kernel-rollback << EOF
#!/bin/sh
mkdir -p /boot/tefi
mount /dev/mmcblk0p1 /boot/tefi
cmp /boot/zImage /boot/tefi/zImage || cp /boot/zImage /boot/tefi/zImage
cmp /boot/ftp /boot/tefi/ftd || cp /boot/ftd /boot/tefi/ftd
umount /boot/tefi
EOF
chmod a+rx /etc/schnapps/rollback.d/kernel-rollback
# Deploy updater hook
mkdir -p /etc/updater/hook_postupdate/
[ -f /etc/updater/hook_postupdate/10_kernel-install ] || cat > /etc/updater/hook_postupdate/10_kernel-install << EOF
#!/bin/sh
mkdir -p /boot/tefi
mount /dev/mmcblk0p1 /boot/tefi
cmp /boot/zImage /boot/tefi/zImage || cp /boot/zImage /boot/tefi/zImage
cmp /boot/ftp /boot/tefi/ftd || cp /boot/ftd /boot/tefi/ftd
umount /boot/tefi
EOF
ANS=""
echo "Are you sure you want to loose everythin on mmcblk0? (yes/no)"
read ANS
if [ "$ANS" \!= "yes" ]; then
exit 0
fi
dd if=/dev/zero of=/dev/mmcblk0 bs=10M count=11
fdisk /dev/mmcblk0 <<EOF
o
n
p
1
+100M
n
p
2
w
EOF
mkdir -p /tmp/btrfs-convert/target
mkdir -p /tmp/btrfs-convert/src
mkfs.vfat /dev/mmcblk0p1 || die "Can't create fat!"
mkfs.btrfs -f /dev/mmcblk0p2 || die "Can't format btrfs partition!"
mount /dev/mmcblk0p2 /tmp/btrfs-convert/target || die "Can't mount mmclbk0p2"
btrfs subvolume create /tmp/btrfs-convert/target/@ || die "Can't create subvolume!"
mount -o bind / /tmp/btrfs-convert/src || die "Can't bind btrfs mount."
tar -C /tmp/btrfs-convert/src -cf - . | tar -C /tmp/btrfs-convert/target/@ -xf - || die "Filesystem copy failed!"
mkdir -p /tmp/btrfs-convert/target/@/boot/tefi
mount /dev/mmcblk0p1 /tmp/btrfs-convert/target/@/boot/tefi || die "Can't mount fat"
cp /boot/zImage /boot/fdt /tmp/btrfs-convert/target/@/boot/tefi || die "Can't copy kernel"
umount /tmp/btrfs-convert/target/@/boot/tefi
umount /tmp/btrfs-convert/target
umount /tmp/btrfs-convert/src
# Setup u-Boot
fw_setenv -s - <<EOF
baudrate 115200
bootargsnor root=/dev/mtdblock2 rw rootfstype=jffs2 console=ttyS0,115200
bootargsubi root=ubi0:rootfs rootfstype=ubifs ubi.mtd=9,2048 rootflags=chk_data_crc rw console=ttyS0,115200
bootdelay 1
bootfile uImage
consoledev ttyS0
ethact eTSEC3
ethprime eTSEC3
fdtaddr c00000
jffs2nand mtdblock9
jffs2nor mtdblock3
loadaddr 1000000
map_lowernorbank i2c dev 1; i2c mw 18 1 02 1; i2c mw 18 3 fd 1
map_uppernorbank i2c dev 1; i2c mw 18 1 00 1; i2c mw 18 3 fd 1
mtdids nor0=ef000000.nor,nand0=nand
mtdparts mtdparts=ef000000.nor:128k(dtb-image),1664k(kernel),1536k(root),11264k(backup),128k(cert),1024k(u-boot);nand:-(rootfs)
nandbootaddr 2100000
nandfdtaddr 2000000
netdev eth0
nfsboot setenv bootargs root=/dev/nfs rw nfsroot=\$serverip:\$rootpath ip=\$ipaddr:\$serverip:\$gatewayip:\$netmask:\$hostname:\$netdev:off console=\$consoledev,\$baudrate \$othbootargs;tftp \$loadaddr \$bootfile;tftp \$fdtaddr \$fdtfile;bootm \$loadaddr - \$fdtaddr
norbootaddr ef080000
norfdtaddr ef040000
ramboot setenv bootargs root=/dev/ram rw console=\$consoledev,\$baudrate \$othbootargs ramdisk_size=\$ramdisk_size;tftp \$ramdiskaddr \$ramdiskfile;tftp \$loadaddr \$bootfile;tftp \$fdtaddr \$fdtfile;bootm \$loadaddr \$ramdiskaddr \$fdtaddr
ramdisk_size 120000
ramdiskaddr 2000000
ramdiskfile rootfs.ext2.gz.uboot
reflash_timeout 40
rootpath /opt/nfsroot
stderr serial
stdin serial
stdout serial
tftpflash tftpboot \$loadaddr \$uboot; protect off 0xeff40000 +\$filesize; erase 0xeff40000 +\$filesize; cp.b \$loadaddr 0xeff40000 \$filesize; protect on 0xeff40000 +\$filesize; cmp.b \$loadaddr 0xeff40000 \$filesize
ubiboot max6370_wdt_off; setenv bootargs \$bootargsubi; ubi part rootfs; ubifsmount ubi0:rootfs; ubifsload \$nandfdtaddr /boot/fdt; ubifsload \$nandbootaddr /boot/zImage; bootm \$nandbootaddr - \$nandfdtaddr
uboot u-boot.bin
backbootcmd setexpr.b reflash *0xFFA0001F;if test \$reflash -ge \$reflash_timeout; then echo BOOT NOR; run norboot; else echo BOOT NAND; run ubiboot; fi
bootcmd setexpr.b reflash *0xFFA0001F;if test \$reflash -ge \$reflash_timeout; then echo BOOT NOR; run norboot; else echo BOOT NAND; mmc detect; if fatload mmc 0:1 \$nandbootaddr zImage; then run mmcboot; else run ubiboot; fi; fi
norboot max6370_wdt_off; setenv bootargs \$bootargsnor; bootm 0xef020000 - 0xef000000
bootargsmmc root=/dev/mmcblk0p2 rootwait rw rootfstype=btrfs rootflags=subvol=@,commit=5 console=ttyS0,115200
mmcboot max6370_wdt_off; fatload mmc 0:1 \$nandfdtaddr fdt; setenv bootargs \$bootargsmmc; bootm \$nandbootaddr - \$nandfdtaddr
EOF
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment