Skip to content

Commit 116ba4b

Browse files
authored
[baseimage]: allocate varlog disk in the initramfs stage (#936)
moving to initramfs unifies disk allocate on different platforms. use fallocate instead of dd to speed up the disk allocation. By default, mkfs.ext4 has -E discard option which discards the blocks at the mkfs time, also speed up the initialization time.
1 parent 48f1a63 commit 116ba4b

File tree

5 files changed

+47
-48
lines changed

5 files changed

+47
-48
lines changed

build_debian.sh

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ sudo chmod +x $FILESYSTEM_ROOT/etc/initramfs-tools/scripts/init-premount/arista-
132132
## 2. Bind-mount docker working directory (docker aufs cannot work over aufs rootfs)
133133
sudo cp files/initramfs-tools/union-mount $FILESYSTEM_ROOT/etc/initramfs-tools/scripts/init-bottom/union-mount
134134
sudo chmod +x $FILESYSTEM_ROOT/etc/initramfs-tools/scripts/init-bottom/union-mount
135+
sudo cp files/initramfs-tools/varlog $FILESYSTEM_ROOT/etc/initramfs-tools/scripts/init-bottom/varlog
136+
sudo chmod +x $FILESYSTEM_ROOT/etc/initramfs-tools/scripts/init-bottom/varlog
135137
sudo cp files/initramfs-tools/union-fsck $FILESYSTEM_ROOT/etc/initramfs-tools/hooks/union-fsck
136138
sudo chmod +x $FILESYSTEM_ROOT/etc/initramfs-tools/hooks/union-fsck
137139
sudo chroot $FILESYSTEM_ROOT update-initramfs -u

files/initramfs-tools/arista-convertfs.j2

+2-29
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,6 @@ run_cmd() {
8282
fi
8383
}
8484

85-
create_varlog_file() {
86-
local err_msg="Error: create var-log ext4 file"
87-
local cmd="[ -n "$varlog_size" ] && mkdir -p $root_mnt/disk-img && dd if=/dev/zero of=$root_mnt/disk-img/var-log.ext4 count=$((2048*$varlog_size)) && mke2fs -t ext4 -q -F $root_mnt/disk-img/var-log.ext4"
88-
run_cmd "$cmd" "$err_msg"
89-
}
90-
91-
mount_and_create_varlog_file() {
92-
[ -z "$varlog_size" ] && exit 0
93-
mkdir -p "$root_mnt"
94-
mount -t ext4 "$root_dev" "$root_mnt"
95-
# exit when the var_log.ext4 exists and the size matches
96-
if [ -e "$root_mnt/disk-img/var-log.ext4" ]; then
97-
cur_varlog_size=$(ls -l $root_mnt/disk-img/var-log.ext4 | awk '{print $5}')
98-
if [ $cur_varlog_size == $((1024*1024*$varlog_size)) ]; then
99-
exit 0
100-
fi
101-
fi
102-
create_varlog_file
103-
umount "$root_mnt"
104-
exit 0
105-
}
106-
10785
# Extract kernel parameters
10886
set -- $(cat /proc/cmdline)
10987
for x in "$@"; do
@@ -114,9 +92,6 @@ for x in "$@"; do
11492
Aboot=*)
11593
aboot_flag="${x#Aboot=}"
11694
;;
117-
varlog_size=*)
118-
varlog_size="${x#varlog_size=}"
119-
;;
12095
loop=*)
12196
x1="${x#loop=}"
12297
image_dir="${x1%/*}"
@@ -135,8 +110,8 @@ if ! wait_for_root_dev; then
135110
exit 1
136111
fi
137112

138-
# mount, create varlog file and exit when the root is ext4
139-
blkid | grep "$root_dev.*vfat" -q || mount_and_create_varlog_file
113+
# exit when the root is ext4
114+
blkid | grep "$root_dev.*vfat" -q || exit 0
140115

141116
# Get flash dev name
142117
if [ -z "$block_flash" ]; then
@@ -201,5 +176,3 @@ run_cmd "$cmd" "$err_msg"
201176
err_msg="Error: copying files form $tmp_mnt to $root_mnt failed"
202177
cmd="cp -a $tmp_mnt/. $root_mnt/"
203178
run_cmd "$cmd" "$err_msg"
204-
205-
create_varlog_file

files/initramfs-tools/union-mount.j2

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/bin/sh -e
2+
3+
PREREQS="varlog"
4+
5+
prereqs() { echo "$PREREQS"; }
6+
27
case $1 in
38
prereqs)
9+
prereqs
410
exit 0
511
;;
612
esac

files/initramfs-tools/varlog

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh -e
2+
3+
PREREQS=""
4+
5+
prereqs() { echo "$PREREQS"; }
6+
7+
case $1 in
8+
prereqs)
9+
prereqs
10+
exit 0
11+
;;
12+
esac
13+
14+
# Extract kernel parameters
15+
set -- $(cat /proc/cmdline)
16+
for x in "$@"; do
17+
case "$x" in
18+
varlog_size=*)
19+
varlog_size="${x#varlog_size=}"
20+
esac
21+
done
22+
23+
[ -z "$varlog_size" ] && exit 0
24+
25+
# exit when the var_log.ext4 exists and the size matches
26+
if [ -e "${rootmnt}/host/disk-img/var-log.ext4" ]; then
27+
cur_varlog_size=$(ls -l ${rootmnt}/host/disk-img/var-log.ext4 | awk '{print $5}')
28+
if [ $cur_varlog_size == $((1024*1024*$varlog_size)) ]; then
29+
exit 0
30+
else
31+
rm -rf ${rootmnt}/host/disk-img
32+
fi
33+
fi
34+
35+
# create varlog disk
36+
mkdir -p ${rootmnt}/host/disk-img && ${rootmnt}/usr/bin/fallocate -l "$varlog_size"M ${rootmnt}/host/disk-img/var-log.ext4 && mkfs.ext4 -q -F ${rootmnt}/host/disk-img/var-log.ext4

installer/x86_64/install.sh

+1-19
Original file line numberDiff line numberDiff line change
@@ -450,24 +450,6 @@ TAR_EXTRA_OPTION="--numeric-owner"
450450
mkdir -p $demo_mnt/$image_dir/$DOCKERFS_DIR
451451
unzip -op $ONIE_INSTALLER_PAYLOAD "$FILESYSTEM_DOCKERFS" | tar xz $TAR_EXTRA_OPTION -f - -C $demo_mnt/$image_dir/$DOCKERFS_DIR
452452
453-
# Create loop device for /var/log to limit its size to $VAR_LOG_SIZE MB
454-
if [ -f $demo_mnt/disk-img/var-log.ext4 ]; then
455-
current_log_size_mb=$(ls -l --block-size=M $demo_mnt/disk-img/var-log.ext4 | cut -f5 -d" ")
456-
if [ "$current_log_size_mb" = "$VAR_LOG_SIZE"M ]; then
457-
echo "Log file system already exists. Size: ${VAR_LOG_SIZE}MB"
458-
VAR_LOG_SIZE=0
459-
else
460-
rm -rf $demo_mnt/disk-img
461-
fi
462-
fi
463-
464-
if [ "$VAR_LOG_SIZE" != "0" ]; then
465-
echo "Creating new log file system. Size: ${VAR_LOG_SIZE}MB"
466-
mkdir -p $demo_mnt/disk-img
467-
dd if=/dev/zero of=$demo_mnt/disk-img/var-log.ext4 count=$((2048*$VAR_LOG_SIZE))
468-
mkfs.ext4 -q $demo_mnt/disk-img/var-log.ext4 -F
469-
fi
470-
471453
if [ "$install_env" = "onie" ]; then
472454
# Store machine description in target file system
473455
cp /etc/machine.conf $demo_mnt
@@ -563,7 +545,7 @@ menuentry '$demo_grub_entry' {
563545
insmod ext2
564546
linux /$image_dir/boot/vmlinuz-3.16.0-4-amd64 root=$demo_dev rw $GRUB_CMDLINE_LINUX \
565547
loop=$image_dir/$FILESYSTEM_SQUASHFS loopfstype=squashfs \
566-
apparmor=1 security=apparmor $ONIE_PLATFORM_EXTRA_CMDLINE_LINUX
548+
apparmor=1 security=apparmor varlog_size=$VAR_LOG_SIZE $ONIE_PLATFORM_EXTRA_CMDLINE_LINUX
567549
echo 'Loading $demo_volume_label $demo_type initial ramdisk ...'
568550
initrd /$image_dir/boot/initrd.img-3.16.0-4-amd64
569551
}

0 commit comments

Comments
 (0)