Repository navigation
Expand file tree
/
Copy pathreset-build.sh
More file actions
145 lines (125 loc) · 5.5 KB
/
Copy pathreset-build.sh
File metadata and controls
145 lines (125 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
#
# reset-build.sh — recreate the LFS disk image and prepare it for a fresh build.
#
# Does the mechanical, easy-to-get-wrong part of resetting an LFS 12.0 build:
# 1. (re)create the raw disk image
# 2. attach it to a loop device
# 3. partition it (msdos: /boot + /)
# 4. make ext4 filesystems
# 5. mount them at $LFS
# 6. rewrite etc/fstab in this repo with the REAL new UUIDs
#
# It stops there. The chapter work (host check, sources, jhalfs or the manual
# book steps) is up to you afterwards — see README / the build scripts.
#
# DESTRUCTIVE: partitioning + mkfs wipe the target image. Creating a new image
# over an existing one requires FORCE=1 so you can't nuke a good build by accident.
#
# Usage:
# sudo ./reset-build.sh # refuses if the image already exists
# sudo FORCE=1 ./reset-build.sh # recreate even if the image exists
# sudo IMG=/path/to.img LFS=/mnt/lfs SIZE=20G ./reset-build.sh
#
set -euo pipefail
# ---- config (override via env) ---------------------------------------------
IMG="${IMG:-$HOME/lfs-build/lfs-disk.img}"
LFS="${LFS:-/mnt/lfs}"
SIZE="${SIZE:-20G}"
BOOT_MIB_END="${BOOT_MIB_END:-512}" # /boot = 1MiB .. BOOT_MIB_END
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FSTAB="$REPO_DIR/etc/fstab"
# ---- preflight --------------------------------------------------------------
if [[ $EUID -ne 0 ]]; then
echo "FATAL: must run as root (losetup/parted/mkfs/mount need it). Use: sudo $0" >&2
exit 1
fi
for t in qemu-img losetup parted mkfs.ext4 blkid mountpoint; do
command -v "$t" >/dev/null 2>&1 || { echo "FATAL: missing required tool: $t" >&2; exit 1; }
done
echo ">>> Target image : $IMG"
echo ">>> Mount point : $LFS"
echo ">>> Image size : $SIZE"
echo ">>> /boot end : ${BOOT_MIB_END}MiB"
echo
# ---- 0. guard against clobbering a good image -------------------------------
if [[ -e "$IMG" && "${FORCE:-0}" != "1" ]]; then
echo "REFUSING: $IMG already exists. Re-run with FORCE=1 to recreate it (this ERASES it)." >&2
exit 1
fi
# ---- cleanup trap: always detach the loop device on exit --------------------
LOOP=""
cleanup() { [[ -n "$LOOP" ]] && losetup -d "$LOOP" 2>/dev/null || true; }
# only detach on ERROR; on success we leave filesystems mounted for the build.
trap '[[ $? -ne 0 ]] && cleanup' EXIT
# ---- 1. create the image ----------------------------------------------------
mkdir -p "$(dirname "$IMG")"
# make sure nothing from a previous run is still mounted under $LFS
if mountpoint -q "$LFS/boot"; then umount "$LFS/boot"; fi
if mountpoint -q "$LFS"; then umount "$LFS"; fi
echo ">>> Creating raw image ($SIZE) ..."
rm -f "$IMG"
qemu-img create -f raw "$IMG" "$SIZE"
# ---- 2. attach --------------------------------------------------------------
LOOP="$(losetup -fP --show "$IMG")"
echo ">>> Attached at $LOOP"
# ---- 3. partition (msdos: p1=/boot, p2=/) -----------------------------------
echo ">>> Partitioning ..."
parted -s "$LOOP" mklabel msdos
parted -s "$LOOP" -- mkpart primary ext4 1MiB "${BOOT_MIB_END}MiB"
parted -s "$LOOP" -- mkpart primary ext4 "${BOOT_MIB_END}MiB" 100%
parted -s "$LOOP" set 1 boot on
partprobe "$LOOP" 2>/dev/null || true
sleep 1
BOOT_PART="${LOOP}p1"
ROOT_PART="${LOOP}p2"
[[ -b "$BOOT_PART" && -b "$ROOT_PART" ]] || { echo "FATAL: partitions did not appear" >&2; exit 1; }
# ---- 4. filesystems ---------------------------------------------------------
echo ">>> Formatting ext4 ..."
mkfs.ext4 -q -L lfsboot "$BOOT_PART"
mkfs.ext4 -q -L lfsroot "$ROOT_PART"
# ---- 5. mount ---------------------------------------------------------------
echo ">>> Mounting at $LFS ..."
mkdir -p "$LFS"
mount "$ROOT_PART" "$LFS"
mkdir -p "$LFS/boot"
mount "$BOOT_PART" "$LFS/boot"
# ---- 6. rewrite repo etc/fstab with real UUIDs ------------------------------
ROOT_UUID="$(blkid -s UUID -o value "$ROOT_PART")"
BOOT_UUID="$(blkid -s UUID -o value "$BOOT_PART")"
echo ">>> root UUID = $ROOT_UUID"
echo ">>> boot UUID = $BOOT_UUID"
if [[ -f "$FSTAB" ]]; then cp -a "$FSTAB" "$FSTAB.bak.$(date +%s)"; fi
cat > "$FSTAB" <<EOF
# Begin /etc/fstab
# Regenerated by reset-build.sh on $(date -u +%Y-%m-%dT%H:%M:%SZ)
# file system mount-point type options dump fsck
UUID=$ROOT_UUID / ext4 defaults 1 1
UUID=$BOOT_UUID /boot ext4 defaults 1 2
proc /proc proc nosuid,noexec,nodev 0 0
sysfs /sys sysfs nosuid,noexec,nodev 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /run tmpfs defaults 0 0
devtmpfs /dev devtmpfs mode=0755,nosuid 0 0
tmpfs /dev/shm tmpfs nosuid,nodev 0 0
cgroup2 /sys/fs/cgroup cgroup2 nosuid,noexec,nodev 0 0
# End /etc/fstab
EOF
echo ">>> Wrote $FSTAB (backup saved alongside)"
cat <<EOF
================================================================================
Disk reset complete.
image : $IMG
loop : $LOOP (left attached)
root : $ROOT_PART -> $LFS (ext4, UUID $ROOT_UUID)
boot : $BOOT_PART -> $LFS/boot (ext4, UUID $BOOT_UUID)
Next steps:
export LFS=$LFS
# host sanity check:
bash packages/checkfiles.sh
# then: create sources dir, download LFS 12.0 wget-list, and build
# (jhalfs with boot/config-6.4.12 as the kernel .config, or the manual book).
When done building, detach with:
umount $LFS/boot && umount $LFS && losetup -d $LOOP
================================================================================
EOF