mirror of
https://github.com/flatcar/scripts.git
synced 2025-09-23 06:31:18 +02:00
fix(build_library): Use sparse files for disk images, no useless sudo
Enable sparse files for all dd and cp commands and replace some dd commands that are really better off being truncate commands. While in the neighborhood there were a number of useless sudo commands for things that just happen to be in sbin. Call them directly instead.
This commit is contained in:
parent
f46ee8c332
commit
a1a1ed830c
@ -100,11 +100,10 @@ create_base_image() {
|
|||||||
|
|
||||||
# Build root FS image.
|
# Build root FS image.
|
||||||
info "Building ${root_fs_img}"
|
info "Building ${root_fs_img}"
|
||||||
dd if=/dev/zero of="${root_fs_img}" bs=1 count=1 \
|
truncate --size="${root_fs_bytes}" "${root_fs_img}"
|
||||||
seek=$((root_fs_bytes - 1)) status=none
|
/sbin/mkfs.ext2 -F -q -b ${fs_block_size} "${root_fs_img}" \
|
||||||
sudo mkfs.ext2 -F -q -b ${fs_block_size} "${root_fs_img}" \
|
|
||||||
"$((root_fs_bytes / fs_block_size))"
|
"$((root_fs_bytes / fs_block_size))"
|
||||||
sudo tune2fs -L "${root_fs_label}" \
|
/sbin/tune2fs -L "${root_fs_label}" \
|
||||||
-U clear \
|
-U clear \
|
||||||
-T 20091119110000 \
|
-T 20091119110000 \
|
||||||
-c 0 \
|
-c 0 \
|
||||||
@ -120,26 +119,23 @@ create_base_image() {
|
|||||||
|
|
||||||
# Build stateful FS disk image.
|
# Build stateful FS disk image.
|
||||||
info "Building ${stateful_fs_img}"
|
info "Building ${stateful_fs_img}"
|
||||||
dd if=/dev/zero of="${stateful_fs_img}" bs=1 count=1 \
|
truncate --size="${stateful_fs_bytes}" "${stateful_fs_img}"
|
||||||
seek=$((stateful_fs_bytes - 1)) status=none
|
/sbin/mkfs.ext4 -F -q "${stateful_fs_img}"
|
||||||
sudo mkfs.ext4 -F -q "${stateful_fs_img}"
|
/sbin/tune2fs -L "${stateful_fs_label}" -U "${stateful_fs_uuid}" \
|
||||||
sudo tune2fs -L "${stateful_fs_label}" -U "${stateful_fs_uuid}" \
|
|
||||||
-c 0 -i 0 "${stateful_fs_img}"
|
-c 0 -i 0 "${stateful_fs_img}"
|
||||||
mkdir -p "${stateful_fs_dir}"
|
mkdir -p "${stateful_fs_dir}"
|
||||||
sudo mount -o loop "${stateful_fs_img}" "${stateful_fs_dir}"
|
sudo mount -o loop "${stateful_fs_img}" "${stateful_fs_dir}"
|
||||||
|
|
||||||
# Build ESP disk image.
|
# Build ESP disk image.
|
||||||
info "Building ${esp_fs_img}"
|
info "Building ${esp_fs_img}"
|
||||||
dd if=/dev/zero of="${esp_fs_img}" bs=1 count=1 \
|
truncate --size="${esp_fs_bytes}" "${esp_fs_img}"
|
||||||
seek=$((esp_fs_bytes - 1)) status=none
|
/usr/sbin/mkfs.vfat "${esp_fs_img}"
|
||||||
sudo mkfs.vfat "${esp_fs_img}"
|
|
||||||
|
|
||||||
# Build OEM FS disk image.
|
# Build OEM FS disk image.
|
||||||
info "Building ${oem_fs_img}"
|
info "Building ${oem_fs_img}"
|
||||||
dd if=/dev/zero of="${oem_fs_img}" bs=1 count=1 \
|
truncate --size="${oem_fs_bytes}" "${oem_fs_img}"
|
||||||
seek=$((oem_fs_bytes - 1)) status=none
|
/sbin/mkfs.ext4 -F -q "${oem_fs_img}"
|
||||||
sudo mkfs.ext4 -F -q "${oem_fs_img}"
|
/sbin/tune2fs -L "${oem_fs_label}" -U "${oem_fs_uuid}" \
|
||||||
sudo tune2fs -L "${oem_fs_label}" -U "${oem_fs_uuid}" \
|
|
||||||
-c 0 -i 0 "${oem_fs_img}"
|
-c 0 -i 0 "${oem_fs_img}"
|
||||||
mkdir -p "${oem_fs_dir}"
|
mkdir -p "${oem_fs_dir}"
|
||||||
sudo mount -o loop "${oem_fs_img}" "${oem_fs_dir}"
|
sudo mount -o loop "${oem_fs_img}" "${oem_fs_dir}"
|
||||||
|
@ -42,7 +42,7 @@ copy_image() {
|
|||||||
local dst="${BUILD_DIR}/$2"
|
local dst="${BUILD_DIR}/$2"
|
||||||
if should_build_image $1; then
|
if should_build_image $1; then
|
||||||
echo "Creating $2 from $1..."
|
echo "Creating $2 from $1..."
|
||||||
cp "${src}" "${dst}" || die "Cannot copy $1 to $2"
|
cp --sparse=always "${src}" "${dst}" || die "Cannot copy $1 to $2"
|
||||||
else
|
else
|
||||||
mv "${src}" "${dst}" || die "Cannot move $1 to $2"
|
mv "${src}" "${dst}" || die "Cannot move $1 to $2"
|
||||||
fi
|
fi
|
||||||
|
@ -168,7 +168,7 @@ EOF
|
|||||||
local file="part_${part}"
|
local file="part_${part}"
|
||||||
local dir="dir_${part}"
|
local dir="dir_${part}"
|
||||||
local target='"${TARGET}"'
|
local target='"${TARGET}"'
|
||||||
local dd_args="bs=512 count=${size}"
|
local dd_args="bs=512 count=${size} conv=sparse"
|
||||||
local start_b=$(( start * 512 ))
|
local start_b=$(( start * 512 ))
|
||||||
local size_b=$(( size * 512 ))
|
local size_b=$(( size * 512 ))
|
||||||
echo "dd if=${target} of=${file} ${dd_args} skip=${start}" >>"${unpack}"
|
echo "dd if=${target} of=${file} ${dd_args} skip=${start}" >>"${unpack}"
|
||||||
@ -226,19 +226,19 @@ build_gpt() {
|
|||||||
|
|
||||||
# Now populate the partitions.
|
# Now populate the partitions.
|
||||||
info "Copying stateful partition..."
|
info "Copying stateful partition..."
|
||||||
$sudo dd if="$stateful_img" of="$outdev" conv=notrunc bs=512 \
|
$sudo dd if="$stateful_img" of="$outdev" conv=notrunc,sparse bs=512 \
|
||||||
seek=$(partoffset ${outdev} ${stateful_fs_num}) status=none
|
seek=$(partoffset ${outdev} ${stateful_fs_num}) status=none
|
||||||
|
|
||||||
info "Copying rootfs..."
|
info "Copying rootfs..."
|
||||||
$sudo dd if="$rootfs_img" of="$outdev" conv=notrunc bs=512 \
|
$sudo dd if="$rootfs_img" of="$outdev" conv=notrunc,sparse bs=512 \
|
||||||
seek=$(partoffset ${outdev} ${root_fs_num}) status=none
|
seek=$(partoffset ${outdev} ${root_fs_num}) status=none
|
||||||
|
|
||||||
info "Copying EFI system partition..."
|
info "Copying EFI system partition..."
|
||||||
$sudo dd if="$esp_img" of="$outdev" conv=notrunc bs=512 \
|
$sudo dd if="$esp_img" of="$outdev" conv=notrunc,sparse bs=512 \
|
||||||
seek=$(partoffset ${outdev} ${esp_fs_num}) status=none
|
seek=$(partoffset ${outdev} ${esp_fs_num}) status=none
|
||||||
|
|
||||||
info "Copying OEM partition..."
|
info "Copying OEM partition..."
|
||||||
$sudo dd if="$oem_img" of="$outdev" conv=notrunc bs=512 \
|
$sudo dd if="$oem_img" of="$outdev" conv=notrunc,sparse bs=512 \
|
||||||
seek=$(partoffset ${outdev} ${oem_fs_num}) status=none
|
seek=$(partoffset ${outdev} ${oem_fs_num}) status=none
|
||||||
|
|
||||||
# Pre-set "sucessful" bit in gpt, so we will never mark-for-death
|
# Pre-set "sucessful" bit in gpt, so we will never mark-for-death
|
||||||
@ -317,11 +317,11 @@ update_partition_table() {
|
|||||||
-t ${tguid} -u ${uguid} ${dst_img}
|
-t ${tguid} -u ${uguid} ${dst_img}
|
||||||
if [ "${label}" != "STATE" ]; then
|
if [ "${label}" != "STATE" ]; then
|
||||||
# Copy source partition as-is.
|
# Copy source partition as-is.
|
||||||
dd if="${src_img}" of="${dst_img}" conv=notrunc bs=512 \
|
dd if="${src_img}" of="${dst_img}" conv=notrunc,sparse bs=512 \
|
||||||
skip=${src_start} seek=${dst_start} count=${size} status=none
|
skip=${src_start} seek=${dst_start} count=${size} status=none
|
||||||
else
|
else
|
||||||
# Copy new stateful partition into place.
|
# Copy new stateful partition into place.
|
||||||
dd if="${src_state}" of="${dst_img}" conv=notrunc bs=512 \
|
dd if="${src_state}" of="${dst_img}" conv=notrunc,sparse bs=512 \
|
||||||
seek=${dst_start} status=none
|
seek=${dst_start} status=none
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
@ -164,7 +164,7 @@ unpack_source_disk() {
|
|||||||
TEMP_STATE="${VM_TMP_DIR}"/part_${NUM_STATEFUL}
|
TEMP_STATE="${VM_TMP_DIR}"/part_${NUM_STATEFUL}
|
||||||
# Copy the replacement STATE image if it is set
|
# Copy the replacement STATE image if it is set
|
||||||
if [[ -n "${alternate_state_image}" ]]; then
|
if [[ -n "${alternate_state_image}" ]]; then
|
||||||
cp "${alternate_state_image}" "${TEMP_STATE}"
|
cp --sparse=always "${alternate_state_image}" "${TEMP_STATE}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
TEMP_PMBR="${VM_TMP_DIR}"/pmbr
|
TEMP_PMBR="${VM_TMP_DIR}"/pmbr
|
||||||
@ -216,14 +216,14 @@ install_oem_package() {
|
|||||||
# Write the vm disk image to the target directory in the proper format
|
# Write the vm disk image to the target directory in the proper format
|
||||||
write_vm_disk() {
|
write_vm_disk() {
|
||||||
info "Writing partitions to new disk image"
|
info "Writing partitions to new disk image"
|
||||||
dd if="${TEMP_ROOTFS}" of="${VM_TMP_IMG}" conv=notrunc bs=512 \
|
dd if="${TEMP_ROOTFS}" of="${VM_TMP_IMG}" conv=notrunc,sparse \
|
||||||
seek=$(partoffset ${VM_TMP_IMG} ${NUM_ROOTFS_A})
|
bs=512 seek=$(partoffset ${VM_TMP_IMG} ${NUM_ROOTFS_A})
|
||||||
dd if="${TEMP_STATE}" of="${VM_TMP_IMG}" conv=notrunc bs=512 \
|
dd if="${TEMP_STATE}" of="${VM_TMP_IMG}" conv=notrunc,sparse \
|
||||||
seek=$(partoffset ${VM_TMP_IMG} ${NUM_STATEFUL})
|
bs=512 seek=$(partoffset ${VM_TMP_IMG} ${NUM_STATEFUL})
|
||||||
dd if="${TEMP_ESP}" of="${VM_TMP_IMG}" conv=notrunc bs=512 \
|
dd if="${TEMP_ESP}" of="${VM_TMP_IMG}" conv=notrunc,sparse \
|
||||||
seek=$(partoffset ${VM_TMP_IMG} ${NUM_ESP})
|
bs=512 seek=$(partoffset ${VM_TMP_IMG} ${NUM_ESP})
|
||||||
dd if="${TEMP_OEM}" of="${VM_TMP_IMG}" conv=notrunc bs=512 \
|
dd if="${TEMP_OEM}" of="${VM_TMP_IMG}" conv=notrunc,sparse \
|
||||||
seek=$(partoffset ${VM_TMP_IMG} ${NUM_OEM})
|
bs=512 seek=$(partoffset ${VM_TMP_IMG} ${NUM_OEM})
|
||||||
|
|
||||||
if [[ $(_get_vm_opt HYBRID_MBR) -eq 1 ]]; then
|
if [[ $(_get_vm_opt HYBRID_MBR) -eq 1 ]]; then
|
||||||
info "Creating hybrid MBR"
|
info "Creating hybrid MBR"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user