aboutsummaryrefslogtreecommitdiff
path: root/debian-container/lib/storage.sh
blob: e72c0b9da3737c9310b794f9e90e7e4f680fd508 (plain)
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
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2026 by Richard Qin

dc_create_image() {
  local size=$1

  mkdir -p "$DC_INSTANCE_DIR" "$DC_ROOT"
  truncate -s "$size" "$DC_IMAGE"
  mkfs.ext4 -F -q -L DEBIAN_CONTAINER "$DC_IMAGE"
}

dc_resize_image() {
  local size=$1
  local current_size
  local target_size
  local allocated_blocks
  local block_size

  dc_require_commands e2fsck findmnt numfmt resize2fs stat truncate
  if findmnt --mountpoint "$DC_ROOT" >/dev/null 2>&1; then
    dc_die "实例仍处于挂载状态:$DC_INSTANCE"
  fi

  current_size=$(stat -c '%s' "$DC_IMAGE")
  target_size=$(numfmt --from=iec "$size")
  ((target_size > current_size)) || dc_die "扩容大小必须大于当前镜像大小:$current_size bytes"

  truncate -s "$size" "$DC_IMAGE"
  if e2fsck -f -y "$DC_IMAGE"; then
    :
  else
    [[ $? -eq 1 ]] || dc_die "文件系统检查失败:$DC_IMAGE"
  fi
  resize2fs "$DC_IMAGE"
}

dc_status_image() {
  local allocated_blocks
  local block_size

  if [[ -f "$DC_IMAGE" ]]; then
    allocated_blocks=$(stat -c '%b' "$DC_IMAGE")
    block_size=$(stat -c '%B' "$DC_IMAGE")
    echo "逻辑大小:$(stat -c '%s' "$DC_IMAGE") bytes"
    echo "实际占用:$((allocated_blocks * block_size)) bytes"
  else
    echo '镜像:不存在'
  fi
}