132 lines
3.2 KiB
Bash
132 lines
3.2 KiB
Bash
|
#!/usr/bin/env bash
|
||
|
|
||
|
die() {
|
||
|
>&2 printf '\e[1;31mdied: '"$*"'\e[0m\n'
|
||
|
all_umounts
|
||
|
exit 1
|
||
|
}
|
||
|
|
||
|
l() {
|
||
|
>&2 printf '\e[1m'"$*"'\e[0m\n'
|
||
|
}
|
||
|
|
||
|
d() {
|
||
|
>&2 printf '\e[1;34m# '"$*"'\e[0m\n'
|
||
|
"$@" || die "$*"
|
||
|
}
|
||
|
|
||
|
baseimg="$1"
|
||
|
|
||
|
unpack_zip() {
|
||
|
zip -p "$1" > "$2"
|
||
|
}
|
||
|
|
||
|
unzip_xz() {
|
||
|
xz -d < "$1" > "$2"
|
||
|
}
|
||
|
|
||
|
unpack_bzip2() {
|
||
|
bzip2 -d < "$1" > "$2"
|
||
|
}
|
||
|
|
||
|
unpack_gzip() {
|
||
|
gzip -d < "$1" > "$2"
|
||
|
}
|
||
|
|
||
|
unpack_plain() {
|
||
|
ln -sf "$1" "$2"
|
||
|
}
|
||
|
|
||
|
all_umounts() {
|
||
|
umount base/root
|
||
|
umount base/boot
|
||
|
umount dest/root
|
||
|
umount dest/boot
|
||
|
kpartx -d dest/image
|
||
|
kpartx -d base/image
|
||
|
}
|
||
|
|
||
|
pii_mounts() {
|
||
|
local dir="${1:-murks}"
|
||
|
local img="${2:-$dir/image}"
|
||
|
set -- `kpartx -l "$img" | sed -e 's/ : .*//'`
|
||
|
local bootdev="/dev/mapper/$1"
|
||
|
local rootdev="/dev/mapper/$2"
|
||
|
[ -e "$bootdev" -a -e "$rootdev" ] || die "Cannot find devs [$bootdev] [$rootdev]"
|
||
|
d mount "$bootdev" "$dir/boot"
|
||
|
d mount "$rootdev" "$dir/root"
|
||
|
}
|
||
|
|
||
|
pii_mkfs() {
|
||
|
local img="${1:-murks}"
|
||
|
set -- `kpartx -l "$img" | sed -e 's/ : .*//'`
|
||
|
local bootdev="/dev/mapper/$1"
|
||
|
local rootdev="/dev/mapper/$2"
|
||
|
[ -e "$bootdev" -a -e "$rootdev" ] || die "Cannot find devs [$bootdev] [$rootdev]"
|
||
|
l format destination-boot
|
||
|
d mkfs.fat -n boot -F 32 "$bootdev"
|
||
|
l format destination-root
|
||
|
d mkfs.xfs -L root "$rootdev"
|
||
|
}
|
||
|
|
||
|
case "$baseimg" in
|
||
|
*.zip) alias unpack=d\ unpack_zip ;;
|
||
|
*.xz) alias unpack=d\ unpack_xz ;;
|
||
|
*.bz2) alias unpack=d\ unpack_bz2 ;;
|
||
|
*.gz) alias unpack=d\ unpack_gz ;;
|
||
|
*) alias unpack=d\ unpack_plain ;;
|
||
|
esac
|
||
|
|
||
|
# let's go...
|
||
|
|
||
|
d mkdir -p base/root base/boot dest/root dest/boot
|
||
|
|
||
|
l setup base-image-access
|
||
|
#[ -e base/image ] && die "base/image already exists"
|
||
|
#unpack "$baseimg" base/image
|
||
|
d kpartx -sar base/image
|
||
|
l mount base-partitions
|
||
|
pii_mounts base
|
||
|
|
||
|
l create destination image
|
||
|
d dd if=/dev/zero of=dest/image seek=$((4800*256-1)) count=1 bs=4096
|
||
|
d parted dest/image -- \
|
||
|
mklabel msdos \
|
||
|
mkpart primary fat32 4MB 132MB \
|
||
|
mkpart primary xfs 133MB -1s \
|
||
|
set 2 lba off \
|
||
|
print
|
||
|
d kpartx -sa dest/image
|
||
|
pii_mkfs dest/image
|
||
|
d parted dest/image print
|
||
|
l mount destination-partitions
|
||
|
pii_mounts dest
|
||
|
|
||
|
l install it
|
||
|
d rsync -aHAX base/boot/ dest/boot/
|
||
|
d rsync -aHAX base/root/ dest/root/
|
||
|
d rsync kernel7.img initrd7.img dest/boot
|
||
|
if grep -q '^initramfs ' dest/boot/config.txt
|
||
|
then
|
||
|
d sed -i -e 's!^initramfs !initramfs initrd7.img followkernel!' dest/boot/config.txt
|
||
|
else
|
||
|
l "# echo initramfs initrd7.img followkernel >> dest/boot/config.txt"
|
||
|
echo initramfs initrd7.img followkernel >> dest/boot/config.txt || die "echo initramfs initrd7.img followkernel >> dest/boot/config.txt"
|
||
|
fi
|
||
|
d sed -i -e 's!\<root=[^ ]*!root=LABEL=root!;s!\<rootfstype=[^ ]*!rootfstype=xfs!;s!\<init=[^ ]* *!!' dest/boot/cmdline.txt
|
||
|
d touch dest/boot/ssh
|
||
|
|
||
|
l setup initramfs for updates in future
|
||
|
d sed -i -e 's!^COMPRESS=!COMPRESS=xz!' dest/root/etc/initramfs-tools/initramfs.conf
|
||
|
|
||
|
l deactivate resize2fs-init-script
|
||
|
d rm dest/root/etc/rc*.d/*resize2fs_once
|
||
|
|
||
|
l change user pi to deac
|
||
|
d sed -i -e 's!^pi:.*!deac:x:1000:1000:Denis Knauf,,,:/home/deac:/bin/bash!' dest/root/etc/passwd
|
||
|
d sed -i -e 's!^pi:.*!deac:$6$aZRhIfbP$6ELyvhfjXP2wiIuq5gIsDPFXFWM1E8KwGhKFWKtFwJZ8CLwHXmZECzpxslwLbvgtYCScTZBJBTjp.hVjd6.2l1:16958:0:99999:7:::!' dest/root/etc/shadow
|
||
|
d sed -i -e 's!\<pi\>!deac!' dest/root/etc/group
|
||
|
d mv dest/root/home/pi dest/root/home/deac
|
||
|
|
||
|
all_umounts
|