Separate rootfs and image build steps
This commit is contained in:
parent
571b6dd9c4
commit
d3e1b918de
5 changed files with 149 additions and 135 deletions
|
@ -2,7 +2,9 @@ before_install:
|
|||
- sudo apt-get update
|
||||
- sudo apt-get install -y bash quilt parted qemu-user-static debootstrap zerofree dosfstools libcap2-bin grep rsync
|
||||
|
||||
install: sudo ./run ./build.sh
|
||||
install:
|
||||
- sudo ./run ./build.sh
|
||||
- sudo ./run ./mkimg.sh
|
||||
|
||||
script:
|
||||
- ./run ./test/partition_alignment.sh
|
||||
|
|
|
@ -37,7 +37,7 @@ file is `<tool>[:<debian-package>]`.
|
|||
Build
|
||||
-----
|
||||
|
||||
Run `sudo ./run ./build.sh` to build the image.
|
||||
Run `sudo ./run ./build.sh && sudo ./run ./mkimg.sh` to build the image.
|
||||
The following files will be created:
|
||||
|
||||
* `rootfs/` - the root file system (`/` and `/boot/` partitions)
|
||||
|
|
138
build.sh
138
build.sh
|
@ -44,59 +44,17 @@ apply_file() {
|
|||
install -m "$MODE" "$SRC" "$DST"
|
||||
}
|
||||
|
||||
unmount() {
|
||||
if [ -z "$1" ]; then
|
||||
local DIR=$PWD
|
||||
else
|
||||
local DIR=$1
|
||||
fi
|
||||
|
||||
while mount | grep -q "$DIR"; do
|
||||
local LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
|
||||
|
||||
for loc in $LOCS; do
|
||||
umount "$loc"
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
unmount_image() {
|
||||
sync
|
||||
sleep 1
|
||||
local LOOP_DEVICES=$(losetup -j "$1" | cut -f 1 -d ':')
|
||||
|
||||
for LOOP_DEV in $LOOP_DEVICES; do
|
||||
if [ -n "$LOOP_DEV" ]; then
|
||||
local MOUNTED_DIR=$(mount | grep "$(basename "$LOOP_DEV")" | head -n 1 | cut -f 3 -d ' ')
|
||||
|
||||
if [ -n "$MOUNTED_DIR" ] && [ "$MOUNTED_DIR" != "/" ]; then
|
||||
unmount "$(dirname "$MOUNTED_DIR")"
|
||||
fi
|
||||
|
||||
sleep 1
|
||||
losetup -d "$LOOP_DEV"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
##
|
||||
# Prepare environment.
|
||||
#
|
||||
if [ "$(id -u)" != '0' ]; then
|
||||
echo 'Please run as root' 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$DEPLOY_DIR"
|
||||
mkdir -p "$MOUNT_DIR"
|
||||
if [ -d "$ROOTFS_DIR" ]; then
|
||||
echo "$ROOTFS_DIR already exist"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd "$BASE_DIR"
|
||||
|
||||
#################################
|
||||
# START OF ROOT FS BUILD #
|
||||
#################################
|
||||
if [ ! -d "$ROOTFS_DIR" ]; then
|
||||
#################################
|
||||
mkdir "$ROOTFS_DIR"
|
||||
|
||||
##
|
||||
# Bootstrap a basic Debian system.
|
||||
|
@ -300,89 +258,3 @@ rm -f "$ROOTFS_DIR/usr/sbin/policy-rc.d"
|
|||
# Clean Apt cache.
|
||||
#
|
||||
rm -rf "$ROOTFS_DIR/var/cache/apt/archives/*"
|
||||
|
||||
########################
|
||||
fi
|
||||
########################
|
||||
# END OF ROOT FS BUILD #
|
||||
########################
|
||||
|
||||
##
|
||||
# Prepare image file systems.
|
||||
#
|
||||
rm -f "$IMG_FILE"
|
||||
|
||||
BOOT_SIZE=$(du --apparent-size -s "$ROOTFS_DIR/boot" --block-size=1 | cut -f 1)
|
||||
TOTAL_SIZE=$(du --apparent-size -s "$ROOTFS_DIR" --block-size=1 | cut -f 1)
|
||||
|
||||
IMG_SIZE=$((BOOT_SIZE + TOTAL_SIZE + (800 * 1024 * 1024)))
|
||||
|
||||
truncate -s $IMG_SIZE "$IMG_FILE"
|
||||
|
||||
fdisk -H 255 -S 63 "$IMG_FILE" <<EOF
|
||||
o
|
||||
n
|
||||
|
||||
|
||||
8192
|
||||
+$((BOOT_SIZE * 2 / 512))
|
||||
p
|
||||
t
|
||||
c
|
||||
n
|
||||
|
||||
|
||||
8192
|
||||
|
||||
|
||||
p
|
||||
w
|
||||
EOF
|
||||
|
||||
PARTED_OUT=$(parted -s "$IMG_FILE" unit b print)
|
||||
|
||||
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 1' | xargs echo -n | cut -d ' ' -f 2 | tr -d B)
|
||||
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 1' | xargs echo -n | cut -d ' ' -f 4 | tr -d B)
|
||||
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 2' | xargs echo -n | cut -d ' ' -f 2 | tr -d B)
|
||||
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 2' | xargs echo -n | cut -d ' ' -f 4 | tr -d B)
|
||||
|
||||
BOOT_DEV=$(losetup --show -f -o $BOOT_OFFSET --sizelimit $BOOT_LENGTH "$IMG_FILE")
|
||||
ROOT_DEV=$(losetup --show -f -o $ROOT_OFFSET --sizelimit $ROOT_LENGTH "$IMG_FILE")
|
||||
|
||||
mkdosfs -n boot -F 32 -v $BOOT_DEV > /dev/null
|
||||
mkfs.ext4 -O ^huge_file $ROOT_DEV > /dev/null
|
||||
|
||||
##
|
||||
# Mount image file systems.
|
||||
#
|
||||
mkdir -p "$MOUNT_DIR"
|
||||
mount -v $ROOT_DEV "$MOUNT_DIR" -t ext4
|
||||
|
||||
mkdir -p "$MOUNT_DIR/boot/"
|
||||
mount -v $BOOT_DEV "$MOUNT_DIR/boot/" -t vfat
|
||||
|
||||
##
|
||||
# Copy root file system to image file systems.
|
||||
#
|
||||
rsync -aHAXx "$ROOTFS_DIR/" "$MOUNT_DIR/"
|
||||
|
||||
##
|
||||
# Store file system UUIDs to configuration files.
|
||||
#
|
||||
IMGID="$(fdisk -l "$IMG_FILE" | sed -n 's/Disk identifier: 0x\([^ ]*\)/\1/p')"
|
||||
|
||||
BOOT_PARTUUID="$IMGID-01"
|
||||
ROOT_PARTUUID="$IMGID-02"
|
||||
|
||||
sed -i "s/PARTUUID=BOOTUUID/PARTUUID=$BOOT_PARTUUID/" "$MOUNT_DIR/etc/fstab"
|
||||
sed -i "s/PARTUUID=ROOTUUID/PARTUUID=$ROOT_PARTUUID/" "$MOUNT_DIR/etc/fstab"
|
||||
sed -i "s/PARTUUID=ROOTUUID/PARTUUID=$ROOT_PARTUUID/" "$MOUNT_DIR/boot/cmdline.txt"
|
||||
|
||||
##
|
||||
# Unmount all file systems and minimize image file for distribution.
|
||||
#
|
||||
ROOT_DEV=$(mount | grep "$MOUNT_DIR " | cut -f 1 -d ' ')
|
||||
umount "$MOUNT_DIR/boot/"
|
||||
umount "$MOUNT_DIR"
|
||||
zerofree -v "$ROOT_DEV"
|
||||
unmount_image "$IMG_FILE"
|
||||
|
|
136
mkimg.sh
Normal file
136
mkimg.sh
Normal file
|
@ -0,0 +1,136 @@
|
|||
#!/bin/false
|
||||
|
||||
unmount() {
|
||||
if [ -z "$1" ]; then
|
||||
local DIR=$PWD
|
||||
else
|
||||
local DIR=$1
|
||||
fi
|
||||
|
||||
while mount | grep -q "$DIR"; do
|
||||
local LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
|
||||
|
||||
for loc in $LOCS; do
|
||||
umount "$loc"
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
unmount_image() {
|
||||
sync
|
||||
sleep 1
|
||||
local LOOP_DEVICES=$(losetup -j "$1" | cut -f 1 -d ':')
|
||||
|
||||
for LOOP_DEV in $LOOP_DEVICES; do
|
||||
if [ -n "$LOOP_DEV" ]; then
|
||||
local MOUNTED_DIR=$(mount | grep "$(basename "$LOOP_DEV")" | head -n 1 | cut -f 3 -d ' ')
|
||||
|
||||
if [ -n "$MOUNTED_DIR" ] && [ "$MOUNTED_DIR" != "/" ]; then
|
||||
unmount "$(dirname "$MOUNTED_DIR")"
|
||||
fi
|
||||
|
||||
sleep 1
|
||||
losetup -d "$LOOP_DEV"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
if [ "$(id -u)" != '0' ]; then
|
||||
echo 'Please run as root' 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$ROOTFS_DIR" ]; then
|
||||
echo "$ROOTFS_DIR is not a directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$ROOTFS_DIR/boot" ]; then
|
||||
echo "$ROOTFS_DIR/boot is not a directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$(dirname "$IMG_FILE")" ]; then
|
||||
echo "$(dirname "$IMG_FILE") is not a directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
##
|
||||
# Prepare image file systems.
|
||||
#
|
||||
rm -f "$IMG_FILE"
|
||||
|
||||
BOOT_SIZE=$(du --apparent-size -s "$ROOTFS_DIR/boot" --block-size=1 | cut -f 1)
|
||||
TOTAL_SIZE=$(du --apparent-size -s "$ROOTFS_DIR" --block-size=1 | cut -f 1)
|
||||
|
||||
IMG_SIZE=$((BOOT_SIZE + TOTAL_SIZE + (800 * 1024 * 1024)))
|
||||
|
||||
truncate -s $IMG_SIZE "$IMG_FILE"
|
||||
|
||||
fdisk -H 255 -S 63 "$IMG_FILE" <<EOF
|
||||
o
|
||||
n
|
||||
|
||||
|
||||
8192
|
||||
+$((BOOT_SIZE * 2 / 512))
|
||||
p
|
||||
t
|
||||
c
|
||||
n
|
||||
|
||||
|
||||
8192
|
||||
|
||||
|
||||
p
|
||||
w
|
||||
EOF
|
||||
|
||||
PARTED_OUT=$(parted -s "$IMG_FILE" unit b print)
|
||||
|
||||
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 1' | xargs echo -n | cut -d ' ' -f 2 | tr -d B)
|
||||
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 1' | xargs echo -n | cut -d ' ' -f 4 | tr -d B)
|
||||
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 2' | xargs echo -n | cut -d ' ' -f 2 | tr -d B)
|
||||
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 2' | xargs echo -n | cut -d ' ' -f 4 | tr -d B)
|
||||
|
||||
BOOT_DEV=$(losetup --show -f -o $BOOT_OFFSET --sizelimit $BOOT_LENGTH "$IMG_FILE")
|
||||
ROOT_DEV=$(losetup --show -f -o $ROOT_OFFSET --sizelimit $ROOT_LENGTH "$IMG_FILE")
|
||||
|
||||
mkdosfs -n boot -F 32 -v $BOOT_DEV > /dev/null
|
||||
mkfs.ext4 -O ^huge_file $ROOT_DEV > /dev/null
|
||||
|
||||
##
|
||||
# Mount image file systems.
|
||||
#
|
||||
mkdir -p "$MOUNT_DIR"
|
||||
mount -v $ROOT_DEV "$MOUNT_DIR" -t ext4
|
||||
|
||||
mkdir -p "$MOUNT_DIR/boot/"
|
||||
mount -v $BOOT_DEV "$MOUNT_DIR/boot/" -t vfat
|
||||
|
||||
##
|
||||
# Copy root file system to image file systems.
|
||||
#
|
||||
rsync -aHAXx "$ROOTFS_DIR/" "$MOUNT_DIR/"
|
||||
|
||||
##
|
||||
# Store file system UUIDs to configuration files.
|
||||
#
|
||||
IMGID="$(fdisk -l "$IMG_FILE" | sed -n 's/Disk identifier: 0x\([^ ]*\)/\1/p')"
|
||||
|
||||
BOOT_PARTUUID="$IMGID-01"
|
||||
ROOT_PARTUUID="$IMGID-02"
|
||||
|
||||
sed -i "s/PARTUUID=BOOTUUID/PARTUUID=$BOOT_PARTUUID/" "$MOUNT_DIR/etc/fstab"
|
||||
sed -i "s/PARTUUID=ROOTUUID/PARTUUID=$ROOT_PARTUUID/" "$MOUNT_DIR/etc/fstab"
|
||||
sed -i "s/PARTUUID=ROOTUUID/PARTUUID=$ROOT_PARTUUID/" "$MOUNT_DIR/boot/cmdline.txt"
|
||||
|
||||
##
|
||||
# Unmount all file systems and minimize image file for distribution.
|
||||
#
|
||||
ROOT_DEV=$(mount | grep "$MOUNT_DIR " | cut -f 1 -d ' ')
|
||||
umount "$MOUNT_DIR/boot/"
|
||||
umount "$MOUNT_DIR"
|
||||
zerofree -v "$ROOT_DEV"
|
||||
unmount_image "$IMG_FILE"
|
4
run
4
run
|
@ -55,4 +55,8 @@ dependencies_check() {
|
|||
|
||||
dependencies_check "$BASE_DIR/depends"
|
||||
|
||||
cd "$BASE_DIR"
|
||||
|
||||
mkdir -p "$DEPLOY_DIR"
|
||||
|
||||
exec bash -e $@
|
||||
|
|
Reference in a new issue