Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Last active March 27, 2017 04:44
Show Gist options
  • Save svanellewee/f0254497add9e856051fcac9fa31e0a2 to your computer and use it in GitHub Desktop.
Save svanellewee/f0254497add9e856051fcac9fa31e0a2 to your computer and use it in GitHub Desktop.
Linux from scratch
export LFS=/mnt/lfs
DISK_IMAGE=disk-img.raw
IMAGE_SIZE=8.1G
function make_image {
echo "making image"
qemu-img create -f raw ${DISK_IMAGE} ${IMAGE_SIZE}
}
function partition {
echo "Setting up partitions"
if [[ ! -f ${DISK_IMAGE} ]]; then
echo "but let's first"
make_image;
fi
# taken from https://superuser.com/questions/332252/creating-and-formating-a-partition-using-a-bash-script
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk ${DISK_IMAGE}
n # new partition
p # primary partition
1 # partition number 1
# default - start at beginning of disk
+100M # 100 MB boot parttion
a # make bootable
n # new partition
p # primary partition
2 # partion number 2
# default, start immediately after preceding partition
+2048M # default, extend partition to end of disk
n # root partition
p #
3 #
#
+6000M #
w # write the partition table
q # and we're done
EOF
}
# Should now look like this
# fdisk -l disk-img.raw
#
# Disk disk-img.raw: 8.1 GiB, 8697309184 bytes, 16986932 sectors
# Units: sectors of 1 * 512 = 512 bytes
# Sector size (logical/physical): 512 bytes / 512 bytes
# I/O size (minimum/optimal): 512 bytes / 512 bytes
# Disklabel type: dos
# Disk identifier: 0x06905a80
# Device Boot Start End Sectors Size Id Type
# disk-img.raw1 * 2048 206847 204800 100M 83 Linux
# disk-img.raw2 206848 4401151 4194304 2G 83 Linux
# disk-img.raw3 4401152 16689151 12288000 5.9G 83 Linux
#
function do_losetup {
echo "Now making loopbacks"
if [[ ! -f ${DISK_IMAGE} ]]; then
echo "but before that even.."
partition;
fi
sudo losetup /dev/loop0 ${DISK_IMAGE}
sudo losetup --offset $((2048*512)) --sizelimit 100M /dev/loop1 /dev/loop0
sudo losetup --offset $((206848*512)) --sizelimit 2G /dev/loop2 /dev/loop0
sudo losetup --offset $((4401152*512)) --sizelimit 5.9G /dev/loop3 /dev/loop0
}
function format_fs {
if [[ ! -f ${DISK_IMAGE} ]]; then
echo "but before that even.."
do_losetup;
fi
sudo mkfs -v -t ext4 /dev/loop1
sudo mkfs -v -t ext4 /dev/loop3
sudo mkswap /dev/loop2
}
function mount_root {
if [[ ! -f ${DISK_IMAGE} ]]; then
echo "format first..."
format_fs;
fi
echo "mounting"
sudo mkdir -p ${LFS}
sudo mount -v -t ext4 /dev/loop3 ${LFS}
}
opt=$1
case "${opt}" in
"image")
make_image
;;
"part")
partition
;;
"lo")
do_losetup
;;
"dof")
format_fs
;;
"mnts")
mount_root
;;
"clean")
sudo umount ${LFS}
sudo losetup -d /dev/loop3
sudo losetup -d /dev/loop2
sudo losetup -d /dev/loop1
sudo losetup -d /dev/loop0
rm -fr ${DISK_IMAGE}
;;
esac
# https://isotope11.com/blog/linux-from-scratch-part-1
# http://nairobi-embedded.org/transfering_buildroot_fs_data_into_qemu_disk_images.html
IMAGE=disk-img.raw
IMAGE_SIZE=8.1G
# 6 Gig ext2, 2 Gig Swap, 100meg boot
$(IMAGE):
qemu-img create -f raw $(IMAGE) $(IMAGE_SIZE)
loop0: $(IMAGE)
sudo losetup /dev/loop0 $(IMAGE)
uloop0:
sudo losetup -d /dev/loop0
make-filesys:
./make-filesys.sh /dev/loop0
clean-all: uloop0
rm -fr $(IMAGE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment