-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootfs_docker.sh
executable file
·76 lines (55 loc) · 1.68 KB
/
rootfs_docker.sh
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
#!/usr/bin/env sh
set -e -x
# Create a rootfs file from docker container
# See https://github.com/anyfiddle/firecracker-rootfs-builder/blob/main/create-rootfs.sh
# usage: ./rootfs_docker.sh <docker image> <output name>
docker_image=$1
output_name=${2:-image.ext4}
mntdir=/tmp/rootfs
# create and export docker container
container_export=/tmp/rootfs.tar
echo "create and export docker container from ${docker_image} into ${container_export}"
rm -fr $container_export
containerId=$(docker container create $docker_image)
docker export $containerId > $container_export
docker container rm $containerId
# create a mounted ext4 file
output_dir=/tmp/rootfs_output
output=${output_dir}/${output_name}
echo "create a mounted ext4 file ${output}"
# prepare output
mkdir -p $output_dir
# create empty image
rm -f ${output}
truncate -s 100M ${output}
/usr/sbin/mkfs.ext4 ${output}
# mount the image
rm -rf $mntdir
mkdir -p $mntdir
sudo mount -o loop $output $mntdir
# export docker container into a mounted ext4 file
echo "extract the docker container into ${output}"
sudo tar -C $mntdir -xf $container_export
# delete the docker export
rm -fr $container_export
# # prepare the rootfs
init=init.sh
sudo mount -t proc /proc ${mntdir}/proc/
sudo mount -t sysfs /sys ${mntdir}/sys/
sudo mount -o bind /dev ${mntdir}/dev/
sudo cp $init $mntdir
sudo chroot $mntdir /bin/sh $init
sudo rm ${mntdir}/${init}
sudo umount ${mntdir}/dev
sudo umount ${mntdir}/proc
sudo umount ${mntdir}/sys
# unmount the image
sudo umount $mntdir
rm -fr $mntdir
# check image fs
/usr/sbin/e2fsck -y -f $output
# resize image
/usr/sbin/resize2fs -M $output
# check image fs
/usr/sbin/e2fsck -y -f $output
echo "rootfs ready: ${output}"