xref: /aosp_15_r20/external/e2fsprogs/tests/f_badsymlinks2/mkimage.sh (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker#!/bin/bash
2*6a54128fSAndroid Build Coastguard Worker
3*6a54128fSAndroid Build Coastguard Worker# This is the script that was used to create the image.gz in this directory.
4*6a54128fSAndroid Build Coastguard Worker
5*6a54128fSAndroid Build Coastguard Workerset -e -u
6*6a54128fSAndroid Build Coastguard Worker
7*6a54128fSAndroid Build Coastguard WorkerBLOCKSIZE=4096
8*6a54128fSAndroid Build Coastguard Worker
9*6a54128fSAndroid Build Coastguard Workerdo_debugfs() {
10*6a54128fSAndroid Build Coastguard Worker	umount mnt
11*6a54128fSAndroid Build Coastguard Worker	debugfs -w "$@" image
12*6a54128fSAndroid Build Coastguard Worker	mount image mnt
13*6a54128fSAndroid Build Coastguard Worker}
14*6a54128fSAndroid Build Coastguard Worker
15*6a54128fSAndroid Build Coastguard Workerdo_tune2fs() {
16*6a54128fSAndroid Build Coastguard Worker	umount mnt
17*6a54128fSAndroid Build Coastguard Worker	tune2fs $@ image
18*6a54128fSAndroid Build Coastguard Worker	mount image mnt
19*6a54128fSAndroid Build Coastguard Worker}
20*6a54128fSAndroid Build Coastguard Worker
21*6a54128fSAndroid Build Coastguard Workersymlink() {
22*6a54128fSAndroid Build Coastguard Worker	local len=$1
23*6a54128fSAndroid Build Coastguard Worker	local src=$2
24*6a54128fSAndroid Build Coastguard Worker	local target=$(perl -e 'print "A" x '$len)
25*6a54128fSAndroid Build Coastguard Worker	ln -s $target $src
26*6a54128fSAndroid Build Coastguard Worker	stat -c %i $src
27*6a54128fSAndroid Build Coastguard Worker}
28*6a54128fSAndroid Build Coastguard Worker
29*6a54128fSAndroid Build Coastguard Worker# Overwrite the length in the header of the encrypted symlink target
30*6a54128fSAndroid Build Coastguard Workerset_encrypted_symlink_len() {
31*6a54128fSAndroid Build Coastguard Worker	local ino=$1
32*6a54128fSAndroid Build Coastguard Worker	local len=$2
33*6a54128fSAndroid Build Coastguard Worker
34*6a54128fSAndroid Build Coastguard Worker	echo "zap_block -f <$ino> -p $((len%256)) -o 0 -l 1 0"
35*6a54128fSAndroid Build Coastguard Worker	echo "zap_block -f <$ino> -p $((len/256)) -o 1 -l 1 0"
36*6a54128fSAndroid Build Coastguard Worker}
37*6a54128fSAndroid Build Coastguard Worker
38*6a54128fSAndroid Build Coastguard Workercreate_symlinks() {
39*6a54128fSAndroid Build Coastguard Worker	local dir=$1
40*6a54128fSAndroid Build Coastguard Worker	local encrypted=${2:-false}
41*6a54128fSAndroid Build Coastguard Worker	local overhead=0
42*6a54128fSAndroid Build Coastguard Worker	local ino
43*6a54128fSAndroid Build Coastguard Worker
44*6a54128fSAndroid Build Coastguard Worker	if $encrypted; then
45*6a54128fSAndroid Build Coastguard Worker		overhead=2
46*6a54128fSAndroid Build Coastguard Worker	fi
47*6a54128fSAndroid Build Coastguard Worker
48*6a54128fSAndroid Build Coastguard Worker	mkdir -p $dir
49*6a54128fSAndroid Build Coastguard Worker
50*6a54128fSAndroid Build Coastguard Worker	{
51*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink 1 $dir/empty)
52*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size 10"
53*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> block[0] 0"
54*6a54128fSAndroid Build Coastguard Worker
55*6a54128fSAndroid Build Coastguard Worker	symlink 1 $dir/fast_min > /dev/null
56*6a54128fSAndroid Build Coastguard Worker
57*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink 10 $dir/fast_isize_too_small)
58*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size 1"
59*6a54128fSAndroid Build Coastguard Worker
60*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink 10 $dir/fast_isize_too_large)
61*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size 20"
62*6a54128fSAndroid Build Coastguard Worker
63*6a54128fSAndroid Build Coastguard Worker	symlink $((59 - overhead)) $dir/fast_max > /dev/null
64*6a54128fSAndroid Build Coastguard Worker
65*6a54128fSAndroid Build Coastguard Worker	symlink $((60 - overhead)) $dir/slow_min > /dev/null
66*6a54128fSAndroid Build Coastguard Worker
67*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink 100 $dir/slow_isize_too_small)
68*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size 80"
69*6a54128fSAndroid Build Coastguard Worker
70*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink 100 $dir/slow_isize_too_large)
71*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size 120"
72*6a54128fSAndroid Build Coastguard Worker
73*6a54128fSAndroid Build Coastguard Worker	symlink $((BLOCKSIZE - 1 - overhead)) $dir/slow_max > /dev/null
74*6a54128fSAndroid Build Coastguard Worker
75*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink $((BLOCKSIZE - 1 - overhead)) $dir/one_too_long)
76*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size $BLOCKSIZE"
77*6a54128fSAndroid Build Coastguard Worker	echo "zap_block -f <$ino> -p 65 0"
78*6a54128fSAndroid Build Coastguard Worker	if $encrypted; then
79*6a54128fSAndroid Build Coastguard Worker		set_encrypted_symlink_len $ino $((BLOCKSIZE - overhead))
80*6a54128fSAndroid Build Coastguard Worker	fi
81*6a54128fSAndroid Build Coastguard Worker
82*6a54128fSAndroid Build Coastguard Worker	ino=$(symlink $((BLOCKSIZE - 1 - overhead)) $dir/too_long)
83*6a54128fSAndroid Build Coastguard Worker	echo "set_inode_field <$ino> i_size $((BLOCKSIZE + 1000))"
84*6a54128fSAndroid Build Coastguard Worker	echo "zap_block -f <$ino> -p 65 0"
85*6a54128fSAndroid Build Coastguard Worker	if $encrypted; then
86*6a54128fSAndroid Build Coastguard Worker		set_encrypted_symlink_len $ino $((BLOCKSIZE + 1000 - overhead))
87*6a54128fSAndroid Build Coastguard Worker	fi
88*6a54128fSAndroid Build Coastguard Worker
89*6a54128fSAndroid Build Coastguard Worker	} >> debugfs_commands
90*6a54128fSAndroid Build Coastguard Worker	do_debugfs < debugfs_commands
91*6a54128fSAndroid Build Coastguard Worker}
92*6a54128fSAndroid Build Coastguard Worker
93*6a54128fSAndroid Build Coastguard Workercreate_encrypted_symlinks() {
94*6a54128fSAndroid Build Coastguard Worker	local dir=$1 link
95*6a54128fSAndroid Build Coastguard Worker
96*6a54128fSAndroid Build Coastguard Worker	mkdir $dir
97*6a54128fSAndroid Build Coastguard Worker	echo | e4crypt add_key $dir
98*6a54128fSAndroid Build Coastguard Worker	create_symlinks $dir true
99*6a54128fSAndroid Build Coastguard Worker
100*6a54128fSAndroid Build Coastguard Worker	# Move symlinks into an unencrypted directory (leaving their targets
101*6a54128fSAndroid Build Coastguard Worker	# encrypted).  This makes the fsck output consistent.
102*6a54128fSAndroid Build Coastguard Worker	mv $dir ${dir}~encrypted
103*6a54128fSAndroid Build Coastguard Worker	mkdir $dir
104*6a54128fSAndroid Build Coastguard Worker	mv ${dir}~encrypted/* $dir
105*6a54128fSAndroid Build Coastguard Worker}
106*6a54128fSAndroid Build Coastguard Worker
107*6a54128fSAndroid Build Coastguard Workermkdir -p mnt
108*6a54128fSAndroid Build Coastguard Workerumount mnt &> /dev/null || true
109*6a54128fSAndroid Build Coastguard Workerdd if=/dev/zero of=image bs=1024 count=600
110*6a54128fSAndroid Build Coastguard Worker
111*6a54128fSAndroid Build Coastguard Workermke2fs -O 'encrypt,^extents,^64bit' -b $BLOCKSIZE -I 256 image
112*6a54128fSAndroid Build Coastguard Workermount image mnt
113*6a54128fSAndroid Build Coastguard Worker
114*6a54128fSAndroid Build Coastguard Workercreate_symlinks mnt/default
115*6a54128fSAndroid Build Coastguard Workercreate_encrypted_symlinks mnt/encrypted
116*6a54128fSAndroid Build Coastguard Worker
117*6a54128fSAndroid Build Coastguard Workerdo_tune2fs -O extents
118*6a54128fSAndroid Build Coastguard Workercreate_symlinks mnt/extents
119*6a54128fSAndroid Build Coastguard Workercreate_encrypted_symlinks mnt/extents_encrypted
120*6a54128fSAndroid Build Coastguard Worker
121*6a54128fSAndroid Build Coastguard Workerdo_debugfs -R 'feature inline_data'
122*6a54128fSAndroid Build Coastguard Workercreate_symlinks mnt/inline_data
123*6a54128fSAndroid Build Coastguard Worker
124*6a54128fSAndroid Build Coastguard Workerrm -rf debugfs_commands mnt/*~encrypted
125*6a54128fSAndroid Build Coastguard Workerumount mnt
126*6a54128fSAndroid Build Coastguard Workerrmdir mnt
127*6a54128fSAndroid Build Coastguard Workergzip -9 -f image
128