Debian Codegraph @

dracut/modules.d/59kiwi-lib/kiwi-filesystem-lib.sh

raw
1 #!/bin/bash
2
3 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
4
5 function getOverlayBaseDirectory {
6 # initialize and print overlay base directory below which
7 # the overlayfs based mountpoints are managed
8 local overlay_base=/run/overlay
9 local unit_name
10 local overlay_size
11 mkdir -p "${overlay_base}"
12 if ! mountpoint -q "${overlay_base}";then
13 overlay_size=$(getarg rd.root.overlay.size)
14 [ -z "${overlay_size}" ] && overlay_size="50%"
15 unit_name=$(echo "${overlay_base}" | cut -c 2- | tr / -)
16 cat >/run/systemd/system/"${unit_name}".mount <<-EOF
17 [Unit]
18 Before=initrd-root-fs.target
19 DefaultDependencies=no
20 [Mount]
21 Where=$overlay_base
22 What=tmpfs
23 Type=tmpfs
24 DirectoryMode=0755
25 [Install]
26 WantedBy=multi-user.target
27 EOF
28 systemctl start "${unit_name}".mount
29 fi
30 echo "${overlay_base}"
31 }
32
33 function resize_filesystem {
34 local device=$1
35 test -n "${device}" || return
36 local resize_fs
37 local mpoint=/fs-resize
38 local fstype
39 fstype=$(probe_filesystem "${device}")
40 lock="set_device_lock ${device}"
41 case ${fstype} in
42 ext2|ext3|ext4)
43 resize_fs="${lock} resize2fs -f -p ${device}"
44 ;;
45 btrfs)
46 resize_fs="mkdir -p ${mpoint} && mount ${device} ${mpoint} &&"
47 resize_fs="${resize_fs} ${lock} btrfs filesystem resize max ${mpoint}"
48 resize_fs="${resize_fs};umount ${mpoint} && rmdir ${mpoint}"
49 ;;
50 xfs)
51 resize_fs="mkdir -p ${mpoint} && mount ${device} ${mpoint} &&"
52 resize_fs="${resize_fs} ${lock} xfs_growfs ${mpoint}"
53 resize_fs="${resize_fs};umount ${mpoint} && rmdir ${mpoint}"
54 ;;
55 swap)
56 resize_fs="${lock} mkswap ${device} --label SWAP"
57 ;;
58 *)
59 # don't know how to resize this filesystem
60 warn "Don't know how to resize ${fstype}... skipped"
61 return
62 ;;
63 esac
64 if ! _is_ramdisk_device "${device}"; then
65 if [ ! "${fstype}" = "btrfs" ];then
66 # btrfs reports the free space cache to be outdated/corrupt
67 # after kiwi has resized the partition table. This is an
68 # expected condition prior resize of the filesystem and
69 # should not prevent the following resize. As such the
70 # filesystem check is not applied for btrfs prior resize
71 check_filesystem "${device}"
72 fi
73 fi
74 info "Resizing ${fstype} filesystem on ${device}..."
75 if ! eval "${resize_fs}"; then
76 die "Failed to resize filesystem"
77 fi
78 }
79
80 function check_filesystem {
81 local device=$1
82 test -n "${device}" || return
83 local check_fs
84 local check_fs_return_ok
85 local fstype
86 fstype=$(probe_filesystem "${device}")
87 lock="set_device_lock ${device}"
88 case ${fstype} in
89 ext2|ext3|ext4)
90 # The exit code by e2fsck is the sum of the following conditions:
91 # 0 - No errors
92 # 1 - File system errors corrected
93 # 2 - File system errors corrected, system should be rebooted
94 # 4 - File system errors left uncorrected
95 # 8 - Operational error
96 # 16 - Usage or syntax error
97 # 32 - E2fsck canceled by user request
98 # 128 - Shared library error
99 check_fs="${lock} e2fsck -p -f ${device}"
100 check_fs_return_ok="test \$? -le 2"
101 ;;
102 btrfs)
103 # btrfs check returns a zero exit status if it succeeds.
104 # Non zero is returned in case of failure.
105 check_fs="${lock} btrfsck ${device}"
106 check_fs_return_ok="test \$? -eq 0"
107 ;;
108 xfs)
109 # xfs_repair can be used to check the filesystem. However
110 # for subsequent xfs_growfs no check is needed. Thus for
111 # xfs we skip the checking
112 return
113 ;;
114 *)
115 # don't know how to check this filesystem
116 warn "Don't know how to check ${fstype}... skipped"
117 return
118 ;;
119 esac
120 info "Checking ${fstype} filesystem on ${device}..."
121 eval "${check_fs}"
122 if ! eval "${check_fs_return_ok}"; then
123 die "Failed to check filesystem"
124 fi
125 }
126
127 function probe_filesystem {
128 local device=$1
129 test -n "${device}" || return
130 local fstype
131 fstype=$(blkid "${device}" -s TYPE -o value)
132 if [ -z "${fstype}" ];then
133 fstype=unknown
134 fi
135 if [ "${fstype}" = "crypto_LUKS" ];then
136 fstype=luks
137 fi
138 echo "${fstype}"
139 }
140
141 #======================================
142 # Methods considered private
143 #--------------------------------------
144 function _is_ramdisk_device {
145 echo "$1" | grep -qi "/dev/ram"
146 }